/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjIntConsumer<String> biConsumer = Mockito.mock(ObjIntConsumer.class); final ObjIntFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjIntConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjIntConsumerMemoizer<>( cache, keyFunction, biConsumer); // then memoizer.accept("first", 123); Mockito.verify(biConsumer).accept("first", 123); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { // given final ObjIntConsumer<String> biConsumer = Mockito.mock(ObjIntConsumer.class); final ObjIntFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { // when final JCacheBasedObjIntConsumerMemoizer<String, String> memoizer = new JCacheBasedObjIntConsumerMemoizer<>( 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 ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<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 ConcurrentMapBasedObjIntConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyFunction() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = null; final ObjIntConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Provide a key function, might just be 'MemoizationDefaults.objIntConsumerHashCodeKeyFunction()'."); // then new ConcurrentMapBasedObjIntConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullConsumer() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); // then new ConcurrentMapBasedObjIntConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = (first, second) -> System.out.println(first + second); // when final ConcurrentMapBasedObjIntConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjIntConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123); 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 ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = Mockito.mock(ObjIntConsumer.class); // when final ConcurrentMapBasedObjIntConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjIntConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept("test", 123); Mockito.verify(consumer).accept("test", 123); }
@Nonnull @ReturnsMutableCopy public ICommonsSortedSet <IParticipantIdentifier> getAllContainedParticipantIDs () { final ICommonsSortedSet <IParticipantIdentifier> aTargetSet = new CommonsTreeSet <> (); final Query aQuery = PDQueryManager.andNotDeleted (new MatchAllDocsQuery ()); try { final ObjIntConsumer <Document> aConsumer = (aDoc, nDocID) -> aTargetSet.add (PDField.PARTICIPANT_ID.getDocValue (aDoc)); final Collector aCollector = new AllDocumentsCollector (m_aLucene, aConsumer); searchAtomic (aQuery, aCollector); } catch (final IOException ex) { s_aLogger.error ("Error searching for documents with query " + aQuery, ex); } return aTargetSet; }
public static void readUntilEOF (@Nonnull @WillClose final InputStream aIS, @Nonnull final byte [] aBuffer, @Nonnull final ObjIntConsumer <? super byte []> aConsumer) throws IOException { try { ValueEnforcer.notNull (aIS, "InputStream"); ValueEnforcer.notNull (aBuffer, "Buffer"); ValueEnforcer.notNull (aConsumer, "Consumer"); _readUntilEOF (aIS, aBuffer, aConsumer); } finally { close (aIS); } }
public static void readUntilEOF (@Nonnull @WillClose final Reader aReader, @Nonnull final char [] aBuffer, @Nonnull final ObjIntConsumer <? super char []> aConsumer) throws IOException { try { ValueEnforcer.notNull (aReader, "Reader"); ValueEnforcer.notNull (aBuffer, "Buffer"); ValueEnforcer.notNull (aConsumer, "Consumer"); _readUntilEOF (aReader, aBuffer, aConsumer); } finally { close (aReader); } }
@Test public void testCheckedObjIntConsumer() { final CheckedObjIntConsumer<Object> objIntConsumer = (o1, o2) -> { throw new Exception(o1 + ":" + o2); }; ObjIntConsumer<Object> c1 = Unchecked.objIntConsumer(objIntConsumer); ObjIntConsumer<Object> c2 = CheckedObjIntConsumer.unchecked(objIntConsumer); ObjIntConsumer<Object> c3 = Sneaky.objIntConsumer(objIntConsumer); ObjIntConsumer<Object> c4 = CheckedObjIntConsumer.sneaky(objIntConsumer); assertObjIntConsumer(c1, UncheckedException.class); assertObjIntConsumer(c2, UncheckedException.class); assertObjIntConsumer(c3, Exception.class); assertObjIntConsumer(c4, Exception.class); }
/** * Iterates through an iterable type, passing each item and the item's index * (a counter starting at zero) to the given consumer. */ public static <T> Object eachWithIndex(Iterable<T> receiver, ObjIntConsumer<T> consumer) { int count = 0; for (T t : receiver) { consumer.accept(t, count++); } return receiver; }
@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); for (int i = 0; i < length; i++) { action.accept(elementSet.asList().get(i), getCount(i)); } }
/** * Constructs a {@code TerminalOp} that implements a mutable reduce on * {@code int} 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 ReduceOp} implementing the reduction */ public static <R> TerminalOp<Integer, R> makeInt(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BinaryOperator<R> combiner) { Objects.requireNonNull(supplier); Objects.requireNonNull(accumulator); Objects.requireNonNull(combiner); class ReducingSink extends Box<R> implements AccumulatingSink<Integer, R, ReducingSink>, Sink.OfInt { @Override public void begin(long size) { state = supplier.get(); } @Override public void accept(int t) { accumulator.accept(state, t); } @Override public void combine(ReducingSink other) { state = combiner.apply(state, other.state); } } return new ReduceOp<Integer, R, ReducingSink>(StreamShape.INT_VALUE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } }; }
@Override public final <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) { BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeInt(supplier, accumulator, operator)); }
@Override public final <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) { Objects.requireNonNull(combiner); BinaryOperator<R> operator = (left, right) -> { combiner.accept(left, right); return left; }; return evaluate(ReduceOps.makeInt(supplier, accumulator, operator)); }
@Override public void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); for (int i = 0; i < size(); i++) { action.accept(elementSet.asList().get(i), getCount(i)); } }
@SuppressWarnings("ValueOfIncrementOrDecrementUsed") private <T> void setIndexes(T[] standings, Comparator<T> c, ObjIntConsumer<T> consumer) { Objects.requireNonNull(standings, "standings"); Objects.requireNonNull(c, "c"); Objects.requireNonNull(consumer, "consumer"); int i = 0, len = standings.length, lastIndex = 0; for (T last = null, standing; i < len; last = standing) { standing = standings[i++]; if (c.compare(standing, last) != 0) { lastIndex = i; } consumer.accept(standing, lastIndex); } }
public CollectIntTerminatorImpl( HasNext<Integer, IntStream> previous, boolean parallel, Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) { super(previous, parallel); this.supplier = requireNonNull(supplier); this.accumulator = requireNonNull(accumulator); this.combiner = requireNonNull(combiner); }
static <R> CollectIntTerminator<R> create( HasNext<Integer, IntStream> previous, boolean parallel, Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> merger) { return new CollectIntTerminatorImpl<>( previous, parallel, supplier, accumulator, merger ); }
/** * */ @Test public void shouldMemoizeObjIntConsumerWithKeyFunction() { // given final ObjIntConsumer<Long> consumer = (first, second) -> System.out.println(first.toString() + second); final ObjIntFunction<Long, String> keyFunction = MemoizationDefaults.objIntConsumerHashCodeKeyFunction(); // when final ObjIntConsumer<Long> memoize = CaffeineMemoize.objIntConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjIntConsumer() { // given final ObjIntConsumer<Long> consumer = (first, second) -> System.out.println(first + " " + second); // when final ObjIntConsumer<Long> memoize = CaffeineMemoize.objIntConsumer(consumer); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjIntConsumerWithLambda() { // given // when final ObjIntConsumer<Long> memoize = CaffeineMemoize .objIntConsumer((first, second) -> System.out.println(first + " " + second)); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
GuavaCacheBasedObjIntConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjIntFunction<FIRST, KEY> keyFunction, final ObjIntConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }
/** * */ @Test public void shouldMemoizeObjIntConsumerWithKeyFunction() { // given final ObjIntConsumer<String> consumer = (a, b) -> System.out.println(a + b); final ObjIntFunction<String, String> keyFunction = (a, b) -> "key"; // when final ObjIntConsumer<String> memoize = GuavaMemoize.objIntConsumer(consumer, keyFunction); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiConsumer() { // given final ObjIntConsumer<String> biConsumer = (first, second) -> System.out.println(first + second); final ObjIntFunction<String, String> keyFunction = (first, second) -> second + first; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedObjIntConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedObjIntConsumerMemoizer<>( cache, keyFunction, biConsumer); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldMemoizeObjIntConsumerWithLambda() { // given // when final ObjIntConsumer<String> memoize = GuavaMemoize.objIntConsumer((a, b) -> System.out.println(a + b)); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
/** * */ @Test public void shouldMemoizeObjIntConsumer() { // given final ObjIntConsumer<String> consumer = (a, b) -> System.out.println(a + b); // when final ObjIntConsumer<String> memoize = GuavaMemoize.objIntConsumer(consumer); // then Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize); }
JCacheBasedObjIntConsumerMemoizer( final Cache<KEY, KEY> cache, final ObjIntFunction<FIRST, KEY> keyFunction, final ObjIntConsumer<FIRST> biConsumer) { super(cache); this.keyFunction = keyFunction; this.biConsumer = biConsumer; }