/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, Integer> cache = null; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToIntBiFunction<String, String> biFunction = (a, b) -> 123; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedToIntBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyBiFunction() { // given final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = null; final ToIntBiFunction<String, String> biFunction = (a, b) -> 123; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide a key function, might just be 'MemoizationDefaults.hashCodeKeyFunction()'."); // then new ConcurrentMapBasedToIntBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullBiFunction() { // given final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToIntBiFunction<String, String> biFunction = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL ToIntBiFunction - provide an actual ToIntBiFunction to fix this."); // then new ConcurrentMapBasedToIntBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToIntBiFunction<String, String> biFunction = (a, b) -> 123; // when final ConcurrentMapBasedToIntBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToIntBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsInt("123.456", "789.123"); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "key", memoizer.viewCacheForTest().keySet().iterator().next()); Assert.assertEquals("Memoization value does not match expectations", 123, memoizer.viewCacheForTest().values().iterator().next().intValue()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedBiFunction() { // given final ConcurrentMap<String, Integer> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToIntBiFunction<String, String> biFunction = Mockito.mock(ToIntBiFunction.class); // when final ConcurrentMapBasedToIntBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToIntBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsInt("123.456", "789.123"); Mockito.verify(biFunction).applyAsInt("123.456", "789.123"); }
/** * Compares two segments, first by start positions, then end positions, then * content, then sources. * * @param other * Sequence segment which needs to be compared. * @return the compare value of the start positions. */ @Override public int compareTo(final SequenceSegment other) { for (ToIntBiFunction<SequenceSegment, SequenceSegment> comp : COMPARATORS) { int result = comp.applyAsInt(this, other); if (result != 0) { return result; } } int candidateComp = 0; Iterator<Sequence> thisIt = this.getSources().iterator(); Iterator<Sequence> otherIt = other.getSources().iterator(); while (thisIt.hasNext()) { candidateComp = thisIt.next().getIdentifier() .compareTo(otherIt.next().getIdentifier()); if (candidateComp != 0) { return candidateComp; } } if (this.getIdentifier() == other.getIdentifier()) { candidateComp = 0; } return candidateComp; }
SimpleBufferTrigger(Supplier<Object> bufferFactory, ToIntBiFunction<Object, E> queueAdder, ScheduledExecutorService scheduledExecutorService, ThrowableConsumer<Object, Throwable> consumer, TriggerStrategy triggerStrategy, BiConsumer<Throwable, Object> exceptionHandler, long maxBufferCount, Consumer<E> rejectHandler) { this.queueAdder = queueAdder; this.bufferFactory = bufferFactory; this.consumer = consumer; this.exceptionHandler = exceptionHandler; this.maxBufferCount = maxBufferCount; this.rejectHandler = rejectHandler; this.buffer.set(this.bufferFactory.get()); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); scheduledExecutorService.schedule( new TriggerRunnable(scheduledExecutorService, triggerStrategy), DEFAULT_NEXT_TRIGGER_PERIOD, MILLISECONDS); }
private static Optional<ZonedDateTime> dateTimeFromTemporal( TemporalAccessor ta ) { ToIntBiFunction<ChronoField, Integer> getField = ( field, orElse ) -> ta.isSupported( field ) ? ta.get( field ) : orElse; ZoneId zone = Optional.<ZoneId>ofNullable( ta.query( offset() ) ) .orElse( Optional.ofNullable( ta.query( zoneId() ) ) .orElse( ZoneOffset.UTC ) ); int year = getField.applyAsInt( ChronoField.YEAR, THIS_YEAR ); int month = getField.applyAsInt( ChronoField.MONTH_OF_YEAR, 1 ); int day = getField.applyAsInt( ChronoField.DAY_OF_MONTH, 1 ); int hour = getField.applyAsInt( ChronoField.HOUR_OF_DAY, 0 ); int minute = getField.applyAsInt( ChronoField.MINUTE_OF_HOUR, 0 ); int second = getField.applyAsInt( ChronoField.SECOND_OF_MINUTE, 0 ); int nanos = getField.applyAsInt( ChronoField.NANO_OF_SECOND, 0 ); ZonedDateTime dateTime = ZonedDateTime.of( year, month, day, hour, minute, second, nanos, zone ); return Optional.of( dateTime ); }
@Test public void testCheckedToIntBiFunction() { final CheckedToIntBiFunction<Object, Object> toIntBiFunction = (t, u) -> { throw new Exception(t + ":" + u); }; ToIntBiFunction<Object, Object> f1 = Unchecked.toIntBiFunction(toIntBiFunction); ToIntBiFunction<Object, Object> f2 = CheckedToIntBiFunction.unchecked(toIntBiFunction); ToIntBiFunction<Object, Object> f3 = Sneaky.toIntBiFunction(toIntBiFunction); ToIntBiFunction<Object, Object> f4 = CheckedToIntBiFunction.sneaky(toIntBiFunction); assertToIntBiFunction(f1, UncheckedException.class); assertToIntBiFunction(f2, UncheckedException.class); assertToIntBiFunction(f3, Exception.class); assertToIntBiFunction(f4, Exception.class); }
MapReduceMappingsToIntTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceMappingsToIntTask<K,V> nextRight, ToIntBiFunction<? super K, ? super V> transformer, int basis, IntBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToIntBiFunction<? super K, ? super V> transformer; final IntBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { int r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceMappingsToIntTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V> t = (MapReduceMappingsToIntTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsInt(t.result, s.result); s = t.rights = s.nextRight; } } } }
public PartitionedFeatureVectors(int numPartitions, ExecutorService executor, ToIntBiFunction<String,float[]> partitioner) { Preconditions.checkArgument(numPartitions > 0); Objects.requireNonNull(executor); Objects.requireNonNull(partitioner); partitions = new FeatureVectorsPartition[numPartitions]; for (int i = 0; i < numPartitions; i++) { partitions[i] = new FeatureVectorsPartition(); } partitionMap = HashObjIntMaps.newMutableMap(); partitionMapLock = new AutoReadWriteLock(); this.partitioner = partitioner; this.executor = executor; }
@Override public final void compute() { final ToIntBiFunction<? super K, ? super V> transformer; final IntBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { int r = this.basis; for (int i = this.baseIndex, f, h; this.batch > 0 && (h = ((f = this.baseLimit) + i) >>> 1) > i;) { this.addToPendingCount(1); (this.rights = new MapReduceMappingsToIntTask<K,V> (this, this.batch >>>= 1, this.baseLimit = h, f, this.tab, this.rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = this.advance()) != null; ) { r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V> t = (MapReduceMappingsToIntTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsInt(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceMappingsToIntTask(final BulkTask<K, V, ?> p, final int b, final int i, final int f, final Node<K, V>[] t, final MapReduceMappingsToIntTask<K, V> nextRight, final ToIntBiFunction<? super K, ? super V> transformer, final int basis, final IntBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
@Override public final void compute() { final ToIntBiFunction<? super K, ? super V> transformer; final IntBinaryOperator reducer; if ((((transformer = this.transformer)) != null) && (((reducer = this.reducer)) != null)) { int r = this.basis; for (int i = this.baseIndex, f, h; (this.batch > 0) && (((h = (((f = this.baseLimit)) + i) >>> 1)) > i); ) { this.addToPendingCount(1); (this.rights = new MapReduceMappingsToIntTask<>(this, this.batch >>>= 1, this.baseLimit = h, f, this.tab, this.rights, transformer, r, reducer)).fork(); } for (Node<K, V> p; (p = this.advance()) != null; ) { r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") final MapReduceMappingsToIntTask<K, V> t = (MapReduceMappingsToIntTask<K, V>) c; @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K, V> s = t.rights; while (s != null) { t.result = reducer.applyAsInt(t.result, s.result); s = t.rights = s.nextRight; } } } }
/** * */ @Test public void shouldMemoizeToIntBiFunctionWithKeyFunction() { // given final ToIntBiFunction<Integer, Integer> function = (first, second) -> first.intValue() + second.intValue(); final BiFunction<Integer, Integer, String> keyFunction = hashCodeKeyFunction(); // when final ToIntBiFunction<Integer, Integer> memoize = CaffeineMemoize.toIntBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToIntBiFunction() { // given final ToIntBiFunction<Integer, Integer> function = (first, second) -> first.intValue() + second.intValue(); // when final ToIntBiFunction<Integer, Integer> memoize = CaffeineMemoize.toIntBiFunction(function); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToIntBiFunctionWithLambda() { // given // when final ToIntBiFunction<Integer, Integer> memoize = CaffeineMemoize .toIntBiFunction((first, second) -> first.intValue() + second.intValue()); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
GuavaCacheBasedToIntBiFunctionMemoizer( final Cache<KEY, Integer> cache, final BiFunction<FIRST, SECOND, KEY> keyFunction, final ToIntBiFunction<FIRST, SECOND> biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeToIntBiFunctionWithKeyFunction() { // given final ToIntBiFunction<String, String> function = (a, b) -> 123; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; // when final ToIntBiFunction<String, String> memoize = GuavaMemoize.toIntBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiFunction() { // given final ToIntBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToIntBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToIntBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final ToIntBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToIntBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToIntBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertEquals("firstsecond", 123, memoizer.applyAsInt("first", "second")); }
/** * */ @Test public void shouldMemoizeToIntBiFunctionWithLambda() { // given // when final ToIntBiFunction<String, String> memoize = GuavaMemoize.toIntBiFunction((a, b) -> 123); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToIntBiFunction() { // given final ToIntBiFunction<String, String> function = (a, b) -> 123; // when final ToIntBiFunction<String, String> memoize = GuavaMemoize.toIntBiFunction(function); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }
JCacheBasedToIntBiFunctionMemoizer( final Cache<KEY, Integer> cache, final BiFunction<FIRST, SECOND, KEY> keyFunction, final ToIntBiFunction<FIRST, SECOND> biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final ToIntBiFunction<String, String> function = (first, second) -> 123; final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, Integer> cache = JCacheMemoize.createCache(ToIntBiFunction.class)) { // when final JCacheBasedToIntBiFunctionMemoizer<String, String, String> loader = new JCacheBasedToIntBiFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsInt("first", "second")); } }
/** * */ @Test public void shouldMemoizeToIntBiFunctionWithKeyBiFunction() { // given final ToIntBiFunction<String, String> function = (a, b) -> 123; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; // when final ToIntBiFunction<String, String> memoize = JCacheMemoize.toIntBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToIntBiFunction is NULL", memoize); }