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
7 package de
.xn__ho_hia
.memoization
.guava
;
9 import java
.util
.function
.BiConsumer
;
10 import java
.util
.function
.BiFunction
;
11 import java
.util
.function
.BiPredicate
;
12 import java
.util
.function
.BooleanSupplier
;
13 import java
.util
.function
.Consumer
;
14 import java
.util
.function
.DoubleBinaryOperator
;
15 import java
.util
.function
.DoubleConsumer
;
16 import java
.util
.function
.DoubleFunction
;
17 import java
.util
.function
.DoublePredicate
;
18 import java
.util
.function
.DoubleSupplier
;
19 import java
.util
.function
.DoubleToIntFunction
;
20 import java
.util
.function
.DoubleToLongFunction
;
21 import java
.util
.function
.DoubleUnaryOperator
;
22 import java
.util
.function
.Function
;
23 import java
.util
.function
.IntBinaryOperator
;
24 import java
.util
.function
.IntConsumer
;
25 import java
.util
.function
.IntFunction
;
26 import java
.util
.function
.IntPredicate
;
27 import java
.util
.function
.IntSupplier
;
28 import java
.util
.function
.IntToDoubleFunction
;
29 import java
.util
.function
.LongBinaryOperator
;
30 import java
.util
.function
.LongConsumer
;
31 import java
.util
.function
.LongFunction
;
32 import java
.util
.function
.LongPredicate
;
33 import java
.util
.function
.LongSupplier
;
34 import java
.util
.function
.Predicate
;
35 import java
.util
.function
.Supplier
;
37 import org
.junit
.Assert
;
38 import org
.junit
.Test
;
40 import de
.xn__ho_hia
.memoization
.shared
.DoubleBinaryFunction
;
41 import de
.xn__ho_hia
.memoization
.shared
.IntBinaryFunction
;
42 import de
.xn__ho_hia
.memoization
.shared
.LongBinaryFunction
;
43 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
49 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
50 public class GuavaMemoizeCustomKeyTest
{
56 public void shouldMemoizeBiConsumerWithKeyBiFunction() {
58 final BiConsumer
<String
, String
> biConsumer
= (a
, b
) -> System
.out
.println(a
+ b
);
59 final BiFunction
<String
, String
, String
> keyFunction
= (a
, b
) -> "key";
62 final BiConsumer
<String
, String
> memoize
= GuavaMemoize
.biConsumer(biConsumer
, keyFunction
);
65 Assert
.assertNotNull("Memoized BiConsumer is NULL", memoize
);
72 public void shouldMemoizeBiFunctionWithKeyBiFunction() {
74 final BiFunction
<String
, String
, String
> biFunction
= (a
, b
) -> "test";
75 final BiFunction
<String
, String
, String
> keyFunction
= (a
, b
) -> "key";
78 final BiFunction
<String
, String
, String
> memoize
= GuavaMemoize
.biFunction(biFunction
, keyFunction
);
81 Assert
.assertNotNull("Memoized BiFunction is NULL", memoize
);
88 public void shouldMemoizeBiPredicateWithKeyBiFunction() {
90 final BiPredicate
<String
, String
> biPredicate
= (a
, b
) -> true;
91 final BiFunction
<String
, String
, String
> keyFunction
= (a
, b
) -> "key";
94 final BiPredicate
<String
, String
> memoize
= GuavaMemoize
.biPredicate(biPredicate
, keyFunction
);
97 Assert
.assertNotNull("Memoized BiPredicate is NULL", memoize
);
104 public void shouldMemoizeBooleanSupplierWithKeySupplier() {
106 final BooleanSupplier supplier
= () -> true;
107 final Supplier
<String
> keySupplier
= () -> "key";
110 final BooleanSupplier memoize
= GuavaMemoize
.booleanSupplier(supplier
, keySupplier
);
113 Assert
.assertNotNull("Memoized BooleanSupplier is NULL", memoize
);
120 public void shouldMemoizeConsumerWithKeyFunction() {
122 final Consumer
<String
> consumer
= System
.out
::println
;
123 final Function
<String
, String
> keyFunction
= a
-> "key";
126 final Consumer
<String
> memoize
= GuavaMemoize
.consumer(consumer
, keyFunction
);
129 Assert
.assertNotNull("Memoized Consumer is NULL", memoize
);
136 public void shouldMemoizeDoubleBinaryOperatorWithKeyFunction() {
138 final DoubleBinaryOperator operator
= (a
, b
) -> 123.456D
;
139 final DoubleBinaryFunction
<String
> keyFunction
= (a
, b
) -> "key";
142 final DoubleBinaryOperator memoize
= GuavaMemoize
.doubleBinaryOperator(operator
, keyFunction
);
145 Assert
.assertNotNull("Memoized DoubleBinaryOperator is NULL", memoize
);
152 public void shouldMemoizeDoubleConsumerWithKeyFunction() {
154 final DoubleConsumer consumer
= System
.out
::println
;
155 final DoubleFunction
<String
> keyFunction
= a
-> "key";
158 final DoubleConsumer memoize
= GuavaMemoize
.doubleConsumer(consumer
, keyFunction
);
161 Assert
.assertNotNull("Memoized DoubleConsumer is NULL", memoize
);
168 public void shouldMemoizeDoubleFunctionWithKeyFunction() {
170 final DoubleFunction
<String
> function
= a
-> "test";
171 final DoubleFunction
<String
> keyFunction
= a
-> "key";
174 final DoubleFunction
<String
> memoize
= GuavaMemoize
.doubleFunction(function
, keyFunction
);
177 Assert
.assertNotNull("Memoized DoubleFunction is NULL", memoize
);
184 public void shouldMemoizeDoublePredicateWithKeyFunction() {
186 final DoublePredicate predicate
= a
-> true;
187 final DoubleFunction
<String
> keyFunction
= a
-> "key";
190 final DoublePredicate memoize
= GuavaMemoize
.doublePredicate(predicate
, keyFunction
);
193 Assert
.assertNotNull("Memoized DoublePredicate is NULL", memoize
);
200 public void shouldMemoizeDoubleSupplierWithKeySupplier() {
202 final DoubleSupplier supplier
= () -> 123.456D
;
203 final Supplier
<String
> keySupplier
= () -> "key";
206 final DoubleSupplier memoize
= GuavaMemoize
.doubleSupplier(supplier
, keySupplier
);
209 Assert
.assertNotNull("Memoized DoubleSupplier is NULL", memoize
);
216 public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() {
218 final DoubleToIntFunction function
= a
-> 123;
219 final DoubleFunction
<Double
> keyFunction
= Double
::valueOf
;
222 final DoubleToIntFunction memoize
= GuavaMemoize
.doubleToIntFunction(function
, keyFunction
);
225 Assert
.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize
);
232 public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() {
234 final DoubleToLongFunction function
= a
-> 123;
235 final DoubleFunction
<Double
> keyFunction
= Double
::valueOf
;
238 final DoubleToLongFunction memoize
= GuavaMemoize
.doubleToLongFunction(function
, keyFunction
);
241 Assert
.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize
);
248 public void shouldMemoizeDoubleUnaryOperatorWithKeyFunction() {
250 final DoubleUnaryOperator function
= a
-> 123.456D
;
251 final DoubleFunction
<Double
> keyFunction
= Double
::valueOf
;
254 final DoubleUnaryOperator memoize
= GuavaMemoize
.doubleUnaryOperator(function
, keyFunction
);
257 Assert
.assertNotNull("Memoized DoubleUnaryOperator is NULL", memoize
);
264 public void shouldMemoizeFunctionWithKeyFunction() {
266 final Function
<String
, String
> function
= a
-> "test";
267 final Function
<String
, String
> keyFunction
= a
-> "key";
270 final Function
<String
, String
> memoize
= GuavaMemoize
.function(function
, keyFunction
);
273 Assert
.assertNotNull("Memoized Function is NULL", memoize
);
280 public void shouldMemoizeIntBinaryOperatorWithKeyFunction() {
282 final IntBinaryOperator function
= (a
, b
) -> 123;
283 final IntBinaryFunction
<String
> keyFunction
= (a
, b
) -> "key";
286 final IntBinaryOperator memoize
= GuavaMemoize
.intBinaryOperator(function
, keyFunction
);
289 Assert
.assertNotNull("Memoized IntBinaryOperator is NULL", memoize
);
296 public void shouldMemoizeIntConsumerWithKeyFunction() {
298 final IntConsumer consumer
= System
.out
::println
;
299 final IntFunction
<String
> keyFunction
= a
-> "key";
302 final IntConsumer memoize
= GuavaMemoize
.intConsumer(consumer
, keyFunction
);
305 Assert
.assertNotNull("Memoized IntConsumer is NULL", memoize
);
312 public void shouldMemoizeIntFunctionWithKeyFunction() {
314 final IntFunction
<String
> function
= a
-> "test";
315 final IntFunction
<String
> keyFunction
= a
-> "key";
318 final IntFunction
<String
> memoize
= GuavaMemoize
.intFunction(function
, keyFunction
);
321 Assert
.assertNotNull("Memoized IntFunction is NULL", memoize
);
328 public void shouldMemoizeIntPredicateWithKeyFunction() {
330 final IntPredicate predicate
= a
-> true;
331 final IntFunction
<String
> keyFunction
= a
-> "key";
334 final IntPredicate memoize
= GuavaMemoize
.intPredicate(predicate
, keyFunction
);
337 Assert
.assertNotNull("Memoized IntPredicate is NULL", memoize
);
344 public void shouldMemoizeIntSupplierWithKeySupplier() {
346 final IntSupplier supplier
= () -> 123;
347 final Supplier
<String
> keySupplier
= () -> "key";
350 final IntSupplier memoize
= GuavaMemoize
.intSupplier(supplier
, keySupplier
);
353 Assert
.assertNotNull("Memoized IntSupplier is NULL", memoize
);
360 public void shouldMemoizeIntToDoubleFunctionWithKeyFunction() {
362 final IntToDoubleFunction function
= a
-> 123D
;
363 final IntFunction
<Integer
> keyFunction
= Integer
::valueOf
;
366 final IntToDoubleFunction memoize
= GuavaMemoize
.intToDoubleFunction(function
, keyFunction
);
369 Assert
.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize
);
376 public void shouldMemoizeLongBinaryOperatorWithKeyFunction() {
378 final LongBinaryOperator operator
= (a
, b
) -> 123;
379 final LongBinaryFunction
<String
> keyFunction
= (a
, b
) -> "key";
382 final LongBinaryOperator memoize
= GuavaMemoize
.longBinaryOperator(operator
, keyFunction
);
385 Assert
.assertNotNull("Memoized LongBinaryOperator is NULL", memoize
);
392 public void shouldMemoizeLongConsumerWithKeyFunction() {
394 final LongConsumer consumer
= System
.out
::println
;
395 final LongFunction
<String
> keyFunction
= a
-> "key";
398 final LongConsumer memoize
= GuavaMemoize
.longConsumer(consumer
, keyFunction
);
401 Assert
.assertNotNull("Memoized LongConsumer is NULL", memoize
);
408 public void shouldMemoizeLongFunctionWithKeyFunction() {
410 final LongFunction
<String
> function
= a
-> "test";
411 final LongFunction
<String
> keyFunction
= a
-> "key";
414 final LongFunction
<String
> memoize
= GuavaMemoize
.longFunction(function
, keyFunction
);
417 Assert
.assertNotNull("Memoized LongFunction is NULL", memoize
);
424 public void shouldMemoizeLongPredicateWithLambda() {
426 final LongPredicate predicate
= a
-> true;
427 final LongFunction
<String
> keyFunction
= a
-> "key";
430 final LongPredicate memoize
= GuavaMemoize
.longPredicate(predicate
, keyFunction
);
433 Assert
.assertNotNull("Memoized LongPredicate is NULL", memoize
);
440 public void shouldMemoizeLongSupplierWithKeySupplier() {
442 final LongSupplier supplier
= () -> 123;
443 final Supplier
<String
> keySupplier
= () -> "key";
446 final LongSupplier memoize
= GuavaMemoize
.longSupplier(supplier
, keySupplier
);
449 Assert
.assertNotNull("Memoized LongSupplier is NULL", memoize
);
456 public void shouldMemoizePredicateWithKeyFunction() {
458 final Predicate
<String
> predicate
= a
-> true;
459 final Function
<String
, String
> keyFunction
= a
-> "key";
462 final Predicate
<String
> memoize
= GuavaMemoize
.predicate(predicate
, keyFunction
);
465 Assert
.assertNotNull("Memoized Predicate is NULL", memoize
);
472 public void shouldMemoizeSupplierWithKeySupplier() {
474 final Supplier
<String
> supplier
= () -> "test";
475 final Supplier
<String
> keySupplier
= () -> "key";
478 final Supplier
<String
> memoize
= GuavaMemoize
.supplier(supplier
, keySupplier
);
481 Assert
.assertNotNull("Memoized Supplier is NULL", memoize
);