java类java.util.function.LongFunction的实例源码

SlidingWindow.java 文件源码 项目:incubator-ratis 阅读 41 收藏 0 点赞 0 评论 0
/**
 * 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;
}
RaftClientImpl.java 文件源码 项目:incubator-ratis 阅读 40 收藏 0 点赞 0 评论 0
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());
}
LongPipeline.java 文件源码 项目:OpenJSharp 阅读 44 收藏 0 点赞 0 评论 0
@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));
                }
            };
        }
    };
}
LongPipeline.java 文件源码 项目:OpenJSharp 阅读 41 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
LongPipeline.java 文件源码 项目:jdk8u-jdk 阅读 39 收藏 0 点赞 0 评论 0
@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));
                }
            };
        }
    };
}
LongPipeline.java 文件源码 项目:jdk8u-jdk 阅读 51 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
IntegralPrimitiveToString.java 文件源码 项目:jdk8u-jdk 阅读 41 收藏 0 点赞 0 评论 0
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()]);
}
LongPipeline.java 文件源码 项目:openjdk-jdk10 阅读 41 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
IntegralPrimitiveToString.java 文件源码 项目:openjdk-jdk10 阅读 34 收藏 0 点赞 0 评论 0
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()]);
}
LongPipeline.java 文件源码 项目:openjdk9 阅读 48 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
IntegralPrimitiveToString.java 文件源码 项目:openjdk9 阅读 39 收藏 0 点赞 0 评论 0
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()]);
}
ConcurrentMapBasedLongToIntFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 37 收藏 0 点赞 0 评论 0
/**
*
*/
@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);
}
ConcurrentMapBasedLongToIntFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 52 收藏 0 点赞 0 评论 0
/**
*
*/
@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);
}
LongPipeline.java 文件源码 项目:jdk8u_jdk 阅读 47 收藏 0 点赞 0 评论 0
@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));
                }
            };
        }
    };
}
LongPipeline.java 文件源码 项目:jdk8u_jdk 阅读 49 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
ConcurrentMapBasedLongToIntFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 34 收藏 0 点赞 0 评论 0
/**
*
*/
@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());
}
ConcurrentMapBasedLongPredicateMemoizerTest.java 文件源码 项目:memoization.java 阅读 54 收藏 0 点赞 0 评论 0
/**
*
*/
@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);
}
LongPipeline.java 文件源码 项目:lookaside_java-1.8.0-openjdk 阅读 42 收藏 0 点赞 0 评论 0
@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));
                    }
                }
            };
        }
    };
}
ConcurrentMapBasedLongFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 35 收藏 0 点赞 0 评论 0
/**
 *
 */
@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);
}
ConcurrentMapBasedLongFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 37 收藏 0 点赞 0 评论 0
/**
 *
 */
@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);
}
ConcurrentMapBasedLongUnaryOperatorMemoizerTest.java 文件源码 项目:memoization.java 阅读 35 收藏 0 点赞 0 评论 0
/**
*
*/
@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());
}
ConcurrentMapBasedLongToDoubleFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 50 收藏 0 点赞 0 评论 0
/**
*
*/
@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);
}
GuavaCacheBasedLongToIntFunctionMemoizerTest.java 文件源码 项目:memoization.java 阅读 38 收藏 0 点赞 0 评论 0
/**
 * @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);
}
GuavaCacheBasedLongConsumerMemoizerTest.java 文件源码 项目:memoization.java 阅读 27 收藏 0 点赞 0 评论 0
/**
 * @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);
}
GuavaCacheBasedLongPredicateMemoizerTest.java 文件源码 项目:memoization.java 阅读 33 收藏 0 点赞 0 评论 0
/**
*
*/
@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);
}
GuavaCacheBasedLongPredicateMemoizerTest.java 文件源码 项目:memoization.java 阅读 43 收藏 0 点赞 0 评论 0
/**
 * @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);
}
ConcurrentMapBasedLongConsumerMemoizerTest.java 文件源码 项目:memoization.java 阅读 42 收藏 0 点赞 0 评论 0
/**
*
*/
@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());
}
Nodes.java 文件源码 项目:OpenJSharp 阅读 34 收藏 0 点赞 0 评论 0
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;
}
Nodes.java 文件源码 项目:jdk8u-jdk 阅读 38 收藏 0 点赞 0 评论 0
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;
}
LongPipeline.java 文件源码 项目:openjdk-jdk10 阅读 68 收藏 0 点赞 0 评论 0
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));
                }
            };
        }
    };
}


问题


面经


文章

微信
公众号

扫码关注公众号