/** * */ @Test public void shouldMemoizeBiFunction() { // given final ToLongBiFunction<String, String> function = (first, second) -> 123; final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, Long> cache = JCacheMemoize.createCache(ToLongBiFunction.class)) { // when final JCacheBasedToLongBiFunctionMemoizer<String, String, String> loader = new JCacheBasedToLongBiFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsLong("first", "second")); } }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, Long> cache = null; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToLongBiFunction<String, String> biFunction = (a, b) -> 123L; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedToLongBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyBiFunction() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = null; final ToLongBiFunction<String, String> biFunction = (a, b) -> 123L; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide a key function, might just be 'MemoizationDefaults.hashCodeKeyFunction()'."); // then new ConcurrentMapBasedToLongBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullBiFunction() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToLongBiFunction<String, String> biFunction = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL ToLongBiFunction - provide an actual ToLongBiFunction to fix this."); // then new ConcurrentMapBasedToLongBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToLongBiFunction<String, String> biFunction = (a, b) -> 123L; // when final ConcurrentMapBasedToLongBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToLongBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsLong("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", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedBiFunction() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToLongBiFunction<String, String> biFunction = Mockito.mock(ToLongBiFunction.class); // when final ConcurrentMapBasedToLongBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToLongBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsLong("123.456", "789.123"); Mockito.verify(biFunction).applyAsLong("123.456", "789.123"); }
static <ENTITY> SqlStreamOptimizerInfo<ENTITY> of( final DbmsType dbmsType, final String sqlSelect, final String sqlSelectCount, final ToLongBiFunction<String, List<Object>> counter, final Function<Field<ENTITY>, String> sqlColumnNamer, final Function<Field<ENTITY>, Class<?>> sqlDatabaseTypeFunction ) { return new SqlStreamOptimizerInfoImpl<>( dbmsType, sqlSelect, sqlSelectCount, counter, sqlColumnNamer, sqlDatabaseTypeFunction ); }
@Test public void testCheckedToLongBiFunction() { final CheckedToLongBiFunction<Object, Object> toLongBiFunction = (t, u) -> { throw new Exception(t + ":" + u); }; ToLongBiFunction<Object, Object> f1 = Unchecked.toLongBiFunction(toLongBiFunction); ToLongBiFunction<Object, Object> f2 = CheckedToLongBiFunction.unchecked(toLongBiFunction); ToLongBiFunction<Object, Object> f3 = Sneaky.toLongBiFunction(toLongBiFunction); ToLongBiFunction<Object, Object> f4 = CheckedToLongBiFunction.sneaky(toLongBiFunction); assertToLongBiFunction(f1, UncheckedException.class); assertToLongBiFunction(f2, UncheckedException.class); assertToLongBiFunction(f3, Exception.class); assertToLongBiFunction(f4, Exception.class); }
MapReduceMappingsToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceMappingsToLongTask<K,V> nextRight, ToLongBiFunction<? super K, ? super V> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongBiFunction<? super K, ? super V> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceMappingsToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V> t = (MapReduceMappingsToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
@Override public final void compute() { final ToLongBiFunction<? super K, ? super V> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long 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 MapReduceMappingsToLongTask<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.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V> t = (MapReduceMappingsToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceMappingsToLongTask(final BulkTask<K, V, ?> p, final int b, final int i, final int f, final Node<K, V>[] t, final MapReduceMappingsToLongTask<K, V> nextRight, final ToLongBiFunction<? super K, ? super V> transformer, final long basis, final LongBinaryOperator 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 ToLongBiFunction<? super K, ? super V> transformer; final LongBinaryOperator reducer; if ((((transformer = this.transformer)) != null) && (((reducer = this.reducer)) != null)) { long 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 MapReduceMappingsToLongTask<>(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.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") final MapReduceMappingsToLongTask<K, V> t = (MapReduceMappingsToLongTask<K, V>) c; @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K, V> s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
public DefaultCardinalityEstimator(double certaintyProb, int numInputs, boolean isAllowMoreInputs, ToLongBiFunction<long[], Configuration> singlePointEstimator) { this.certaintyProb = certaintyProb; this.numInputs = numInputs; this.singlePointEstimator = singlePointEstimator; this.isAllowMoreInputs = isAllowMoreInputs; }
public DefaultLoadEstimator(int numInputs, int numOutputs, double correctnessProbability, CardinalityEstimate nullCardinalityReplacement, ToLongBiFunction<long[], long[]> singlePointFunction) { this( numInputs, numOutputs, correctnessProbability, nullCardinalityReplacement, (context, inputEstimates, outputEstimates) -> singlePointFunction.applyAsLong(inputEstimates, outputEstimates) ); }
public IntervalLoadEstimator(int numInputs, int numOutputs, double correctnessProbability, ToLongBiFunction<long[], long[]> lowerBoundEstimator, ToLongBiFunction<long[], long[]> upperBoundEstimator) { this(numInputs, numOutputs, correctnessProbability, null, lowerBoundEstimator, upperBoundEstimator); }
public IntervalLoadEstimator(int numInputs, int numOutputs, double correctnessProbablity, CardinalityEstimate nullCardinalityReplacement, ToLongBiFunction<long[], long[]> lowerBoundEstimator, ToLongBiFunction<long[], long[]> upperBoundEstimator) { super(nullCardinalityReplacement); this.numInputs = numInputs; this.numOutputs = numOutputs; this.correctnessProbablity = correctnessProbablity; this.lowerBoundEstimator = lowerBoundEstimator; this.upperBoundEstimator = upperBoundEstimator; }
/** * */ @Test public void shouldMemoizeToLongBiFunctionWithKeyFunction() { // given final ToLongBiFunction<Long, Long> function = (first, second) -> first.longValue() + second.longValue(); final BiFunction<Long, Long, String> keyFunction = hashCodeKeyFunction(); // when final ToLongBiFunction<Long, Long> memoize = CaffeineMemoize.toLongBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToLongBiFunction() { // given final ToLongBiFunction<Long, Long> function = (first, second) -> first.longValue() + second.longValue(); // when final ToLongBiFunction<Long, Long> memoize = CaffeineMemoize.toLongBiFunction(function); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToLongBiFunctionWithLambda() { // given // when final ToLongBiFunction<Long, Long> memoize = CaffeineMemoize .toLongBiFunction((first, second) -> first.longValue() + second.longValue()); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
GuavaCacheBasedToLongBiFunctionMemoizer( final Cache<KEY, Long> cache, final BiFunction<FIRST, SECOND, KEY> keyFunction, final ToLongBiFunction<FIRST, SECOND> biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeToLongBiFunctionWithKeyFunction() { // given final ToLongBiFunction<String, String> function = (a, b) -> 123; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; // when final ToLongBiFunction<String, String> memoize = GuavaMemoize.toLongBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiFunction() { // given final ToLongBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Long> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToLongBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToLongBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final ToLongBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Long> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToLongBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToLongBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertEquals("firstsecond", 123, memoizer.applyAsLong("first", "second")); }
/** * */ @Test public void shouldMemoizeToLongBiFunctionWithLambda() { // given // when final ToLongBiFunction<String, String> memoize = GuavaMemoize.toLongBiFunction((a, b) -> 123); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToLongBiFunction() { // given final ToLongBiFunction<String, String> function = (a, b) -> 123; // when final ToLongBiFunction<String, String> memoize = GuavaMemoize.toLongBiFunction(function); // then Assert.assertNotNull("Memoized ToLongBiFunction is NULL", memoize); }
JCacheBasedToLongBiFunctionMemoizer( final Cache<KEY, Long> cache, final BiFunction<FIRST, SECOND, KEY> keyFunction, final ToLongBiFunction<FIRST, SECOND> biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }