/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjDoubleConsumer<String> biConsumer = Mockito.mock(ObjDoubleConsumer.class); final ObjDoubleFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjDoubleConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjDoubleConsumerMemoizer<>( cache, keyFunction, biConsumer); // then memoizer.accept("first", 123); Mockito.verify(biConsumer).accept("first", 123); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjDoubleConsumer<String> biConsumer = Mockito.mock(ObjDoubleConsumer.class); final ObjDoubleFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { // when final JCacheBasedObjDoubleConsumerMemoizer<String, String> memoizer = new JCacheBasedObjDoubleConsumerMemoizer<>( cache, keyfunction, biConsumer); // then memoizer.accept("first", 123); Mockito.verify(biConsumer).accept("first", 123); } }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, String> cache = null; final ObjDoubleFunction<String, String> keyFunction = (first, second) -> first + second; final ObjDoubleConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedObjDoubleConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyFunction() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjDoubleFunction<String, String> keyFunction = null; final ObjDoubleConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Provide a key function, might just be 'MemoizationDefaults.objDoubleConsumerHashCodeKeyFunction()'."); // then new ConcurrentMapBasedObjDoubleConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjDoubleFunction<String, String> keyFunction = (first, second) -> first + second; final ObjDoubleConsumer<String> consumer = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); // then new ConcurrentMapBasedObjDoubleConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjDoubleFunction<String, String> keyFunction = (first, second) -> first + second; final ObjDoubleConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ConcurrentMapBasedObjDoubleConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjDoubleConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123.456D); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "test123.456", memoizer.viewCacheForTest().keySet().iterator().next()); Assert.assertEquals("Memoization value does not match expectations", "test", memoizer.viewCacheForTest().values().iterator().next()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjDoubleFunction<String, String> keyFunction = (first, second) -> first + second; final ObjDoubleConsumer<String> consumer = Mockito.mock(ObjDoubleConsumer.class); // when final ConcurrentMapBasedObjDoubleConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjDoubleConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123.456D); Mockito.verify(consumer).accept("test", 123.456D); }
@Test public void testCheckedObjDoubleConsumer() { final CheckedObjDoubleConsumer<Object> objDoubleConsumer = (o1, o2) -> { throw new Exception(o1 + ":" + o2); }; ObjDoubleConsumer<Object> c1 = Unchecked.objDoubleConsumer(objDoubleConsumer); ObjDoubleConsumer<Object> c2 = CheckedObjDoubleConsumer.unchecked(objDoubleConsumer); ObjDoubleConsumer<Object> c3 = Sneaky.objDoubleConsumer(objDoubleConsumer); ObjDoubleConsumer<Object> c4 = CheckedObjDoubleConsumer.sneaky(objDoubleConsumer); assertObjDoubleConsumer(c1, UncheckedException.class); assertObjDoubleConsumer(c2, UncheckedException.class); assertObjDoubleConsumer(c3, Exception.class); assertObjDoubleConsumer(c4, Exception.class); }
/** * Constructs a {@code TerminalOp} that implements a mutable reduce on * {@code double} values. * * @param <R> the type of the result * @param supplier a factory to produce a new accumulator of the result type * @param accumulator a function to incorporate an int into an * accumulator * @param combiner a function to combine an accumulator into another * @return a {@code TerminalOp} implementing the reduction */ public static <R> TerminalOp<Double, R> makeDouble(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BinaryOperator<R> combiner) { Objects.requireNonNull(supplier); Objects.requireNonNull(accumulator); Objects.requireNonNull(combiner); class ReducingSink extends Box<R> implements AccumulatingSink<Double, R, ReducingSink>, Sink.OfDouble { @Override public void begin(long size) { state = supplier.get(); } @Override public void accept(double t) { accumulator.accept(state, t); } @Override public void combine(ReducingSink other) { state = combiner.apply(state, other.state); } } return new ReduceOp<Double, R, ReducingSink>(StreamShape.DOUBLE_VALUE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } }; }
@Override public final <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) { BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeDouble(supplier, accumulator, operator)); }
@Override public final <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) { Objects.requireNonNull(combiner); BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeDouble(supplier, accumulator, operator)); }
public CollectDoubleTerminatorImpl( HasNext<Double, DoubleStream> previous, boolean parallel, Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) { super(previous, parallel); this.supplier = requireNonNull(supplier); this.accumulator = requireNonNull(accumulator); this.combiner = requireNonNull(combiner); }
static <R> CollectDoubleTerminator<R> create( HasNext<Double, DoubleStream> previous, boolean parallel, Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> merger) { return new CollectDoubleTerminatorImpl<>( previous, parallel, supplier, accumulator, merger ); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithKeyFunction() { // given final ObjDoubleConsumer<Long> consumer = (first, second) -> System.out.println(first.toString() + second); final ObjDoubleFunction<Long, String> keyFunction = objDoubleConsumerHashCodeKeyFunction(); // when final ObjDoubleConsumer<Long> memoize = CaffeineMemoize.objDoubleConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumer() { // given final ObjDoubleConsumer<Long> consumer = (first, second) -> System.out.println(first + " " + second); // when final ObjDoubleConsumer<Long> memoize = CaffeineMemoize.objDoubleConsumer(consumer); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithLambda() { // given // when final ObjDoubleConsumer<Long> memoize = CaffeineMemoize .objDoubleConsumer((first, second) -> System.out.println(first + " " + second)); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
GuavaCacheBasedObjDoubleConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjDoubleFunction<FIRST, KEY> keyFunction, final ObjDoubleConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithKeyFunction() { // given final ObjDoubleConsumer<String> consumer = (a, b) -> System.out.println(a + b); final ObjDoubleFunction<String, String> keyFunction = (a, b) -> "key"; // when final ObjDoubleConsumer<String> memoize = GuavaMemoize.objDoubleConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiConsumer() { // given final ObjDoubleConsumer<String> biConsumer = (first, second) -> System.out.println(first + second); final ObjDoubleFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjDoubleConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjDoubleConsumerMemoizer<>( cache, keyFunction, biConsumer); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithLambda() { // given // when final ObjDoubleConsumer<String> memoize = GuavaMemoize.objDoubleConsumer((a, b) -> System.out.println(a + b)); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumer() { // given final ObjDoubleConsumer<String> consumer = (a, b) -> System.out.println(a + b); // when final ObjDoubleConsumer<String> memoize = GuavaMemoize.objDoubleConsumer(consumer); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
JCacheBasedObjDoubleConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjDoubleFunction<FIRST, KEY> keyFunction, final ObjDoubleConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }
/** * */ @Test public void shouldMemoizeBiConsumer() { // given final ObjDoubleConsumer<String> biConsumer = (first, second) -> System.out.println(first + second); final ObjDoubleFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { // when final JCacheBasedObjDoubleConsumerMemoizer<String, String> memoizer = new JCacheBasedObjDoubleConsumerMemoizer<>( cache, keyfunction, biConsumer); // then Assert.assertNotNull("Memoizer is NULL", memoizer); } }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithKeyFunction() { // given final ObjDoubleConsumer<String> consumer = (a, b) -> System.out.println(a + b); final ObjDoubleFunction<String, String> keyFunction = (a, b) -> "key"; // when final ObjDoubleConsumer<String> memoize = JCacheMemoize.objDoubleConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithLambda() { // given // when final ObjDoubleConsumer<String> memoize = JCacheMemoize.objDoubleConsumer((a, b) -> System.out.println(a + b)); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumer() { // given final ObjDoubleConsumer<String> consumer = (a, b) -> System.out.println(a + b); // when final ObjDoubleConsumer<String> memoize = JCacheMemoize.objDoubleConsumer(consumer); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedObjDoubleConsumerMemoizer( final ConcurrentMap<KEY, INPUT> cache, final ObjDoubleFunction<INPUT, KEY> keyFunction, final ObjDoubleConsumer<INPUT> consumer) { super(cache); this.keyFunction = requireNonNull(keyFunction, "Provide a key function, might just be 'MemoizationDefaults.objDoubleConsumerHashCodeKeyFunction()'."); this.consumer = requireNonNull(consumer, "Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumer() { // given final ObjDoubleConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ObjDoubleConsumer<String> memoize = MapMemoize.objDoubleConsumer(consumer); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjDoubleConsumerWithLambda() { // given // when final ObjDoubleConsumer<String> memoize = MapMemoize .objDoubleConsumer((first, second) -> System.out.println(first + second)); // then Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize); }