本文整理汇总了Java中java.util.function.LongPredicate类的典型用法代码示例。如果您正苦于以下问题:Java LongPredicate类的具体用法?Java LongPredicate怎么用?Java LongPredicate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LongPredicate类属于java.util.function包,在下文中一共展示了LongPredicate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: LongPredicate
import java.util.function.LongPredicate; //导入依赖的package包/类
@Test
public void LongPredicate()
{
// TODO - Convert the anonymous inner class to a lambda
LongPredicate predicate = new LongPredicate()
{
@Override
public boolean test(long value)
{
return value % 2 == 0;
}
};
List<Long> evens =
LongStream.rangeClosed(1, 5).filter(predicate).boxed().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList(2L, 4L), evens);
List<Long> odds =
LongStream.rangeClosed(1, 5).filter(predicate.negate()).boxed().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList(1L, 3L, 5L), odds);
Assert.assertTrue(LongStream.rangeClosed(1, 5).anyMatch(predicate));
Assert.assertFalse(LongStream.rangeClosed(1, 5).allMatch(predicate));
Assert.assertFalse(LongStream.rangeClosed(1, 5).noneMatch(predicate));
}
示例2: filter
import java.util.function.LongPredicate; //导入依赖的package包/类
@Override
public final LongStream filter(LongPredicate predicate) {
Objects.requireNonNull(predicate);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
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) {
if (predicate.test(t))
downstream.accept(t);
}
};
}
};
}
示例3: makeLong
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
* Constructs a quantified predicate matcher for a {@code LongStream}.
*
* @param predicate the {@code Predicate} to apply to stream elements
* @param matchKind the kind of quantified match (all, any, none)
* @return a {@code TerminalOp} implementing the desired quantified match
* criteria
*/
public static TerminalOp<Long, Boolean> makeLong(LongPredicate predicate,
MatchKind matchKind) {
Objects.requireNonNull(predicate);
Objects.requireNonNull(matchKind);
class MatchSink extends BooleanTerminalSink<Long> implements Sink.OfLong {
MatchSink() {
super(matchKind);
}
@Override
public void accept(long t) {
if (!stop && predicate.test(t) == matchKind.stopOnPredicateMatches) {
stop = true;
value = matchKind.shortCircuitResult;
}
}
}
return new MatchOp<>(StreamShape.LONG_VALUE, matchKind, MatchSink::new);
}
示例4: shouldRequireNonNullCache
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
*
*/
@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);
}
示例5: shouldTestGivenValue
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
*
*/
@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);
}
示例6: shouldWrapExecutionExceptionInMemoizationException
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
* @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);
}
示例7: assertLongPredicates
import java.util.function.LongPredicate; //导入依赖的package包/类
private void assertLongPredicates(Supplier<LongStream> source, Kind kind, LongPredicate[] predicates, boolean... answers) {
for (int i = 0; i < predicates.length; i++) {
setContext("i", i);
boolean match = longKinds.get(kind).apply(predicates[i]).apply(source.get());
assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
}
}
示例8: testLongStream
import java.util.function.LongPredicate; //导入依赖的package包/类
@Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
public void testLongStream(String name, TestData.OfLong data) {
for (LongPredicate p : LONG_PREDICATES) {
setContext("p", p);
for (Kind kind : Kind.values()) {
setContext("kind", kind);
exerciseTerminalOps(data, longKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(lpFalse), longKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(lpEven), longKinds.get(kind).apply(p));
}
}
}
示例9: should_verify_an_actual_long_predicate_is_conform_to_an_expected_result
import java.util.function.LongPredicate; //导入依赖的package包/类
@Test
public void should_verify_an_actual_long_predicate_is_conform_to_an_expected_result() {
assertThat(resultOf(() -> {
gwtMock.whenAnEventHappensInRelationToAnActionOfTheConsumer();
return (LongPredicate) number -> number < 100;
}).accepts(99)).hasSameClassAs(assertThat((LongPredicate) t -> t == 0));
}
示例10: RangeOfLongs
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
* Constructor
* @param start the start for range, inclusive
* @param end the end for range, exclusive
* @param step the step increment
* @param excludes optional predicate to exclude elements in this range
*/
RangeOfLongs(long start, long end, long step, LongPredicate excludes) {
super(start, end);
this.start = start;
this.end = end;
this.step = step;
this.ascend = start <= end;
this.excludes = excludes;
}
示例11: thatReturnsOnCatch
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
* @param defaultReturnValue A value to return if any throwable is caught.
* @return An interface that returns a default value if any exception occurs.
*/
default LongPredicate thatReturnsOnCatch(final boolean defaultReturnValue) {
return (final long v1) -> {
try {
return testWithThrowable(v1);
} catch(final Throwable throwable) {
return defaultReturnValue;
}
};
}
示例12: shouldMemoizeLongPredicateWithKeyFunction
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
*
*/
@Test
public void shouldMemoizeLongPredicateWithKeyFunction() {
// given
final LongPredicate predicate = a -> true;
final LongFunction<String> keyFunction = a -> "key";
// when
final LongPredicate memoize = JCacheMemoize.longPredicate(predicate, keyFunction);
// then
Assert.assertNotNull("Memoized LongPredicate is NULL", memoize);
}
示例13: shouldMemoizePredicateCall
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
*
*/
@Test
public void shouldMemoizePredicateCall() {
// given
final ConcurrentMap<Long, Boolean> cache = new ConcurrentHashMap<>();
final LongPredicate predicate = input -> true;
final LongFunction<Long> keyFunction = Long::valueOf;
// when
final ConcurrentMapBasedLongPredicateMemoizer<Long> memoizer = new ConcurrentMapBasedLongPredicateMemoizer<>(
cache, keyFunction, predicate);
// then
Assert.assertTrue("Memoized value does not match expectations", memoizer.test(123L));
}
示例14: shouldMemoizeLongPredicateWithLambda
import java.util.function.LongPredicate; //导入依赖的package包/类
/**
*
*/
@Test
public void shouldMemoizeLongPredicateWithLambda() {
// given
// when
final LongPredicate memoize = JCacheMemoize.longPredicate(a -> true);
// then
Assert.assertNotNull("Memoized LongPredicate is NULL", memoize);
}
示例15: ConcurrentMapBasedLongPredicateMemoizer
import java.util.function.LongPredicate; //导入依赖的package包/类
@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.");
}