/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjLongConsumer<String> biConsumer = Mockito.mock(ObjLongConsumer.class); final ObjLongFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjLongConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjLongConsumerMemoizer<>( cache, keyFunction, biConsumer); // then memoizer.accept("first", 123); Mockito.verify(biConsumer).accept("first", 123); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjLongConsumer<String> biConsumer = Mockito.mock(ObjLongConsumer.class); final ObjLongFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { // when final JCacheBasedObjLongConsumerMemoizer<String, String> memoizer = new JCacheBasedObjLongConsumerMemoizer<>( 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 ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<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 ConcurrentMapBasedObjLongConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyFunction() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjLongFunction<String, String> keyFunction = null; final ObjLongConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Provide a key function, might just be 'MemoizationDefaults.objLongConsumerHashCodeKeyFunction()'."); // then new ConcurrentMapBasedObjLongConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<String> consumer = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); // then new ConcurrentMapBasedObjLongConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ConcurrentMapBasedObjLongConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjLongConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "test123", 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 ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<String> consumer = Mockito.mock(ObjLongConsumer.class); // when final ConcurrentMapBasedObjLongConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjLongConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123L); Mockito.verify(consumer).accept("test", 123L); }
@Test public void testCheckedObjLongConsumer() { final CheckedObjLongConsumer<Object> objLongConsumer = (o1, o2) -> { throw new Exception(o1 + ":" + o2); }; ObjLongConsumer<Object> c1 = Unchecked.objLongConsumer(objLongConsumer); ObjLongConsumer<Object> c2 = CheckedObjLongConsumer.unchecked(objLongConsumer); ObjLongConsumer<Object> c3 = Sneaky.objLongConsumer(objLongConsumer); ObjLongConsumer<Object> c4 = CheckedObjLongConsumer.sneaky(objLongConsumer); assertObjLongConsumer(c1, UncheckedException.class); assertObjLongConsumer(c2, UncheckedException.class); assertObjLongConsumer(c3, Exception.class); assertObjLongConsumer(c4, Exception.class); }
/** * Constructs a {@code TerminalOp} that implements a mutable reduce on * {@code long} 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<Long, R> makeLong(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BinaryOperator<R> combiner) { Objects.requireNonNull(supplier); Objects.requireNonNull(accumulator); Objects.requireNonNull(combiner); class ReducingSink extends Box<R> implements AccumulatingSink<Long, R, ReducingSink>, Sink.OfLong { @Override public void begin(long size) { state = supplier.get(); } @Override public void accept(long t) { accumulator.accept(state, t); } @Override public void combine(ReducingSink other) { state = combiner.apply(state, other.state); } } return new ReduceOp<Long, R, ReducingSink>(StreamShape.LONG_VALUE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } }; }
@Override public final <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner) { BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeLong(supplier, accumulator, operator)); }
@Override public final <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner) { Objects.requireNonNull(combiner); BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeLong(supplier, accumulator, operator)); }
public CollectLongTerminatorImpl( HasNext<Long, LongStream> previous, boolean parallel, Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner) { super(previous, parallel); this.supplier = requireNonNull(supplier); this.accumulator = requireNonNull(accumulator); this.combiner = requireNonNull(combiner); }
static <R> CollectLongTerminator<R> create( HasNext<Long, LongStream> previous, boolean parallel, Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> merger) { return new CollectLongTerminatorImpl<>( previous, parallel, supplier, accumulator, merger ); }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithKeyFunction() { // given final ObjLongConsumer<Long> function = (first, second) -> System.out.println(first.toString() + second); final ObjLongFunction<Long, String> keyFunction = MemoizationDefaults.objLongConsumerHashCodeKeyFunction(); // when final ObjLongConsumer<Long> memoize = CaffeineMemoize.objLongConsumer(function, keyFunction); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumer() { // given final ObjLongConsumer<Long> function = (first, second) -> System.out.println(first + " " + second); // when final ObjLongConsumer<Long> memoize = CaffeineMemoize.objLongConsumer(function); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithLambda() { // given // when final ObjLongConsumer<Long> memoize = CaffeineMemoize .objLongConsumer((first, second) -> System.out.println(first + " " + second)); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
GuavaCacheBasedObjLongConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjLongFunction<FIRST, KEY> keyFunction, final ObjLongConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiConsumer() { // given final ObjLongConsumer<String> biConsumer = (first, second) -> System.out.println(first + second); final ObjLongFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjLongConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjLongConsumerMemoizer<>( cache, keyFunction, biConsumer); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithKeyFunction() { // given final ObjLongConsumer<String> consumer = (a, b) -> System.out.println(a + b); final ObjLongFunction<String, String> keyFunction = (a, b) -> "key"; // when final ObjLongConsumer<String> memoize = GuavaMemoize.objLongConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithLambda() { // given // when final ObjLongConsumer<String> memoize = GuavaMemoize.objLongConsumer((a, b) -> System.out.println(a + b)); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumer() { // given final ObjLongConsumer<String> consumer = (a, b) -> System.out.println(a + b); // when final ObjLongConsumer<String> memoize = GuavaMemoize.objLongConsumer(consumer); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
JCacheBasedObjLongConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjLongFunction<FIRST, KEY> keyFunction, final ObjLongConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }
/** * */ @Test public void shouldMemoizeBiConsumer() { // given final ObjLongConsumer<String> biConsumer = (first, second) -> System.out.println(first + second); final ObjLongFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { // when final JCacheBasedObjLongConsumerMemoizer<String, String> memoizer = new JCacheBasedObjLongConsumerMemoizer<>( cache, keyfunction, biConsumer); // then Assert.assertNotNull("Memoizer is NULL", memoizer); } }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithKeyFunction() { // given final ObjLongConsumer<String> consumer = (a, b) -> System.out.println(a + b); final ObjLongFunction<String, String> keyFunction = (a, b) -> "key"; // when final ObjLongConsumer<String> memoize = JCacheMemoize.objLongConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumerWithLambda() { // given // when final ObjLongConsumer<String> memoize = JCacheMemoize.objLongConsumer((a, b) -> System.out.println(a + b)); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjLongConsumer() { // given final ObjLongConsumer<String> consumer = (a, b) -> System.out.println(a + b); // when final ObjLongConsumer<String> memoize = JCacheMemoize.objLongConsumer(consumer); // then Assert.assertNotNull("Memoized ObjLongConsumer is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedObjLongConsumerMemoizer( final ConcurrentMap<KEY, INPUT> cache, final ObjLongFunction<INPUT, KEY> keyFunction, final ObjLongConsumer<INPUT> consumer) { super(cache); this.keyFunction = requireNonNull(keyFunction, "Provide a key function, might just be 'MemoizationDefaults.objLongConsumerHashCodeKeyFunction()'."); this.consumer = requireNonNull(consumer, "Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ConcurrentMapBasedObjLongConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjLongConsumerMemoizer<>( cache, keyFunction, consumer); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjLongFunction<String, String> keyFunction = (first, second) -> first + second; final ObjLongConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ConcurrentMapBasedObjLongConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjLongConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123); }