本文整理匯總了Java中java.util.function.IntPredicate類的典型用法代碼示例。如果您正苦於以下問題:Java IntPredicate類的具體用法?Java IntPredicate怎麽用?Java IntPredicate使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
IntPredicate類屬於java.util.function包,在下文中一共展示了IntPredicate類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: IntPredicate
import java.util.function.IntPredicate; //導入依賴的package包/類
@Test
public void IntPredicate()
{
// TODO - Convert the anonymous inner class to a lambda
IntPredicate predicate = new IntPredicate()
{
@Override
public boolean test(int value)
{
return value % 2 == 0;
}
};
List<Integer> evens =
IntStream.rangeClosed(1, 5).filter(predicate).boxed().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList(2, 4), evens);
List<Integer> odds =
IntStream.rangeClosed(1, 5).filter(predicate.negate()).boxed().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList(1, 3, 5), odds);
Assert.assertTrue(IntStream.rangeClosed(1, 5).anyMatch(predicate));
Assert.assertFalse(IntStream.rangeClosed(1, 5).allMatch(predicate));
Assert.assertFalse(IntStream.rangeClosed(1, 5).noneMatch(predicate));
}
示例2: makeInt
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
* Constructs a quantified predicate matcher for an {@code IntStream}.
*
* @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<Integer, Boolean> makeInt(IntPredicate predicate,
MatchKind matchKind) {
Objects.requireNonNull(predicate);
Objects.requireNonNull(matchKind);
class MatchSink extends BooleanTerminalSink<Integer> implements Sink.OfInt {
MatchSink() {
super(matchKind);
}
@Override
public void accept(int t) {
if (!stop && predicate.test(t) == matchKind.stopOnPredicateMatches) {
stop = true;
value = matchKind.shortCircuitResult;
}
}
}
return new MatchOp<>(StreamShape.INT_VALUE, matchKind, MatchSink::new);
}
示例3: filter
import java.util.function.IntPredicate; //導入依賴的package包/類
@Override
public final IntStream filter(IntPredicate predicate) {
Objects.requireNonNull(predicate);
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
StreamOpFlag.NOT_SIZED) {
@Override
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
return new Sink.ChainedInt<Integer>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(int t) {
if (predicate.test(t))
downstream.accept(t);
}
};
}
};
}
示例4: takeIntWhile
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
* Adapted from StackOverflow {@linkplain so http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate}
*
* @param splitr the original Spliterator
* @param predicate the predicate
* @return a Spliterator.OfInt
*/
private static Spliterator.OfInt takeIntWhile(Spliterator.OfInt splitr, IntPredicate predicate) {
return new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
boolean stillGoing = true;
@Override
public boolean tryAdvance(IntConsumer consumer) {
if (stillGoing) {
boolean hadNext = splitr.tryAdvance((int elem) -> {
if (predicate.test(elem)) {
consumer.accept(elem);
} else {
stillGoing = false;
}
});
return hadNext && stillGoing;
}
return false;
}
};
}
示例5: estimateSize
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
* Estimates the size of a filter applied to an IMembershipSet
* @return an approximation of the size based on sampling. May return 0.
* There are no strict guarantees on the quality of the approximation,
* but is good enough for initialization of a hash table sizes.
*/
public static int estimateSize(final IMembershipSet baseMap,
final IntPredicate filter) {
final IMembershipSet sampleSet = baseMap.sample(sizeEstimationSampleSize, 0);
if (sampleSet.getSize() == 0)
return 0;
int eSize = 0;
final IRowIterator iter = sampleSet.getIterator();
int curr = iter.getNextRow();
while (curr >= 0) {
if (filter.test(curr))
eSize++;
curr = iter.getNextRow();
}
return (baseMap.getSize() * eSize) / sampleSet.getSize();
}
示例6: shouldWrapExecutionExceptionInMemoizationException
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
* @throws ExecutionException
* Added for the call to 'cache.get(..)'.
*/
@Test
@SuppressWarnings(CompilerWarnings.UNCHECKED)
public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException {
// given
final IntPredicate predicate = a -> true;
final IntFunction<String> keyFunction = a -> "key";
final Cache<String, Boolean> cache = Mockito.mock(Cache.class);
given(cache.get(any(), any())).willThrow(ExecutionException.class);
final GuavaCacheBasedIntPredicateMemoizer<String> memoizer = new GuavaCacheBasedIntPredicateMemoizer<>(
cache, keyFunction, predicate);
// when
thrown.expect(MemoizationException.class);
// then
memoizer.test(123);
}
示例7: shouldRequireNonNullPredicate
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
*
*/
@Test
@SuppressWarnings(CompilerWarnings.UNUSED)
public void shouldRequireNonNullPredicate() {
// given
final ConcurrentMap<Integer, Boolean> cache = new ConcurrentHashMap<>();
final IntPredicate predicate = null;
final IntFunction<Integer> keyFunction = Integer::valueOf;
// when
thrown.expect(NullPointerException.class);
thrown.expectMessage("Cannot memoize a NULL Predicate - provide an actual Predicate to fix this.");
// then
new ConcurrentMapBasedIntPredicateMemoizer<>(cache, keyFunction, predicate);
}
示例8: shouldTestGivenValue
import java.util.function.IntPredicate; //導入依賴的package包/類
/**
*
*/
@Test
public void shouldTestGivenValue() {
// given
final IntPredicate predicate = Mockito.mock(IntPredicate.class);
final IntFunction<String> keyFunction = a -> "key";
final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build();
// when
final GuavaCacheBasedIntPredicateMemoizer<String> memoizer = new GuavaCacheBasedIntPredicateMemoizer<>(
cache, keyFunction, predicate);
// then
memoizer.test(123);
Mockito.verify(predicate).test(123);
}
示例9: scanAllVersion
import java.util.function.IntPredicate; //導入依賴的package包/類
void scanAllVersion(IntPredicate predicate) {
SkipListScanner scanner = this.slist.scan(0, true, 0, true);
if (scanner == null) {
return;
}
while (scanner.next()) {
long pHead = scanner.getValuePointer();
if (pHead == 0) {
continue;
}
int oHead = Unsafe.getInt(pHead);
if (oHead == 0) {
continue;
}
for (ListNode i=ListNode.create(this.base, oHead); i!=null; i=i.getNextNode()) {
boolean cont = predicate.test(i.getOffset());
if (!cont) {
return;
}
}
}
}
示例10: assertIntPredicates
import java.util.function.IntPredicate; //導入依賴的package包/類
private void assertIntPredicates(Supplier<IntStream> source, Kind kind, IntPredicate[] predicates, boolean... answers) {
for (int i = 0; i < predicates.length; i++) {
setContext("i", i);
boolean match = intKinds.get(kind).apply(predicates[i]).apply(source.get());
assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
}
}
示例11: testIntStream
import java.util.function.IntPredicate; //導入依賴的package包/類
@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
public void testIntStream(String name, TestData.OfInt data) {
for (IntPredicate p : INT_PREDICATES) {
setContext("p", p);
for (Kind kind : Kind.values()) {
setContext("kind", kind);
exerciseTerminalOps(data, intKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(ipFalse), intKinds.get(kind).apply(p));
exerciseTerminalOps(data, s -> s.filter(ipEven), intKinds.get(kind).apply(p));
}
}
}
示例12: checkAndSetOrder
import java.util.function.IntPredicate; //導入依賴的package包/類
private void checkAndSetOrder(IntPredicate expectedValue,
IntUnaryOperator newValue) {
if (!expectedValue.test(invocationOrder)) {
throw new TestSupport.AssertionFailedException(
expectedValue + " -> " + newValue);
}
invocationOrder = newValue.applyAsInt(invocationOrder);
}
示例13: should_verify_an_actual_int_predicate_is_conform_to_an_expected_result
import java.util.function.IntPredicate; //導入依賴的package包/類
@Test
public void should_verify_an_actual_int_predicate_is_conform_to_an_expected_result() {
assertThat(resultOf(() -> {
gwtMock.whenAnEventHappensInRelationToAnActionOfTheConsumer();
return (IntPredicate) number -> number < 100;
}).accepts(99)).hasSameClassAs(assertThat((IntPredicate) t -> t == 0));
}
示例14: RangeOfInts
import java.util.function.IntPredicate; //導入依賴的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
*/
RangeOfInts(int start, int end, int step, IntPredicate excludes) {
super(start, end);
this.start = start;
this.end = end;
this.step = step;
this.ascend = start <= end;
this.excludes = excludes;
}
示例15: filterNumbersWithPredicate
import java.util.function.IntPredicate; //導入依賴的package包/類
public List<Integer> filterNumbersWithPredicate(final List<Integer> numbers,
IntPredicate predicate) {
List<Integer> filteredNumbers = new ArrayList<>();
for (Integer number : numbers) {
if (predicate.test(number)) {
filteredNumbers.add(number);
}
}
return filteredNumbers;
}