Java Code Examples for java.util.function.IntPredicate#test()

The following examples show how to use java.util.function.IntPredicate#test() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntPipeline.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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);
                }
            };
        }
    };
}
 
Example 2
Source File: IntPipeline.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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);
                }
            };
        }
    };
}
 
Example 3
Source File: MatchOps.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: MatchOps.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: IntPipeline.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@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);
                }
            };
        }
    };
}
 
Example 6
Source File: MatchOps.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: IntPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@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);
                }
            };
        }
    };
}
 
Example 8
Source File: MatchOps.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: IntPipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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);
                }
            };
        }
    };
}
 
Example 10
Source File: NumeralGenerator.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
public static LogicalConstant getIntegerConstant(String string,
		IntPredicate filter) {
	if (NUMBER.matches(string) && filter.test(Integer.valueOf(string))) {
		return LogicalConstant.create(Integer.valueOf(string).toString(),
				LogicLanguageServices.getNumeralType(), true);
	} else {
		return null;
	}
}
 
Example 11
Source File: SimpleUnicodeRegExpMatcher.java    From es6draft with MIT License 5 votes vote down vote up
private static Term star(IntPredicate predicate) {
    return (string, start) -> {
        int i = start;
        while (i < string.length()) {
            int codePoint = string.codePointAt(i);
            if (predicate.test(codePoint)) {
                i += Character.charCount(codePoint);
                continue;
            }
            break;
        }
        return Optional.of(new MatchRegion(start, i));
    };
}
 
Example 12
Source File: CalciteSystemProperty.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the system property with the specified name as int, or
 * the <code>defaultValue</code> if any of the conditions below hold:
 *
 * <ol>
 * <li>the property is not defined;
 * <li>the property value cannot be transformed to an int;
 * <li>the property value does not satisfy the checker.
 * </ol>
 */
private static CalciteSystemProperty<Integer> intProperty(String key, int defaultValue,
    IntPredicate valueChecker) {
  return new CalciteSystemProperty<>(key, v -> {
    if (v == null) {
      return defaultValue;
    }
    try {
      int intVal = Integer.parseInt(v);
      return valueChecker.test(intVal) ? intVal : defaultValue;
    } catch (NumberFormatException nfe) {
      return defaultValue;
    }
  });
}
 
Example 13
Source File: DefaultDataBuffer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public int lastIndexOf(IntPredicate predicate, int fromIndex) {
	Assert.notNull(predicate, "IntPredicate must not be null");
	int i = Math.min(fromIndex, this.writePosition - 1);
	for (; i >= 0; i--) {
		byte b = this.byteBuffer.get(i);
		if (predicate.test(b)) {
			return i;
		}
	}
	return -1;
}
 
Example 14
Source File: ReaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void checkAndSetOrder(IntPredicate expectedValue,
                              IntUnaryOperator newValue) {
    if (!expectedValue.test(invocationOrder)) {
        throw new TestSupport.AssertionFailedException(
                expectedValue + " -> " + newValue);
    }
    invocationOrder = newValue.applyAsInt(invocationOrder);
}
 
Example 15
Source File: OptionalUtils.java    From luna with MIT License 5 votes vote down vote up
/**
 * Filters an {@link OptionalInt}.
 *
 * @param optional The optional to filter.
 * @param predicate The predicate to filter with.
 * @return The optional if predicate passes, otherwise an empty optional.
 */
public static OptionalInt filter(OptionalInt optional, IntPredicate predicate) {
    if (optional.isPresent()) {
        int value = optional.getAsInt();
        if (predicate.test(value)) {
            return optional;
        }
    }
    return OptionalInt.empty();
}
 
Example 16
Source File: StringUtils.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Test that each char of specified string match for predicate. <p/>
 * Note that it method does not support unicode, because it usual applicable only for match letters that placed under 128 code.
 * @param str string
 * @param predicate char matcher
 * @return true if all chars match
 */
public static boolean match(String str, IntPredicate predicate) {
    final int len = str.length();
    if(len == 0) {
        return false;
    }
    for(int i = 0; i < len; i++) {
        if(!predicate.test(str.charAt(i))) {
            return false;
        }
    }
    return true;
}
 
Example 17
Source File: Annotation.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Override
public void trim(IntPredicate aIsTrimChar) {
  int begin = getBegin();
  int end = getEnd();
  String text = _casView.getDocumentText();
    
  // If the span is empty, or there is no text, there is nothing to trim
  if (begin == end || text == null) {
    return;
  }
  
  final int saved_begin = begin;
  final int saved_end = end;
  // First we trim at the end. If a trimmed span is empty, we want to return the original 
  // begin as the begin/end of the trimmed span
  int backwardsSeekingCodepoint;
  while (
            (end > 0)
            && end > begin
            && aIsTrimChar.test(backwardsSeekingCodepoint = text.codePointBefore(end))
  ) {
    end -= Character.charCount(backwardsSeekingCodepoint);
  }
  
  // Then, trim at the start
  int forwardSeekingCodepoint;
  while (
            (begin < (text.length() - 1))
            && begin < end
            && aIsTrimChar.test(forwardSeekingCodepoint = text.codePointAt(begin))
  ) {
    begin += Character.charCount(forwardSeekingCodepoint);
  }
    
  if (saved_begin != begin || saved_end != end) {
    int final_begin = begin;
    int final_end = end;
  
     _casView.protectIndexes(() -> {
        setBegin(final_begin);
        setEnd(final_end);
     });
  }
}
 
Example 18
Source File: CharTokenizer.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new instance of CharTokenizer with the supplied attribute factory using a custom predicate, supplied as method reference or lambda expression.
 * The predicate should return {@code true} for all valid token characters.
 * <p>
 * This factory is intended to be used with lambdas or method references. E.g., an elegant way
 * to create an instance which behaves exactly as {@link LetterTokenizer} is:
 * <pre class="prettyprint lang-java">
 * Tokenizer tok = CharTokenizer.fromTokenCharPredicate(factory, Character::isLetter);
 * </pre>
 */
public static CharTokenizer fromTokenCharPredicate(AttributeFactory factory, final IntPredicate tokenCharPredicate) {
  Objects.requireNonNull(tokenCharPredicate, "predicate must not be null.");
  return new CharTokenizer(factory) {
    @Override
    protected boolean isTokenChar(int c) {
      return tokenCharPredicate.test(c);
    }
  };
}
 
Example 19
Source File: Snippets.java    From 30-seconds-of-java8 with Creative Commons Zero v1.0 Universal 3 votes vote down vote up
/**
 * Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.
 *
 * @param elements
 * @param condition
 * @return
 */
public static int[] dropElements(int[] elements, IntPredicate condition) {
    while (elements.length > 0 && !condition.test(elements[0])) {
        elements = Arrays.copyOfRange(elements, 1, elements.length);
    }
    return elements;
}
 
Example 20
Source File: Util.java    From incubator-nemo with Apache License 2.0 3 votes vote down vote up
/**
 * Check the equality of two intPredicates.
 * Check if the both the predicates either passes together or fails together for each
 * integer in the range [0,noOfTimes]
 *
 * @param firstPredicate  the first IntPredicate.
 * @param secondPredicate the second IntPredicate.
 * @param noOfTimes       Number to check the IntPredicates from.
 * @return whether or not we can say that they are equal.
 */
public static boolean checkEqualityOfIntPredicates(final IntPredicate firstPredicate,
                                                   final IntPredicate secondPredicate, final int noOfTimes) {
  for (int value = 0; value <= noOfTimes; value++) {
    if (firstPredicate.test(value) != secondPredicate.test(value)) {
      return false;
    }
  }
  return true;
}