/** * A new request arrives, create it with {@link #nextSeqNum} * and then try sending it to the server. * * @param requestConstructor use seqNum to create a new request. * @return the new request. */ public synchronized REQUEST submitNewRequest( LongFunction<REQUEST> requestConstructor, Consumer<REQUEST> sendMethod) { if (!requests.isEmpty()) { Preconditions.assertTrue(nextSeqNum == requests.lastSeqNum() + 1, () -> "nextSeqNum=" + nextSeqNum + " but " + this); } final long seqNum = nextSeqNum++; final REQUEST r = requestConstructor.apply(seqNum); requests.putNewRequest(r); final boolean submitted = sendOrDelayRequest(r, sendMethod); LOG.debug("{}: submitting a new request {} in {}? {}", requests.getName(), r, this, submitted? "submitted": "delayed"); return r; }
private CompletableFuture<RaftClientReply> sendAsync(Message message, boolean readOnly) { Objects.requireNonNull(message, "message == null"); try { asyncRequestSemaphore.acquire(); } catch (InterruptedException e) { throw new CompletionException(IOUtils.toInterruptedIOException( "Interrupted when sending " + message, e)); } final long callId = nextCallId(); final LongFunction<PendingAsyncRequest> constructor = seqNum -> new PendingAsyncRequest(seqNum, seq -> new RaftClientRequest(clientId, leaderId, groupId, callId, seq, message, readOnly)); return slidingWindow.submitNewRequest(constructor, this::sendRequestWithRetryAsync ).getReplyFuture( ).thenApply(reply -> handleStateMachineException(reply, CompletionException::new) ).whenComplete((r, e) -> asyncRequestSemaphore.release()); }
@Override public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) { Objects.requireNonNull(mapper); return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Long> opWrapSink(int flags, Sink<U> sink) { return new Sink.ChainedLong<U>(sink) { @Override public void accept(long t) { downstream.accept(mapper.apply(t)); } }; } }; }
@Override public final LongStream flatMap(LongFunction<? extends LongStream> mapper) { return new StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override Sink<Long> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedLong<Long>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(long t) { try (LongStream result = mapper.apply(t)) { // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it if (result != null) result.sequential().forEach(i -> downstream.accept(i)); } } }; } }; }
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) { List<N> numbers = new ArrayList<>(); for(int bitmag = 0; bitmag < bits; bitmag++) { long value = 1L << bitmag; numbers.add(boxer.apply(value)); numbers.add(boxer.apply(value - 1)); numbers.add(boxer.apply(value + 1)); numbers.add(boxer.apply(-value)); for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) { numbers.add(boxer.apply(value - SOME_PRIMES[divisor])); numbers.add(boxer.apply(value + SOME_PRIMES[divisor])); numbers.add(boxer.apply(value * SOME_PRIMES[divisor])); numbers.add(boxer.apply(value / SOME_PRIMES[divisor])); numbers.add(boxer.apply(value | SOME_PRIMES[divisor])); numbers.add(boxer.apply(value & SOME_PRIMES[divisor])); numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor])); } } numbers.addAll(Arrays.asList(extras)); return (N[]) numbers.toArray(new Number[numbers.size()]); }
@Override public final LongStream flatMap(LongFunction<? extends LongStream> mapper) { Objects.requireNonNull(mapper); return new StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override Sink<Long> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedLong<Long>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(long t) { try (LongStream result = mapper.apply(t)) { // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it if (result != null) result.sequential().forEach(i -> downstream.accept(i)); } } }; } }; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = null; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL LongToIntFunction - provide an actual LongToIntFunction to fix this."); // then new ConcurrentMapBasedLongToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Integer> cache = null; final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123, memoizer.viewCacheForTest().values().iterator().next().intValue()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Boolean> cache = null; final LongPredicate predicate = input -> true; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongPredicateMemoizer<>(cache, keyFunction, predicate); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, String> cache = null; final LongFunction<String> function = input -> "output"; final LongFunction<String> keyFunction = input -> "key"; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final LongFunction<String> function = null; final LongFunction<String> keyFunction = input -> "key"; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL LongFunction - provide an actual LongFunction to fix this."); // then new ConcurrentMapBasedLongFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongUnaryOperator operator = input -> input; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongUnaryOperatorMemoizer<Long> memoizer = new ConcurrentMapBasedLongUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsLong(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123, memoizer.viewCacheForTest().keySet().iterator().next().intValue()); Assert.assertEquals("Memoization value does not match expectations", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Integer> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongToIntFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToIntFunctionMemoizer<>( cache, keyFunction, null); // when thrown.expect(MemoizationException.class); // then memoizer.applyAsInt(789); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final LongFunction<String> keyFunction = a -> "key"; final LongConsumer consumer = System.out::println; final Cache<String, Long> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongConsumerMemoizer<String> memoizer = new GuavaCacheBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); // when thrown.expect(MemoizationException.class); // then memoizer.accept(123); }
/** * */ @Test public void shouldTestGivenValue() { // given final LongPredicate predicate = Mockito.mock(LongPredicate.class); final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); // then memoizer.test(123); Mockito.verify(predicate).test(123); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final LongPredicate predicate = a -> true; final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); // when thrown.expect(MemoizationException.class); // then memoizer.test(123); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongConsumer consumer = System.out::println; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongConsumerMemoizer<Long> memoizer = new ConcurrentMapBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); // then memoizer.accept(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
CollectorTask(PipelineHelper<P_OUT> helper, Spliterator<P_IN> spliterator, LongFunction<T_BUILDER> builderFactory, BinaryOperator<T_NODE> concFactory) { super(helper, spliterator); this.helper = helper; this.builderFactory = builderFactory; this.concFactory = concFactory; }
private <U> Stream<U> mapToObj(LongFunction<? extends U> mapper, int opFlags) { return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE, opFlags) { @Override Sink<Long> opWrapSink(int flags, Sink<U> sink) { return new Sink.ChainedLong<U>(sink) { @Override public void accept(long t) { downstream.accept(mapper.apply(t)); } }; } }; }
@DataProvider public static Object[][] floatArrayTypesProvider() { if (floatArrayTypes == null) { LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb); LongFunction<Object> bToD = Double::longBitsToDouble; floatArrayTypes = new Object[][]{ new Object[]{new ArrayType.Floats(), 0x7fc00000L, 0x7f800001L, bTof}, new Object[]{new ArrayType.Doubles(), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD}, }; } return floatArrayTypes; }
@DataProvider public static Object[][] objectArrayTypesProvider() { if (objectArrayTypes == null) { LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb); LongFunction<Object> bToD = Double::longBitsToDouble; objectArrayTypes = new Object[][]{ new Object[]{new ArrayType.BoxedIntegers()}, new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()}, }; } return objectArrayTypes; }
/** * Creates an LONG Printer that wraps the function provided * @param function the function to wrap * @return the newly created function Printer */ public static Printer<Long> forLong(LongFunction<String> function) { return new Printer<Long>(FunctionStyle.LONG, DEFAULT_NULL) { @Override public final String apply(long input) { return function.apply(input); } }; }
/** * Creates an LONG function that wraps to function provided * @param function the function to wrap * @param <O> the output type * @return the newly created function wrapper */ public static <O> Function2<Long,O> fromLong(LongFunction<O> function) { return new Function2<Long,O>(FunctionStyle.LONG) { @Override public final O apply(long input) { return function.apply(input); } }; }
/** * */ @Test public void shouldMemoizeLongFunctionWithKeyFunction() { // given final LongFunction<String> function = a -> "test"; final LongFunction<String> keyFunction = a -> "key"; // when final LongFunction<String> memoize = MapMemoize.longFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongPredicateWithKeyFunction() { // given final LongPredicate predicate = input -> true; final LongFunction<String> keyFunction = a -> "key"; // when final LongPredicate memoize = MapMemoize.longPredicate(predicate, keyFunction); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedLongPredicateMemoizer( final ConcurrentMap<KEY, Boolean> cache, final LongFunction<KEY> keyFunction, final LongPredicate predicate) { super(cache); this.keyFunction = keyFunction; this.predicate = requireNonNull(predicate, "Cannot memoize a NULL Predicate - provide an actual Predicate to fix this."); }