fix #64
[memoization.java.git] / memoization-guava / src / main / java / de / xn__ho_hia / memoization / guava / GuavaMemoize.java
blob28e2b0899bd561e20bf2a4442eb03c82e19b806c
1 /*
2 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package de.xn__ho_hia.memoization.guava;
9 import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.defaultKeySupplier;
10 import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.doubleBinaryOperatorHashCodeKeyFunction;
11 import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.hashCodeKeyFunction;
12 import static de.xn__ho_hia.memoization.shared.MemoizationDefaults.intBinaryOperatorHashCodeKeyFunction;
13 import static java.util.function.Function.identity;
15 import java.util.function.BiConsumer;
16 import java.util.function.BiFunction;
17 import java.util.function.BiPredicate;
18 import java.util.function.BooleanSupplier;
19 import java.util.function.Consumer;
20 import java.util.function.DoubleBinaryOperator;
21 import java.util.function.DoubleConsumer;
22 import java.util.function.DoubleFunction;
23 import java.util.function.DoublePredicate;
24 import java.util.function.DoubleSupplier;
25 import java.util.function.Function;
26 import java.util.function.IntBinaryOperator;
27 import java.util.function.IntConsumer;
28 import java.util.function.IntFunction;
29 import java.util.function.IntPredicate;
30 import java.util.function.IntSupplier;
31 import java.util.function.LongConsumer;
32 import java.util.function.LongFunction;
33 import java.util.function.LongPredicate;
34 import java.util.function.LongSupplier;
35 import java.util.function.Predicate;
36 import java.util.function.Supplier;
38 import com.google.common.cache.Cache;
39 import com.google.common.cache.CacheBuilder;
41 import de.xn__ho_hia.memoization.shared.DoubleBinaryFunction;
42 import de.xn__ho_hia.memoization.shared.IntBinaryFunction;
43 import de.xn__ho_hia.memoization.shared.MemoizationDefaults;
45 /**
46 * <p>
47 * Factory for lightweight wrappers that store the result of a potentially expensive function call. Each method of this
48 * class exposes two of the following features:
49 * </p>
50 * <strong>Default cache</strong>
51 * <p>
52 * The memoizer uses the default cache of this factory. Current implementation creates a new {@link Cache} per memoizer.
53 * </p>
54 * <strong>Default cache key</strong>
55 * <p>
56 * The memoizer uses the default {@link BiFunction} or {@link Supplier} to calculate the cache key for each call. Either
57 * uses the natural key (e.g. the input itself) or one of the methods in {@link MemoizationDefaults}.
58 * </p>
59 * <strong>Custom cache</strong>
60 * <p>
61 * The memoizer uses a user-provided {@link Cache} as its cache. It is possible to add values to the cache both before
62 * and after the memoizer was created.
63 * </p>
64 * <strong>Custom cache key</strong>
65 * <p>
66 * The memoizer uses a user-defined {@link BiFunction} or {@link Supplier} to calculate the cache key for each call.
67 * Take a look at {@link MemoizationDefaults} for a possible key functions and suppliers.
68 * </p>
70 * @see BiConsumer
71 * @see BiFunction
72 * @see BiPredicate
73 * @see BooleanSupplier
74 * @see Consumer
75 * @see DoubleBinaryOperator
76 * @see DoubleConsumer
77 * @see DoubleFunction
78 * @see DoublePredicate
79 * @see DoubleSupplier
80 * @see Function
81 * @see IntBinaryOperator
82 * @see IntConsumer
83 * @see IntFunction
84 * @see IntPredicate
85 * @see IntSupplier
86 * @see LongConsumer
87 * @see LongFunction
88 * @see LongPredicate
89 * @see LongSupplier
90 * @see Predicate
91 * @see Supplier
92 * @see <a href="https://en.wikipedia.org/wiki/Memoization">Wikipedia: Memoization</a>
94 public final class GuavaMemoize {
96 private GuavaMemoize() {
97 // factory class
101 * <p>
102 * Memoizes a {@link BiConsumer} in a Guava {@link Cache}.
103 * </p>
104 * <h3>Features</h3>
105 * <ul>
106 * <li>Default cache</li>
107 * <li>Default cache key</li>
108 * </ul>
110 * @param biConsumer
111 * The {@link BiConsumer} to memoize.
112 * @return The wrapped {@link BiConsumer}.
114 public static final <FIRST, SECOND> BiConsumer<FIRST, SECOND> biConsumer(
115 final BiConsumer<FIRST, SECOND> biConsumer) {
116 return biConsumer(biConsumer, CacheBuilder.newBuilder().build());
120 * <p>
121 * Memoizes a {@link BiConsumer} in a Guava {@link Cache}.
122 * </p>
123 * <h3>Features</h3>
124 * <ul>
125 * <li>Default cache</li>
126 * <li>Custom cache key</li>
127 * </ul>
129 * @param biConsumer
130 * The {@link BiConsumer} to memoize.
131 * @param keyFunction
132 * The {@link BiFunction} to compute the cache key.
133 * @return The wrapped {@link BiConsumer}.
135 public static final <FIRST, SECOND, KEY> BiConsumer<FIRST, SECOND> biConsumer(
136 final BiConsumer<FIRST, SECOND> biConsumer,
137 final BiFunction<FIRST, SECOND, KEY> keyFunction) {
138 return biConsumer(biConsumer, keyFunction, CacheBuilder.newBuilder().build());
142 * <p>
143 * Memoizes a {@link BiConsumer} in a Guava {@link Cache}.
144 * </p>
145 * <h3>Features</h3>
146 * <ul>
147 * <li>Custom cache</li>
148 * <li>Custom cache key</li>
149 * </ul>
151 * @param biConsumer
152 * The {@link BiConsumer} to memoize.
153 * @param keyFunction
154 * The {@link BiFunction} to compute the cache key.
155 * @param cache
156 * The {@link Cache} to use.
157 * @return The wrapped {@link BiConsumer}.
159 public static final <FIRST, SECOND, KEY> BiConsumer<FIRST, SECOND> biConsumer(
160 final BiConsumer<FIRST, SECOND> biConsumer,
161 final BiFunction<FIRST, SECOND, KEY> keyFunction,
162 final Cache<KEY, KEY> cache) {
163 return new GuavaCacheBasedBiConsumerMemoizer<>(cache, keyFunction, biConsumer);
167 * <p>
168 * Memoizes a {@link BiConsumer} in a Guava {@link Cache}.
169 * </p>
170 * <h3>Features</h3>
171 * <ul>
172 * <li>Custom cache</li>
173 * <li>Default cache key</li>
174 * </ul>
176 * @param biConsumer
177 * The {@link BiConsumer} to memoize.
178 * @param cache
179 * The {@link Cache} to use.
180 * @return The wrapped {@link BiConsumer}.
182 public static final <FIRST, SECOND> BiConsumer<FIRST, SECOND> biConsumer(
183 final BiConsumer<FIRST, SECOND> biConsumer,
184 final Cache<String, String> cache) {
185 return biConsumer(biConsumer, hashCodeKeyFunction(), cache);
189 * <p>
190 * Memoizes a {@link BiFunction} in a Guava {@link Cache}.
191 * </p>
192 * <h3>Features</h3>
193 * <ul>
194 * <li>Default cache</li>
195 * <li>Default cache key</li>
196 * </ul>
198 * @param biFunction
199 * The {@link BiFunction} to memoize.
200 * @return The wrapped {@link BiFunction}.
202 public static final <FIRST, SECOND, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biFunction(
203 final BiFunction<FIRST, SECOND, OUTPUT> biFunction) {
204 return biFunction(biFunction, CacheBuilder.newBuilder().build());
208 * <p>
209 * Memoizes a {@link BiFunction} in a Guava {@link Cache}.
210 * </p>
211 * <h3>Features</h3>
212 * <ul>
213 * <li>Default cache</li>
214 * <li>Custom cache key</li>
215 * </ul>
217 * @param biFunction
218 * The {@link BiFunction} to memoize.
219 * @param keyFunction
220 * The {@link BiFunction} to compute the cache key.
221 * @return The wrapped {@link BiFunction}.
223 public static final <FIRST, SECOND, KEY, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biFunction(
224 final BiFunction<FIRST, SECOND, OUTPUT> biFunction,
225 final BiFunction<FIRST, SECOND, KEY> keyFunction) {
226 return biFunction(biFunction, keyFunction, CacheBuilder.newBuilder().build());
230 * <p>
231 * Memoizes a {@link BiFunction} in a Guava {@link Cache}.
232 * </p>
233 * <h3>Features</h3>
234 * <ul>
235 * <li>Custom cache</li>
236 * <li>Custom cache key</li>
237 * </ul>
239 * @param biFunction
240 * The {@link BiFunction} to memoize.
241 * @param keyFunction
242 * The {@link BiFunction} to compute the cache key.
243 * @param cache
244 * The {@link Cache} to use.
245 * @return The wrapped {@link BiFunction}.
247 public static final <FIRST, SECOND, KEY, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biFunction(
248 final BiFunction<FIRST, SECOND, OUTPUT> biFunction,
249 final BiFunction<FIRST, SECOND, KEY> keyFunction,
250 final Cache<KEY, OUTPUT> cache) {
251 return new GuavaCacheBasedBiFunctionMemoizer<>(cache, keyFunction, biFunction);
255 * <p>
256 * Memoizes a {@link BiFunction} in a Guava {@link Cache}.
257 * </p>
258 * <h3>Features</h3>
259 * <ul>
260 * <li>Custom cache</li>
261 * <li>Default cache key</li>
262 * </ul>
264 * @param biFunction
265 * The {@link BiFunction} to memoize.
266 * @param cache
267 * The {@link Cache} to use.
268 * @return The wrapped {@link BiFunction}.
270 public static final <FIRST, SECOND, OUTPUT> BiFunction<FIRST, SECOND, OUTPUT> biFunction(
271 final BiFunction<FIRST, SECOND, OUTPUT> biFunction,
272 final Cache<String, OUTPUT> cache) {
273 return biFunction(biFunction, hashCodeKeyFunction(), cache);
277 * <p>
278 * Memoizes a {@link BiPredicate} in a Guava {@link Cache}.
279 * </p>
280 * <h3>Features</h3>
281 * <ul>
282 * <li>Default cache</li>
283 * <li>Default cache key</li>
284 * </ul>
286 * @param biPredicate
287 * The {@link BiPredicate} to memoize.
288 * @return The wrapped {@link BiPredicate}.
290 public static final <FIRST, SECOND> BiPredicate<FIRST, SECOND> biPredicate(
291 final BiPredicate<FIRST, SECOND> biPredicate) {
292 return biPredicate(biPredicate, CacheBuilder.newBuilder().build());
296 * <p>
297 * Memoizes a {@link BiPredicate} in a Guava {@link Cache}.
298 * </p>
299 * <h3>Features</h3>
300 * <ul>
301 * <li>Default cache</li>
302 * <li>Custom cache key</li>
303 * </ul>
305 * @param biPredicate
306 * The {@link BiPredicate} to memoize.
307 * @param keyFunction
308 * The {@link BiFunction} to compute the cache key.
309 * @return The wrapped {@link BiPredicate}.
311 public static final <FIRST, SECOND, KEY> BiPredicate<FIRST, SECOND> biPredicate(
312 final BiPredicate<FIRST, SECOND> biPredicate,
313 final BiFunction<FIRST, SECOND, KEY> keyFunction) {
314 return biPredicate(biPredicate, keyFunction, CacheBuilder.newBuilder().build());
318 * <p>
319 * Memoizes a {@link BiPredicate} in a Guava {@link Cache}.
320 * </p>
321 * <h3>Features</h3>
322 * <ul>
323 * <li>Custom cache</li>
324 * <li>Custom cache key</li>
325 * </ul>
327 * @param biPredicate
328 * The {@link BiPredicate} to memoize.
329 * @param keyFunction
330 * The {@link BiFunction} to compute the cache key.
331 * @param cache
332 * The {@link Cache} to use.
333 * @return The wrapped {@link BiPredicate}.
335 public static final <FIRST, SECOND, KEY> BiPredicate<FIRST, SECOND> biPredicate(
336 final BiPredicate<FIRST, SECOND> biPredicate,
337 final BiFunction<FIRST, SECOND, KEY> keyFunction,
338 final Cache<KEY, Boolean> cache) {
339 return new GuavaCacheBasedBiPredicateMemoizer<>(cache, keyFunction, biPredicate);
343 * <p>
344 * Memoizes a {@link BiPredicate} in a Guava {@link Cache}.
345 * </p>
346 * <h3>Features</h3>
347 * <ul>
348 * <li>Custom cache</li>
349 * <li>Default cache key</li>
350 * </ul>
352 * @param biPredicate
353 * The {@link BiPredicate} to memoize.
354 * @param cache
355 * The {@link Cache} to use.
356 * @return The wrapped {@link BiPredicate}.
358 public static final <FIRST, SECOND> BiPredicate<FIRST, SECOND> biPredicate(
359 final BiPredicate<FIRST, SECOND> biPredicate,
360 final Cache<String, Boolean> cache) {
361 return biPredicate(biPredicate, hashCodeKeyFunction(), cache);
365 * <p>
366 * Memoizes a {@link BooleanSupplier} in a Guava {@link Cache}.
367 * </p>
368 * <h3>Features</h3>
369 * <ul>
370 * <li>Default cache</li>
371 * <li>Default cache key</li>
372 * </ul>
374 * @param booleanSupplier
375 * The {@link BooleanSupplier} to memoize.
376 * @return The wrapped {@link BooleanSupplier}.
378 public static final BooleanSupplier booleanSupplier(final BooleanSupplier booleanSupplier) {
379 return booleanSupplier(booleanSupplier, CacheBuilder.newBuilder().build());
383 * <p>
384 * Memoizes a {@link BooleanSupplier} in a Guava {@link Cache}.
385 * </p>
386 * <h3>Features</h3>
387 * <ul>
388 * <li>Custom cache</li>
389 * <li>Default cache key</li>
390 * </ul>
392 * @param booleanSupplier
393 * The {@link BooleanSupplier} to memoize.
394 * @param cache
395 * The {@link Cache} to use.
396 * @return The wrapped {@link BooleanSupplier}.
398 public static final BooleanSupplier booleanSupplier(
399 final BooleanSupplier booleanSupplier,
400 final Cache<String, Boolean> cache) {
401 return booleanSupplier(booleanSupplier, defaultKeySupplier(), cache);
405 * <p>
406 * Memoizes a {@link BooleanSupplier} in a Guava {@link Cache}.
407 * </p>
408 * <h3>Features</h3>
409 * <ul>
410 * <li>Default cache</li>
411 * <li>Custom cache key</li>
412 * </ul>
414 * @param booleanSupplier
415 * The {@link BooleanSupplier} to memoize.
416 * @param keySupplier
417 * The {@link Supplier} for the cache key.
418 * @return The wrapped {@link BooleanSupplier}.
420 public static final <KEY> BooleanSupplier booleanSupplier(
421 final BooleanSupplier booleanSupplier,
422 final Supplier<KEY> keySupplier) {
423 return booleanSupplier(booleanSupplier, keySupplier, CacheBuilder.newBuilder().build());
427 * <p>
428 * Memoizes a {@link BooleanSupplier} in a Guava {@link Cache}.
429 * </p>
430 * <h3>Features</h3>
431 * <ul>
432 * <li>Custom cache</li>
433 * <li>Custom cache key</li>
434 * </ul>
436 * @param booleanSupplier
437 * The {@link BooleanSupplier} to memoize.
438 * @param keySupplier
439 * The {@link Supplier} for the cache key.
440 * @param cache
441 * The {@link Cache} to use.
442 * @return The wrapped {@link BooleanSupplier}.
444 public static final <KEY> BooleanSupplier booleanSupplier(
445 final BooleanSupplier booleanSupplier,
446 final Supplier<KEY> keySupplier,
447 final Cache<KEY, Boolean> cache) {
448 return new GuavaCacheBasedBooleanSupplierMemoizer<>(cache, keySupplier, booleanSupplier);
452 * <p>
453 * Memoizes a {@link Consumer} in a Guava {@link Cache}.
454 * </p>
455 * <h3>Features</h3>
456 * <ul>
457 * <li>Default cache</li>
458 * <li>Default cache key</li>
459 * </ul>
461 * @param consumer
462 * The {@link Consumer} to memoize.
463 * @return The wrapped {@link Consumer}.
465 public static final <INPUT> Consumer<INPUT> consumer(
466 final Consumer<INPUT> consumer) {
467 return consumer(consumer, CacheBuilder.newBuilder().build());
471 * <p>
472 * Memoizes a {@link Consumer} in a Guava {@link Cache}.
473 * </p>
474 * <h3>Features</h3>
475 * <ul>
476 * <li>Custom cache</li>
477 * <li>Default cache key</li>
478 * </ul>
480 * @param consumer
481 * The {@link Consumer} to memoize.
482 * @param cache
483 * The {@link Cache} to use.
484 * @return The wrapped {@link Consumer}.
486 public static final <INPUT> Consumer<INPUT> consumer(
487 final Consumer<INPUT> consumer,
488 final Cache<INPUT, INPUT> cache) {
489 return consumer(consumer, identity(), cache);
493 * <p>
494 * Memoizes a {@link Consumer} in a Guava {@link Cache}.
495 * </p>
496 * <h3>Features</h3>
497 * <ul>
498 * <li>Default cache</li>
499 * <li>Custom cache key</li>
500 * </ul>
502 * @param consumer
503 * The {@link Consumer} to memoize.
504 * @param keyFunction
505 * The {@link Function} to compute the cache key.
506 * @return The wrapped {@link Consumer}.
508 public static final <INPUT, KEY> Consumer<INPUT> consumer(
509 final Consumer<INPUT> consumer,
510 final Function<INPUT, KEY> keyFunction) {
511 return consumer(consumer, keyFunction, CacheBuilder.newBuilder().build());
515 * <p>
516 * Memoizes a {@link Consumer} in a Guava {@link Cache}.
517 * </p>
518 * <h3>Features</h3>
519 * <ul>
520 * <li>Custom cache</li>
521 * <li>Custom cache key</li>
522 * </ul>
524 * @param consumer
525 * The {@link Consumer} to memoize.
526 * @param keyFunction
527 * The {@link Function} to compute the cache key.
528 * @param cache
529 * The {@link Cache} to use.
530 * @return The wrapped {@link Consumer}.
532 public static final <INPUT, KEY> Consumer<INPUT> consumer(
533 final Consumer<INPUT> consumer,
534 final Function<INPUT, KEY> keyFunction,
535 final Cache<KEY, INPUT> cache) {
536 return new GuavaCacheBasedConsumerMemoizer<>(cache, keyFunction, consumer);
540 * <p>
541 * Memoizes a {@link DoubleBinaryOperator} in a Guava {@link Cache}.
542 * </p>
543 * <h3>Features</h3>
544 * <ul>
545 * <li>Default cache</li>
546 * <li>Default cache key</li>
547 * </ul>
549 * @param doubleBinaryOperator
550 * The {@link DoubleBinaryOperator} to memoize.
551 * @return The wrapped {@link DoubleBinaryOperator}.
553 public static final DoubleBinaryOperator doubleBinaryOperator(
554 final DoubleBinaryOperator doubleBinaryOperator) {
555 return doubleBinaryOperator(doubleBinaryOperator, CacheBuilder.newBuilder().build());
559 * <p>
560 * Memoizes a {@link DoubleBinaryOperator} in a Guava {@link Cache}.
561 * </p>
562 * <h3>Features</h3>
563 * <ul>
564 * <li>Default cache</li>
565 * <li>Custom cache key</li>
566 * </ul>
568 * @param doubleBinaryOperator
569 * The {@link DoubleBinaryOperator} to memoize.
570 * @param keyFunction
571 * The {@link DoubleBinaryFunction} to compute the cache key.
572 * @return The wrapped {@link DoubleBinaryOperator}.
574 public static final <KEY> DoubleBinaryOperator doubleBinaryOperator(
575 final DoubleBinaryOperator doubleBinaryOperator,
576 final DoubleBinaryFunction<KEY> keyFunction) {
577 return doubleBinaryOperator(doubleBinaryOperator, keyFunction, CacheBuilder.newBuilder().build());
581 * <p>
582 * Memoizes a {@link DoubleBinaryOperator} in a Guava {@link Cache}.
583 * </p>
584 * <h3>Features</h3>
585 * <ul>
586 * <li>Custom cache</li>
587 * <li>Default cache key</li>
588 * </ul>
590 * @param doubleBinaryOperator
591 * The {@link DoubleBinaryOperator} to memoize.
592 * @param cache
593 * The {@link Cache} to use.
594 * @return The wrapped {@link DoubleBinaryOperator}.
596 public static final DoubleBinaryOperator doubleBinaryOperator(
597 final DoubleBinaryOperator doubleBinaryOperator,
598 final Cache<String, Double> cache) {
599 return doubleBinaryOperator(doubleBinaryOperator, doubleBinaryOperatorHashCodeKeyFunction(), cache);
603 * <p>
604 * Memoizes a {@link DoubleBinaryOperator} in a Guava {@link Cache}.
605 * </p>
606 * <h3>Features</h3>
607 * <ul>
608 * <li>Custom cache</li>
609 * <li>Custom cache key</li>
610 * </ul>
612 * @param doubleBinaryOperator
613 * The {@link DoubleBinaryOperator} to memoize.
614 * @param keyFunction
615 * The {@link DoubleBinaryFunction} to compute the cache key.
616 * @param cache
617 * The {@link Cache} to use.
618 * @return The wrapped {@link DoubleBinaryOperator}.
620 public static final <KEY> DoubleBinaryOperator doubleBinaryOperator(
621 final DoubleBinaryOperator doubleBinaryOperator,
622 final DoubleBinaryFunction<KEY> keyFunction,
623 final Cache<KEY, Double> cache) {
624 return new GuavaCacheBasedDoubleBinaryOperatorMemoizer<>(cache, keyFunction, doubleBinaryOperator);
628 * <p>
629 * Memoizes a {@link DoubleConsumer} in a Guava {@link Cache}.
630 * </p>
631 * <h3>Features</h3>
632 * <ul>
633 * <li>Default cache</li>
634 * <li>Default cache key</li>
635 * </ul>
637 * @param doubleConsumer
638 * The {@link DoubleConsumer} to memoize.
639 * @return The wrapped {@link DoubleConsumer}.
641 public static final DoubleConsumer doubleConsumer(
642 final DoubleConsumer doubleConsumer) {
643 return doubleConsumer(doubleConsumer, CacheBuilder.newBuilder().build());
647 * <p>
648 * Memoizes a {@link DoubleConsumer} in a Guava {@link Cache}.
649 * </p>
650 * <h3>Features</h3>
651 * <ul>
652 * <li>Custom cache</li>
653 * <li>Default cache key</li>
654 * </ul>
656 * @param doubleConsumer
657 * The {@link DoubleConsumer} to memoize.
658 * @param cache
659 * The {@link Cache} to use.
660 * @return The wrapped {@link DoubleConsumer}.
662 public static final DoubleConsumer doubleConsumer(
663 final DoubleConsumer doubleConsumer,
664 final Cache<Double, Double> cache) {
665 return doubleConsumer(doubleConsumer, Double::valueOf, cache);
669 * <p>
670 * Memoizes a {@link DoubleConsumer} in a Guava {@link Cache}.
671 * </p>
672 * <h3>Features</h3>
673 * <ul>
674 * <li>Default cache</li>
675 * <li>Custom cache key</li>
676 * </ul>
678 * @param doubleConsumer
679 * The {@link DoubleConsumer} to memoize.
680 * @param keyFunction
681 * The {@link DoubleFunction} to compute the cache key.
682 * @return The wrapped {@link DoubleConsumer}.
684 public static final <KEY> DoubleConsumer doubleConsumer(
685 final DoubleConsumer doubleConsumer,
686 final DoubleFunction<KEY> keyFunction) {
687 return doubleConsumer(doubleConsumer, keyFunction, CacheBuilder.newBuilder().build());
691 * <p>
692 * Memoizes a {@link DoubleConsumer} in a Guava {@link Cache}.
693 * </p>
694 * <h3>Features</h3>
695 * <ul>
696 * <li>Custom cache</li>
697 * <li>Custom cache key</li>
698 * </ul>
700 * @param doubleConsumer
701 * The {@link DoubleConsumer} to memoize.
702 * @param keyFunction
703 * The {@link Function} to compute the cache key.
704 * @param cache
705 * The {@link Cache} to use.
706 * @return The wrapped {@link DoubleConsumer}.
708 public static final <KEY> DoubleConsumer doubleConsumer(
709 final DoubleConsumer doubleConsumer,
710 final DoubleFunction<KEY> keyFunction,
711 final Cache<KEY, Double> cache) {
712 return new GuavaCacheBasedDoubleConsumerMemoizer<>(cache, keyFunction, doubleConsumer);
716 * <p>
717 * Memoizes a {@link DoubleFunction} in a Guava {@link Cache}.
718 * </p>
719 * <h3>Features</h3>
720 * <ul>
721 * <li>Default cache</li>
722 * <li>Default cache key</li>
723 * </ul>
725 * @param function
726 * The {@link DoubleFunction} to memoize.
727 * @return The wrapped {@link DoubleFunction}.
729 public static final <OUTPUT> DoubleFunction<OUTPUT> doubleFunction(
730 final DoubleFunction<OUTPUT> function) {
731 return doubleFunction(function, CacheBuilder.newBuilder().build());
735 * <p>
736 * Memoizes a {@link DoubleFunction} in a Guava {@link Cache}.
737 * </p>
738 * <h3>Features</h3>
739 * <ul>
740 * <li>Custom cache</li>
741 * <li>Default cache key</li>
742 * </ul>
744 * @param function
745 * The {@link DoubleFunction} to memoize.
746 * @param cache
747 * The {@link Cache} to use.
748 * @return The wrapped {@link DoubleFunction}.
750 public static final <OUTPUT> DoubleFunction<OUTPUT> doubleFunction(
751 final DoubleFunction<OUTPUT> function,
752 final Cache<Double, OUTPUT> cache) {
753 return doubleFunction(function, Double::valueOf, cache);
757 * <p>
758 * Memoizes a {@link DoubleFunction} in a Guava {@link Cache}.
759 * </p>
760 * <h3>Features</h3>
761 * <ul>
762 * <li>Default cache</li>
763 * <li>Custom cache key</li>
764 * </ul>
766 * @param function
767 * The {@link DoubleFunction} to memoize.
768 * @param keyFunction
769 * The {@link DoubleFunction} to compute the cache key.
770 * @return The wrapped {@link DoubleFunction}.
772 public static final <KEY, OUTPUT> DoubleFunction<OUTPUT> doubleFunction(
773 final DoubleFunction<OUTPUT> function,
774 final DoubleFunction<KEY> keyFunction) {
775 return doubleFunction(function, keyFunction, CacheBuilder.newBuilder().build());
779 * <p>
780 * Memoizes a {@link DoubleFunction} in a Guava {@link Cache}.
781 * </p>
782 * <h3>Features</h3>
783 * <ul>
784 * <li>Custom cache</li>
785 * <li>Custom cache key</li>
786 * </ul>
788 * @param function
789 * The {@link DoubleFunction} to memoize.
790 * @param keyFunction
791 * The {@link DoubleFunction} to compute the cache key.
792 * @param cache
793 * The {@link Cache} to use.
794 * @return The wrapped {@link DoubleFunction}.
796 public static final <KEY, OUTPUT> DoubleFunction<OUTPUT> doubleFunction(
797 final DoubleFunction<OUTPUT> function,
798 final DoubleFunction<KEY> keyFunction,
799 final Cache<KEY, OUTPUT> cache) {
800 return new GuavaCacheBasedDoubleFunctionMemoizer<>(cache, keyFunction, function);
804 * <p>
805 * Memoizes a {@link DoublePredicate} in a Guava {@link Cache}.
806 * </p>
807 * <h3>Features</h3>
808 * <ul>
809 * <li>Default cache</li>
810 * <li>Default cache key</li>
811 * </ul>
813 * @param doublePredicate
814 * The {@link DoublePredicate} to memoize.
815 * @return The wrapped {@link DoublePredicate}.
817 public static final DoublePredicate doublePredicate(
818 final DoublePredicate doublePredicate) {
819 return doublePredicate(doublePredicate, CacheBuilder.newBuilder().build());
823 * <p>
824 * Memoizes a {@link DoublePredicate} in a Guava {@link Cache}.
825 * </p>
826 * <h3>Features</h3>
827 * <ul>
828 * <li>Custom cache</li>
829 * <li>Default cache key</li>
830 * </ul>
832 * @param doublePredicate
833 * The {@link DoublePredicate} to memoize.
834 * @param cache
835 * The {@link Cache} to use.
836 * @return The wrapped {@link DoublePredicate}.
838 public static final DoublePredicate doublePredicate(
839 final DoublePredicate doublePredicate,
840 final Cache<Double, Boolean> cache) {
841 return doublePredicate(doublePredicate, Double::valueOf, cache);
845 * <p>
846 * Memoizes a {@link DoublePredicate} in a Guava {@link Cache}.
847 * </p>
848 * <h3>Features</h3>
849 * <ul>
850 * <li>Default cache</li>
851 * <li>Custom cache key</li>
852 * </ul>
854 * @param doublePredicate
855 * The {@link DoublePredicate} to memoize.
856 * @param keyFunction
857 * The {@link DoubleFunction} to compute the cache key.
858 * @return The wrapped {@link DoublePredicate}.
860 public static final <KEY> DoublePredicate doublePredicate(
861 final DoublePredicate doublePredicate,
862 final DoubleFunction<KEY> keyFunction) {
863 return doublePredicate(doublePredicate, keyFunction, CacheBuilder.newBuilder().build());
867 * <p>
868 * Memoizes a {@link DoublePredicate} in a Guava {@link Cache}.
869 * </p>
870 * <h3>Features</h3>
871 * <ul>
872 * <li>Custom cache</li>
873 * <li>Custom cache key</li>
874 * </ul>
876 * @param doublePredicate
877 * The {@link DoublePredicate} to memoize.
878 * @param keyFunction
879 * The {@link DoubleFunction} to compute the cache key.
880 * @param cache
881 * The {@link Cache} to use.
882 * @return The wrapped {@link DoublePredicate}.
884 public static final <KEY> DoublePredicate doublePredicate(
885 final DoublePredicate doublePredicate,
886 final DoubleFunction<KEY> keyFunction,
887 final Cache<KEY, Boolean> cache) {
888 return new GuavaCacheBasedDoublePredicateMemoizer<>(cache, keyFunction, doublePredicate);
892 * <p>
893 * Memoizes a {@link DoubleSupplier} in a Guava {@link Cache}.
894 * </p>
895 * <h3>Features</h3>
896 * <ul>
897 * <li>Default cache</li>
898 * <li>Default cache key</li>
899 * </ul>
901 * @param doubleSupplier
902 * The {@link DoubleSupplier} to memoize.
903 * @return The wrapped {@link DoubleSupplier}.
905 public static final DoubleSupplier doubleSupplier(final DoubleSupplier doubleSupplier) {
906 return doubleSupplier(doubleSupplier, CacheBuilder.newBuilder().build());
910 * <p>
911 * Memoizes a {@link DoubleSupplier} in a Guava {@link Cache}.
912 * </p>
913 * <h3>Features</h3>
914 * <ul>
915 * <li>Custom cache</li>
916 * <li>Default cache key</li>
917 * </ul>
919 * @param doubleSupplier
920 * The {@link DoubleSupplier} to memoize.
921 * @param cache
922 * The {@link Cache} to use.
923 * @return The wrapped {@link DoubleSupplier}.
925 public static final DoubleSupplier doubleSupplier(
926 final DoubleSupplier doubleSupplier,
927 final Cache<String, Double> cache) {
928 return doubleSupplier(doubleSupplier, defaultKeySupplier(), cache);
932 * <p>
933 * Memoizes a {@link DoubleSupplier} in a Guava {@link Cache}.
934 * </p>
935 * <h3>Features</h3>
936 * <ul>
937 * <li>Default cache</li>
938 * <li>Custom cache key</li>
939 * </ul>
941 * @param doubleSupplier
942 * The {@link DoubleSupplier} to memoize.
943 * @param keySupplier
944 * The {@link Supplier} for the cache key.
945 * @return The wrapped {@link DoubleSupplier}.
947 public static final <KEY> DoubleSupplier doubleSupplier(
948 final DoubleSupplier doubleSupplier,
949 final Supplier<KEY> keySupplier) {
950 return doubleSupplier(doubleSupplier, keySupplier, CacheBuilder.newBuilder().build());
954 * <p>
955 * Memoizes a {@link DoubleSupplier} in a Guava {@link Cache}.
956 * </p>
957 * <h3>Features</h3>
958 * <ul>
959 * <li>Custom cache</li>
960 * <li>Custom cache key</li>
961 * </ul>
963 * @param doubleSupplier
964 * The {@link DoubleSupplier} to memoize.
965 * @param keySupplier
966 * The {@link Supplier} for the cache key.
967 * @param cache
968 * The {@link Cache} to use.
969 * @return The wrapped {@link DoubleSupplier}.
971 public static final <KEY> DoubleSupplier doubleSupplier(
972 final DoubleSupplier doubleSupplier,
973 final Supplier<KEY> keySupplier,
974 final Cache<KEY, Double> cache) {
975 return new GuavaCacheBasedDoubleSupplierMemoizer<>(cache, keySupplier, doubleSupplier);
979 * <p>
980 * Memoizes a {@link Function} in a Guava {@link Cache}.
981 * </p>
982 * <h3>Features</h3>
983 * <ul>
984 * <li>Default cache</li>
985 * <li>Default cache key</li>
986 * </ul>
988 * @param function
989 * The {@link Function} to memoize.
990 * @return The wrapped {@link Function}.
992 public static final <INPUT, OUTPUT> Function<INPUT, OUTPUT> function(
993 final Function<INPUT, OUTPUT> function) {
994 return function(function, CacheBuilder.newBuilder().build());
998 * <p>
999 * Memoizes a {@link Function} in a Guava {@link Cache}.
1000 * </p>
1001 * <h3>Features</h3>
1002 * <ul>
1003 * <li>Custom cache</li>
1004 * <li>Default cache key</li>
1005 * </ul>
1007 * @param function
1008 * The {@link Function} to memoize.
1009 * @param cache
1010 * The {@link Cache} to use.
1011 * @return The wrapped {@link Function}.
1013 public static final <INPUT, OUTPUT> Function<INPUT, OUTPUT> function(
1014 final Function<INPUT, OUTPUT> function,
1015 final Cache<INPUT, OUTPUT> cache) {
1016 return function(function, Function.identity(), cache);
1020 * <p>
1021 * Memoizes a {@link Function} in a Guava {@link Cache}.
1022 * </p>
1023 * <h3>Features</h3>
1024 * <ul>
1025 * <li>Default cache</li>
1026 * <li>Custom cache key</li>
1027 * </ul>
1029 * @param function
1030 * The {@link Function} to memoize.
1031 * @param keyFunction
1032 * The {@link Function} to compute the cache key.
1033 * @return The wrapped {@link Function}.
1035 public static final <INPUT, KEY, OUTPUT> Function<INPUT, OUTPUT> function(
1036 final Function<INPUT, OUTPUT> function,
1037 final Function<INPUT, KEY> keyFunction) {
1038 return function(function, keyFunction, CacheBuilder.newBuilder().build());
1042 * <p>
1043 * Memoizes a {@link Function} in a Guava {@link Cache}.
1044 * </p>
1045 * <h3>Features</h3>
1046 * <ul>
1047 * <li>Custom cache</li>
1048 * <li>Custom cache key</li>
1049 * </ul>
1051 * @param function
1052 * The {@link Function} to memoize.
1053 * @param keyFunction
1054 * The {@link Function} to compute the cache key.
1055 * @param cache
1056 * The {@link Cache} to use.
1057 * @return The wrapped {@link Function}.
1059 public static final <INPUT, KEY, OUTPUT> Function<INPUT, OUTPUT> function(
1060 final Function<INPUT, OUTPUT> function,
1061 final Function<INPUT, KEY> keyFunction,
1062 final Cache<KEY, OUTPUT> cache) {
1063 return new GuavaCacheBasedFunctionMemoizer<>(cache, keyFunction, function);
1067 * <p>
1068 * Memoizes a {@link IntBinaryOperator} in a Guava {@link Cache}.
1069 * </p>
1070 * <h3>Features</h3>
1071 * <ul>
1072 * <li>Default cache</li>
1073 * <li>Default cache key</li>
1074 * </ul>
1076 * @param intBinaryOperator
1077 * The {@link IntBinaryOperator} to memoize.
1078 * @return The wrapped {@link IntBinaryOperator}.
1080 public static final IntBinaryOperator intBinaryOperator(
1081 final IntBinaryOperator intBinaryOperator) {
1082 return intBinaryOperator(intBinaryOperator, CacheBuilder.newBuilder().build());
1086 * <p>
1087 * Memoizes a {@link IntBinaryOperator} in a Guava {@link Cache}.
1088 * </p>
1089 * <h3>Features</h3>
1090 * <ul>
1091 * <li>Default cache</li>
1092 * <li>Custom cache key</li>
1093 * </ul>
1095 * @param intBinaryOperator
1096 * The {@link IntBinaryOperator} to memoize.
1097 * @param keyFunction
1098 * The {@link IntBinaryFunction} to compute the cache key.
1099 * @return The wrapped {@link IntBinaryOperator}.
1101 public static final <KEY> IntBinaryOperator intBinaryOperator(
1102 final IntBinaryOperator intBinaryOperator,
1103 final IntBinaryFunction<KEY> keyFunction) {
1104 return intBinaryOperator(intBinaryOperator, keyFunction, CacheBuilder.newBuilder().build());
1108 * <p>
1109 * Memoizes a {@link IntBinaryOperator} in a Guava {@link Cache}.
1110 * </p>
1111 * <h3>Features</h3>
1112 * <ul>
1113 * <li>Custom cache</li>
1114 * <li>Default cache key</li>
1115 * </ul>
1117 * @param intBinaryOperator
1118 * The {@link IntBinaryOperator} to memoize.
1119 * @param cache
1120 * The {@link Cache} to use.
1121 * @return The wrapped {@link IntBinaryOperator}.
1123 public static final IntBinaryOperator intBinaryOperator(
1124 final IntBinaryOperator intBinaryOperator,
1125 final Cache<String, Integer> cache) {
1126 return intBinaryOperator(intBinaryOperator, intBinaryOperatorHashCodeKeyFunction(), cache);
1130 * <p>
1131 * Memoizes a {@link IntBinaryOperator} in a Guava {@link Cache}.
1132 * </p>
1133 * <h3>Features</h3>
1134 * <ul>
1135 * <li>Custom cache</li>
1136 * <li>Custom cache key</li>
1137 * </ul>
1139 * @param intBinaryOperator
1140 * The {@link IntBinaryOperator} to memoize.
1141 * @param keyFunction
1142 * The {@link IntBinaryFunction} to compute the cache key.
1143 * @param cache
1144 * The {@link Cache} to use.
1145 * @return The wrapped {@link IntBinaryOperator}.
1147 public static final <KEY> IntBinaryOperator intBinaryOperator(
1148 final IntBinaryOperator intBinaryOperator,
1149 final IntBinaryFunction<KEY> keyFunction,
1150 final Cache<KEY, Integer> cache) {
1151 return new GuavaCacheBasedIntBinaryOperatorMemoizer<>(cache, keyFunction, intBinaryOperator);
1155 * <p>
1156 * Memoizes a {@link IntConsumer} in a Guava {@link Cache}.
1157 * </p>
1158 * <h3>Features</h3>
1159 * <ul>
1160 * <li>Default cache</li>
1161 * <li>Default cache key</li>
1162 * </ul>
1164 * @param intConsumer
1165 * The {@link IntConsumer} to memoize.
1166 * @return The wrapped {@link IntConsumer}.
1168 public static final IntConsumer intConsumer(
1169 final IntConsumer intConsumer) {
1170 return intConsumer(intConsumer, CacheBuilder.newBuilder().build());
1174 * <p>
1175 * Memoizes a {@link IntConsumer} in a Guava {@link Cache}.
1176 * </p>
1177 * <h3>Features</h3>
1178 * <ul>
1179 * <li>Custom cache</li>
1180 * <li>Default cache key</li>
1181 * </ul>
1183 * @param intConsumer
1184 * The {@link IntConsumer} to memoize.
1185 * @param cache
1186 * The {@link Cache} to use.
1187 * @return The wrapped {@link IntConsumer}.
1189 public static final IntConsumer intConsumer(
1190 final IntConsumer intConsumer,
1191 final Cache<Integer, Integer> cache) {
1192 return intConsumer(intConsumer, Integer::valueOf, cache);
1196 * <p>
1197 * Memoizes a {@link IntConsumer} in a Guava {@link Cache}.
1198 * </p>
1199 * <h3>Features</h3>
1200 * <ul>
1201 * <li>Default cache</li>
1202 * <li>Custom cache key</li>
1203 * </ul>
1205 * @param intConsumer
1206 * The {@link IntConsumer} to memoize.
1207 * @param keyFunction
1208 * The {@link DoubleFunction} to compute the cache key.
1209 * @return The wrapped {@link IntConsumer}.
1211 public static final <KEY> IntConsumer intConsumer(
1212 final IntConsumer intConsumer,
1213 final IntFunction<KEY> keyFunction) {
1214 return intConsumer(intConsumer, keyFunction, CacheBuilder.newBuilder().build());
1218 * <p>
1219 * Memoizes a {@link IntConsumer} in a Guava {@link Cache}.
1220 * </p>
1221 * <h3>Features</h3>
1222 * <ul>
1223 * <li>Custom cache</li>
1224 * <li>Custom cache key</li>
1225 * </ul>
1227 * @param intConsumer
1228 * The {@link IntConsumer} to memoize.
1229 * @param keyFunction
1230 * The {@link Function} to compute the cache key.
1231 * @param cache
1232 * The {@link Cache} to use.
1233 * @return The wrapped {@link IntConsumer}.
1235 public static final <KEY> IntConsumer intConsumer(
1236 final IntConsumer intConsumer,
1237 final IntFunction<KEY> keyFunction,
1238 final Cache<KEY, Integer> cache) {
1239 return new GuavaCacheBasedIntConsumerMemoizer<>(cache, keyFunction, intConsumer);
1243 * <p>
1244 * Memoizes a {@link IntFunction} in a Guava {@link Cache}.
1245 * </p>
1246 * <h3>Features</h3>
1247 * <ul>
1248 * <li>Default cache</li>
1249 * <li>Default cache key</li>
1250 * </ul>
1252 * @param function
1253 * The {@link IntFunction} to memoize.
1254 * @return The wrapped {@link IntFunction}.
1256 public static final <OUTPUT> IntFunction<OUTPUT> intFunction(
1257 final IntFunction<OUTPUT> function) {
1258 return intFunction(function, CacheBuilder.newBuilder().build());
1262 * <p>
1263 * Memoizes a {@link IntFunction} in a Guava {@link Cache}.
1264 * </p>
1265 * <h3>Features</h3>
1266 * <ul>
1267 * <li>Custom cache</li>
1268 * <li>Default cache key</li>
1269 * </ul>
1271 * @param function
1272 * The {@link IntFunction} to memoize.
1273 * @param cache
1274 * The {@link Cache} to use.
1275 * @return The wrapped {@link IntFunction}.
1277 public static final <OUTPUT> IntFunction<OUTPUT> intFunction(
1278 final IntFunction<OUTPUT> function,
1279 final Cache<Integer, OUTPUT> cache) {
1280 return intFunction(function, Integer::valueOf, cache);
1284 * <p>
1285 * Memoizes a {@link IntFunction} in a Guava {@link Cache}.
1286 * </p>
1287 * <h3>Features</h3>
1288 * <ul>
1289 * <li>Default cache</li>
1290 * <li>Custom cache key</li>
1291 * </ul>
1293 * @param function
1294 * The {@link IntFunction} to memoize.
1295 * @param keyFunction
1296 * The {@link IntFunction} to compute the cache key.
1297 * @return The wrapped {@link IntFunction}.
1299 public static final <KEY, OUTPUT> IntFunction<OUTPUT> intFunction(
1300 final IntFunction<OUTPUT> function,
1301 final IntFunction<KEY> keyFunction) {
1302 return intFunction(function, keyFunction, CacheBuilder.newBuilder().build());
1306 * <p>
1307 * Memoizes a {@link IntFunction} in a Guava {@link Cache}.
1308 * </p>
1309 * <h3>Features</h3>
1310 * <ul>
1311 * <li>Custom cache</li>
1312 * <li>Custom cache key</li>
1313 * </ul>
1315 * @param function
1316 * The {@link IntFunction} to memoize.
1317 * @param keyFunction
1318 * The {@link IntFunction} to compute the cache key.
1319 * @param cache
1320 * The {@link Cache} to use.
1321 * @return The wrapped {@link IntFunction}.
1323 public static final <KEY, OUTPUT> IntFunction<OUTPUT> intFunction(
1324 final IntFunction<OUTPUT> function,
1325 final IntFunction<KEY> keyFunction,
1326 final Cache<KEY, OUTPUT> cache) {
1327 return new GuavaCacheBasedIntFunctionMemoizer<>(cache, keyFunction, function);
1331 * <p>
1332 * Memoizes a {@link IntPredicate} in a Guava {@link Cache}.
1333 * </p>
1334 * <h3>Features</h3>
1335 * <ul>
1336 * <li>Default cache</li>
1337 * <li>Default cache key</li>
1338 * </ul>
1340 * @param intPredicate
1341 * The {@link IntPredicate} to memoize.
1342 * @return The wrapped {@link IntPredicate}.
1344 public static final IntPredicate intPredicate(
1345 final IntPredicate intPredicate) {
1346 return intPredicate(intPredicate, CacheBuilder.newBuilder().build());
1350 * <p>
1351 * Memoizes a {@link IntPredicate} in a Guava {@link Cache}.
1352 * </p>
1353 * <h3>Features</h3>
1354 * <ul>
1355 * <li>Custom cache</li>
1356 * <li>Default cache key</li>
1357 * </ul>
1359 * @param intPredicate
1360 * The {@link IntPredicate} to memoize.
1361 * @param cache
1362 * The {@link Cache} to use.
1363 * @return The wrapped {@link IntPredicate}.
1365 public static final IntPredicate intPredicate(
1366 final IntPredicate intPredicate,
1367 final Cache<Integer, Boolean> cache) {
1368 return intPredicate(intPredicate, Integer::valueOf, cache);
1372 * <p>
1373 * Memoizes a {@link IntPredicate} in a Guava {@link Cache}.
1374 * </p>
1375 * <h3>Features</h3>
1376 * <ul>
1377 * <li>Default cache</li>
1378 * <li>Custom cache key</li>
1379 * </ul>
1381 * @param intPredicate
1382 * The {@link IntPredicate} to memoize.
1383 * @param keyFunction
1384 * The {@link IntFunction} to compute the cache key.
1385 * @return The wrapped {@link IntPredicate}.
1387 public static final <KEY> IntPredicate intPredicate(
1388 final IntPredicate intPredicate,
1389 final IntFunction<KEY> keyFunction) {
1390 return intPredicate(intPredicate, keyFunction, CacheBuilder.newBuilder().build());
1394 * <p>
1395 * Memoizes a {@link IntPredicate} in a Guava {@link Cache}.
1396 * </p>
1397 * <h3>Features</h3>
1398 * <ul>
1399 * <li>Custom cache</li>
1400 * <li>Custom cache key</li>
1401 * </ul>
1403 * @param intPredicate
1404 * The {@link IntPredicate} to memoize.
1405 * @param keyFunction
1406 * The {@link IntFunction} to compute the cache key.
1407 * @param cache
1408 * The {@link Cache} to use.
1409 * @return The wrapped {@link IntPredicate}.
1411 public static final <KEY> IntPredicate intPredicate(
1412 final IntPredicate intPredicate,
1413 final IntFunction<KEY> keyFunction,
1414 final Cache<KEY, Boolean> cache) {
1415 return new GuavaCacheBasedIntPredicateMemoizer<>(cache, keyFunction, intPredicate);
1419 * <p>
1420 * Memoizes a {@link IntSupplier} in a Guava {@link Cache}.
1421 * </p>
1422 * <h3>Features</h3>
1423 * <ul>
1424 * <li>Default cache</li>
1425 * <li>Default cache key</li>
1426 * </ul>
1428 * @param intSupplier
1429 * The {@link IntSupplier} to memoize.
1430 * @return The wrapped {@link IntSupplier}.
1432 public static final IntSupplier intSupplier(final IntSupplier intSupplier) {
1433 return intSupplier(intSupplier, CacheBuilder.newBuilder().build());
1437 * <p>
1438 * Memoizes a {@link IntSupplier} in a Guava {@link Cache}.
1439 * </p>
1440 * <h3>Features</h3>
1441 * <ul>
1442 * <li>Custom cache</li>
1443 * <li>Default cache key</li>
1444 * </ul>
1446 * @param intSupplier
1447 * The {@link IntSupplier} to memoize.
1448 * @param cache
1449 * The {@link Cache} to use.
1450 * @return The wrapped {@link IntSupplier}.
1452 public static final IntSupplier intSupplier(
1453 final IntSupplier intSupplier,
1454 final Cache<String, Integer> cache) {
1455 return intSupplier(intSupplier, defaultKeySupplier(), cache);
1459 * <p>
1460 * Memoizes a {@link IntSupplier} in a Guava {@link Cache}.
1461 * </p>
1462 * <h3>Features</h3>
1463 * <ul>
1464 * <li>Default cache</li>
1465 * <li>Custom cache key</li>
1466 * </ul>
1468 * @param intSupplier
1469 * The {@link IntSupplier} to memoize.
1470 * @param keySupplier
1471 * The {@link Supplier} for the cache key.
1472 * @return The wrapped {@link IntSupplier}.
1474 public static final <KEY> IntSupplier intSupplier(
1475 final IntSupplier intSupplier,
1476 final Supplier<KEY> keySupplier) {
1477 return intSupplier(intSupplier, keySupplier, CacheBuilder.newBuilder().build());
1481 * <p>
1482 * Memoizes a {@link IntSupplier} in a Guava {@link Cache}.
1483 * </p>
1484 * <h3>Features</h3>
1485 * <ul>
1486 * <li>Custom cache</li>
1487 * <li>Custom cache key</li>
1488 * </ul>
1490 * @param intSupplier
1491 * The {@link IntSupplier} to memoize.
1492 * @param keySupplier
1493 * The {@link Supplier} for the cache key.
1494 * @param cache
1495 * The {@link Cache} to use.
1496 * @return The wrapped {@link IntSupplier}.
1498 public static final <KEY> IntSupplier intSupplier(
1499 final IntSupplier intSupplier,
1500 final Supplier<KEY> keySupplier,
1501 final Cache<KEY, Integer> cache) {
1502 return new GuavaCacheBasedIntSupplierMemoizer<>(cache, keySupplier, intSupplier);
1506 * <p>
1507 * Memoizes a {@link LongConsumer} in a Guava {@link Cache}.
1508 * </p>
1509 * <h3>Features</h3>
1510 * <ul>
1511 * <li>Default cache</li>
1512 * <li>Default cache key</li>
1513 * </ul>
1515 * @param longConsumer
1516 * The {@link LongConsumer} to memoize.
1517 * @return The wrapped {@link LongConsumer}.
1519 public static final LongConsumer longConsumer(
1520 final LongConsumer longConsumer) {
1521 return longConsumer(longConsumer, CacheBuilder.newBuilder().build());
1525 * <p>
1526 * Memoizes a {@link LongConsumer} in a Guava {@link Cache}.
1527 * </p>
1528 * <h3>Features</h3>
1529 * <ul>
1530 * <li>Custom cache</li>
1531 * <li>Default cache key</li>
1532 * </ul>
1534 * @param longConsumer
1535 * The {@link LongConsumer} to memoize.
1536 * @param cache
1537 * The {@link Cache} to use.
1538 * @return The wrapped {@link LongConsumer}.
1540 public static final LongConsumer longConsumer(
1541 final LongConsumer longConsumer,
1542 final Cache<Long, Long> cache) {
1543 return longConsumer(longConsumer, Long::valueOf, cache);
1547 * <p>
1548 * Memoizes a {@link LongConsumer} in a Guava {@link Cache}.
1549 * </p>
1550 * <h3>Features</h3>
1551 * <ul>
1552 * <li>Default cache</li>
1553 * <li>Custom cache key</li>
1554 * </ul>
1556 * @param longConsumer
1557 * The {@link LongConsumer} to memoize.
1558 * @param keyFunction
1559 * The {@link DoubleFunction} to compute the cache key.
1560 * @return The wrapped {@link LongConsumer}.
1562 public static final <KEY> LongConsumer longConsumer(
1563 final LongConsumer longConsumer,
1564 final LongFunction<KEY> keyFunction) {
1565 return longConsumer(longConsumer, keyFunction, CacheBuilder.newBuilder().build());
1569 * <p>
1570 * Memoizes a {@link LongConsumer} in a Guava {@link Cache}.
1571 * </p>
1572 * <h3>Features</h3>
1573 * <ul>
1574 * <li>Custom cache</li>
1575 * <li>Custom cache key</li>
1576 * </ul>
1578 * @param longConsumer
1579 * The {@link LongConsumer} to memoize.
1580 * @param keyFunction
1581 * The {@link Function} to compute the cache key.
1582 * @param cache
1583 * The {@link Cache} to use.
1584 * @return The wrapped {@link LongConsumer}.
1586 public static final <KEY> LongConsumer longConsumer(
1587 final LongConsumer longConsumer,
1588 final LongFunction<KEY> keyFunction,
1589 final Cache<KEY, Long> cache) {
1590 return new GuavaCacheBasedLongConsumerMemoizer<>(cache, keyFunction, longConsumer);
1594 * <p>
1595 * Memoizes a {@link LongFunction} in a Guava {@link Cache}.
1596 * </p>
1597 * <h3>Features</h3>
1598 * <ul>
1599 * <li>Default cache</li>
1600 * <li>Default cache key</li>
1601 * </ul>
1603 * @param function
1604 * The {@link LongFunction} to memoize.
1605 * @return The wrapped {@link LongFunction}.
1607 public static final <OUTPUT> LongFunction<OUTPUT> longFunction(
1608 final LongFunction<OUTPUT> function) {
1609 return longFunction(function, CacheBuilder.newBuilder().build());
1613 * <p>
1614 * Memoizes a {@link LongFunction} in a Guava {@link Cache}.
1615 * </p>
1616 * <h3>Features</h3>
1617 * <ul>
1618 * <li>Custom cache</li>
1619 * <li>Default cache key</li>
1620 * </ul>
1622 * @param function
1623 * The {@link LongFunction} to memoize.
1624 * @param cache
1625 * The {@link Cache} to use.
1626 * @return The wrapped {@link LongFunction}.
1628 public static final <OUTPUT> LongFunction<OUTPUT> longFunction(
1629 final LongFunction<OUTPUT> function,
1630 final Cache<Long, OUTPUT> cache) {
1631 return longFunction(function, Long::valueOf, cache);
1635 * <p>
1636 * Memoizes a {@link LongFunction} in a Guava {@link Cache}.
1637 * </p>
1638 * <h3>Features</h3>
1639 * <ul>
1640 * <li>Default cache</li>
1641 * <li>Custom cache key</li>
1642 * </ul>
1644 * @param function
1645 * The {@link LongFunction} to memoize.
1646 * @param keyFunction
1647 * The {@link LongFunction} to compute the cache key.
1648 * @return The wrapped {@link LongFunction}.
1650 public static final <KEY, OUTPUT> LongFunction<OUTPUT> longFunction(
1651 final LongFunction<OUTPUT> function,
1652 final LongFunction<KEY> keyFunction) {
1653 return longFunction(function, keyFunction, CacheBuilder.newBuilder().build());
1657 * <p>
1658 * Memoizes a {@link LongFunction} in a Guava {@link Cache}.
1659 * </p>
1660 * <h3>Features</h3>
1661 * <ul>
1662 * <li>Custom cache</li>
1663 * <li>Custom cache key</li>
1664 * </ul>
1666 * @param function
1667 * The {@link LongFunction} to memoize.
1668 * @param keyFunction
1669 * The {@link LongFunction} to compute the cache key.
1670 * @param cache
1671 * The {@link Cache} to use.
1672 * @return The wrapped {@link LongFunction}.
1674 public static final <KEY, OUTPUT> LongFunction<OUTPUT> longFunction(
1675 final LongFunction<OUTPUT> function,
1676 final LongFunction<KEY> keyFunction,
1677 final Cache<KEY, OUTPUT> cache) {
1678 return new GuavaCacheBasedLongFunctionMemoizer<>(cache, keyFunction, function);
1682 * <p>
1683 * Memoizes a {@link LongPredicate} in a Guava {@link Cache}.
1684 * </p>
1685 * <h3>Features</h3>
1686 * <ul>
1687 * <li>Default cache</li>
1688 * <li>Default cache key</li>
1689 * </ul>
1691 * @param longPredicate
1692 * The {@link LongPredicate} to memoize.
1693 * @return The wrapped {@link LongPredicate}.
1695 public static final LongPredicate longPredicate(
1696 final LongPredicate longPredicate) {
1697 return longPredicate(longPredicate, CacheBuilder.newBuilder().build());
1701 * <p>
1702 * Memoizes a {@link LongPredicate} in a Guava {@link Cache}.
1703 * </p>
1704 * <h3>Features</h3>
1705 * <ul>
1706 * <li>Custom cache</li>
1707 * <li>Default cache key</li>
1708 * </ul>
1710 * @param longPredicate
1711 * The {@link LongPredicate} to memoize.
1712 * @param cache
1713 * The {@link Cache} to use.
1714 * @return The wrapped {@link LongPredicate}.
1716 public static final LongPredicate longPredicate(
1717 final LongPredicate longPredicate,
1718 final Cache<Long, Boolean> cache) {
1719 return longPredicate(longPredicate, Long::valueOf, cache);
1723 * <p>
1724 * Memoizes a {@link LongPredicate} in a Guava {@link Cache}.
1725 * </p>
1726 * <h3>Features</h3>
1727 * <ul>
1728 * <li>Default cache</li>
1729 * <li>Custom cache key</li>
1730 * </ul>
1732 * @param longPredicate
1733 * The {@link LongPredicate} to memoize.
1734 * @param keyFunction
1735 * The {@link LongFunction} to compute the cache key.
1736 * @return The wrapped {@link LongPredicate}.
1738 public static final <KEY> LongPredicate longPredicate(
1739 final LongPredicate longPredicate,
1740 final LongFunction<KEY> keyFunction) {
1741 return longPredicate(longPredicate, keyFunction, CacheBuilder.newBuilder().build());
1745 * <p>
1746 * Memoizes a {@link LongPredicate} in a Guava {@link Cache}.
1747 * </p>
1748 * <h3>Features</h3>
1749 * <ul>
1750 * <li>Custom cache</li>
1751 * <li>Custom cache key</li>
1752 * </ul>
1754 * @param longPredicate
1755 * The {@link LongPredicate} to memoize.
1756 * @param keyFunction
1757 * The {@link LongFunction} to compute the cache key.
1758 * @param cache
1759 * The {@link Cache} to use.
1760 * @return The wrapped {@link LongPredicate}.
1762 public static final <KEY> LongPredicate longPredicate(
1763 final LongPredicate longPredicate,
1764 final LongFunction<KEY> keyFunction,
1765 final Cache<KEY, Boolean> cache) {
1766 return new GuavaCacheBasedLongPredicateMemoizer<>(cache, keyFunction, longPredicate);
1770 * <p>
1771 * Memoizes a {@link LongSupplier} in a Guava {@link Cache}.
1772 * </p>
1773 * <h3>Features</h3>
1774 * <ul>
1775 * <li>Default cache</li>
1776 * <li>Default cache key</li>
1777 * </ul>
1779 * @param longSupplier
1780 * The {@link LongSupplier} to memoize.
1781 * @return The wrapped {@link LongSupplier}.
1783 public static final LongSupplier longSupplier(final LongSupplier longSupplier) {
1784 return longSupplier(longSupplier, CacheBuilder.newBuilder().build());
1788 * <p>
1789 * Memoizes a {@link LongSupplier} in a Guava {@link Cache}.
1790 * </p>
1791 * <h3>Features</h3>
1792 * <ul>
1793 * <li>Custom cache</li>
1794 * <li>Default cache key</li>
1795 * </ul>
1797 * @param longSupplier
1798 * The {@link LongSupplier} to memoize.
1799 * @param cache
1800 * The {@link Cache} to use.
1801 * @return The wrapped {@link LongSupplier}.
1803 public static final LongSupplier longSupplier(
1804 final LongSupplier longSupplier,
1805 final Cache<String, Long> cache) {
1806 return longSupplier(longSupplier, defaultKeySupplier(), cache);
1810 * <p>
1811 * Memoizes a {@link LongSupplier} in a Guava {@link Cache}.
1812 * </p>
1813 * <h3>Features</h3>
1814 * <ul>
1815 * <li>Default cache</li>
1816 * <li>Custom cache key</li>
1817 * </ul>
1819 * @param longSupplier
1820 * The {@link LongSupplier} to memoize.
1821 * @param keySupplier
1822 * The {@link Supplier} for the cache key.
1823 * @return The wrapped {@link LongSupplier}.
1825 public static final <KEY> LongSupplier longSupplier(
1826 final LongSupplier longSupplier,
1827 final Supplier<KEY> keySupplier) {
1828 return longSupplier(longSupplier, keySupplier, CacheBuilder.newBuilder().build());
1832 * <p>
1833 * Memoizes a {@link LongSupplier} in a Guava {@link Cache}.
1834 * </p>
1835 * <h3>Features</h3>
1836 * <ul>
1837 * <li>Custom cache</li>
1838 * <li>Custom cache key</li>
1839 * </ul>
1841 * @param longSupplier
1842 * The {@link LongSupplier} to memoize.
1843 * @param keySupplier
1844 * The {@link Supplier} for the cache key.
1845 * @param cache
1846 * The {@link Cache} to use.
1847 * @return The wrapped {@link LongSupplier}.
1849 public static final <KEY> LongSupplier longSupplier(
1850 final LongSupplier longSupplier,
1851 final Supplier<KEY> keySupplier,
1852 final Cache<KEY, Long> cache) {
1853 return new GuavaCacheBasedLongSupplierMemoizer<>(cache, keySupplier, longSupplier);
1857 * <p>
1858 * Memoizes a {@link Predicate} in a Guava {@link Cache}.
1859 * </p>
1860 * <h3>Features</h3>
1861 * <ul>
1862 * <li>Default cache</li>
1863 * <li>Default cache key</li>
1864 * </ul>
1866 * @param predicate
1867 * The {@link Predicate} to memoize.
1868 * @return The wrapped {@link Predicate}.
1870 public static final <INPUT> Predicate<INPUT> predicate(
1871 final Predicate<INPUT> predicate) {
1872 return predicate(predicate, CacheBuilder.newBuilder().build());
1876 * <p>
1877 * Memoizes a {@link Predicate} in a Guava {@link Cache}.
1878 * </p>
1879 * <h3>Features</h3>
1880 * <ul>
1881 * <li>Custom cache</li>
1882 * <li>Default cache key</li>
1883 * </ul>
1885 * @param predicate
1886 * The {@link Predicate} to memoize.
1887 * @param cache
1888 * The {@link Cache} to use.
1889 * @return The wrapped {@link Predicate}.
1891 public static final <INPUT> Predicate<INPUT> predicate(
1892 final Predicate<INPUT> predicate,
1893 final Cache<INPUT, Boolean> cache) {
1894 return predicate(predicate, identity(), cache);
1898 * <p>
1899 * Memoizes a {@link Predicate} in a Guava {@link Cache}.
1900 * </p>
1901 * <h3>Features</h3>
1902 * <ul>
1903 * <li>Default cache</li>
1904 * <li>Custom cache key</li>
1905 * </ul>
1907 * @param predicate
1908 * The {@link Predicate} to memoize.
1909 * @param keyFunction
1910 * The {@link Function} to compute the cache key.
1911 * @return The wrapped {@link Predicate}.
1913 public static final <INPUT, KEY> Predicate<INPUT> predicate(
1914 final Predicate<INPUT> predicate,
1915 final Function<INPUT, KEY> keyFunction) {
1916 return predicate(predicate, keyFunction, CacheBuilder.newBuilder().build());
1920 * <p>
1921 * Memoizes a {@link Predicate} in a Guava {@link Cache}.
1922 * </p>
1923 * <h3>Features</h3>
1924 * <ul>
1925 * <li>Custom cache</li>
1926 * <li>Custom cache key</li>
1927 * </ul>
1929 * @param predicate
1930 * The {@link Predicate} to memoize.
1931 * @param keyFunction
1932 * The {@link Function} to compute the cache key.
1933 * @param cache
1934 * The {@link Cache} to use.
1935 * @return The wrapped {@link Predicate}.
1937 public static final <INPUT, KEY> Predicate<INPUT> predicate(
1938 final Predicate<INPUT> predicate,
1939 final Function<INPUT, KEY> keyFunction,
1940 final Cache<KEY, Boolean> cache) {
1941 return new GuavaCacheBasedPredicateMemoizer<>(cache, keyFunction, predicate);
1945 * <p>
1946 * Memoizes a {@link Supplier} in a Guava {@link Cache}.
1947 * </p>
1948 * <h3>Features</h3>
1949 * <ul>
1950 * <li>Default cache</li>
1951 * <li>Default cache key</li>
1952 * </ul>
1954 * @param supplier
1955 * The {@link Supplier} to memoize.
1956 * @return The wrapped {@link Supplier}.
1958 public static final <OUTPUT> Supplier<OUTPUT> supplier(final Supplier<OUTPUT> supplier) {
1959 return supplier(supplier, CacheBuilder.newBuilder().build());
1963 * <p>
1964 * Memoizes a {@link Supplier} in a Guava {@link Cache}.
1965 * </p>
1966 * <h3>Features</h3>
1967 * <ul>
1968 * <li>Custom cache</li>
1969 * <li>Default cache key</li>
1970 * </ul>
1972 * @param supplier
1973 * The {@link Supplier} to memoize.
1974 * @param cache
1975 * The {@link Cache} to use.
1976 * @return The wrapped {@link Supplier}.
1978 public static final <OUTPUT> Supplier<OUTPUT> supplier(
1979 final Supplier<OUTPUT> supplier,
1980 final Cache<String, OUTPUT> cache) {
1981 return supplier(supplier, defaultKeySupplier(), cache);
1985 * <p>
1986 * Memoizes a {@link Supplier} in a Guava {@link Cache}.
1987 * </p>
1988 * <h3>Features</h3>
1989 * <ul>
1990 * <li>Default cache</li>
1991 * <li>Custom cache key</li>
1992 * </ul>
1994 * @param supplier
1995 * The {@link Supplier} to memoize.
1996 * @param keySupplier
1997 * The {@link Supplier} for the cache key.
1998 * @return The wrapped {@link Supplier}.
2000 public static final <KEY, OUTPUT> Supplier<OUTPUT> supplier(
2001 final Supplier<OUTPUT> supplier,
2002 final Supplier<KEY> keySupplier) {
2003 return supplier(supplier, keySupplier, CacheBuilder.newBuilder().build());
2007 * <p>
2008 * Memoizes a {@link Supplier} in a Guava {@link Cache}.
2009 * </p>
2010 * <h3>Features</h3>
2011 * <ul>
2012 * <li>Custom cache</li>
2013 * <li>Custom cache key</li>
2014 * </ul>
2016 * @param supplier
2017 * The {@link Supplier} to memoize.
2018 * @param keySupplier
2019 * The {@link Supplier} for the cache key.
2020 * @param cache
2021 * The {@link Cache} to use.
2022 * @return The wrapped {@link Supplier}.
2024 public static final <KEY, OUTPUT> Supplier<OUTPUT> supplier(
2025 final Supplier<OUTPUT> supplier,
2026 final Supplier<KEY> keySupplier,
2027 final Cache<KEY, OUTPUT> cache) {
2028 return new GuavaCacheBasedSupplierMemoizer<>(cache, keySupplier, supplier);