Example usage for java.util.function Predicate test

List of usage examples for java.util.function Predicate test

Introduction

In this page you can find the example usage for java.util.function Predicate test.

Prototype

boolean test(T t);

Source Link

Document

Evaluates this predicate on the given argument.

Usage

From source file:events.Utils.java

public static <V> Function<ChangePair<V>, ChangeEvent<V>> PairToChangeEvent(Predicate<V> p) {
    return t -> {
        boolean old = t.getOld() != null && p.test(t.getOld());
        boolean newVal = t.getNew() != null && p.test(t.getNew());
        if (old && !newVal)
            return new ChangeEvent(ChangeEvent.ChangeType.delete, t.getOld());
        if (newVal)
            return new ChangeEvent(ChangeEvent.ChangeType.update, t.getNew());
        return null;
    };/*from  ww w  . j  ava 2  s .c om*/
}

From source file:Main.java

/**
 * Find an ancestor of a component that matches a condition.
 * @param component the starting component
 * @param condition the condition to match
 * @return the first ancestor for which {@code condition} is true
 *///from   w w w  .j a va2s  .  co  m
public static Optional<Component> findAncestor(Component component, Predicate<Component> condition) {
    while (component != null && !condition.test(component)) {
        component = getParent(component);
    }
    return Optional.ofNullable(component);
}

From source file:Main.java

public static <E> List<E> filter(List<E> elements, Predicate<E> filter) {
    List<E> result = new ArrayList<>();
    for (E element : elements) {
        if (filter.test(element)) {
            result.add(element);/*from   w ww  .  j  a  v a2s  .c  om*/
        }
    }
    return result;
}

From source file:Main.java

private static List<Student> findStudents(List<Student> employees, Predicate<Student> condition) {
    List<Student> result = new ArrayList<>();

    for (Student e : employees) {
        if (condition.test(e)) {
            result.add(e);/*w  ww.  j  a  v a  2  s  .  com*/
        }
    }
    return result;
}

From source file:Main.java

private static List<Student> findStudents(List<Student> employees, Predicate<Student> condition) {
    List<Student> result = new ArrayList<>();

    for (Student e : employees) {
        if (condition.test(e)) {
            result.add(e);/*from  w w w .  ja  v a 2s  .  c  o m*/
        }
    }

    return result;
}

From source file:Main.java

public static List<Box> filter(List<Box> inventory, Predicate<Box> p) {
    List<Box> result = new ArrayList<>();
    for (Box apple : inventory) {
        if (p.test(apple)) {
            result.add(apple);/*from w  w w .j  a  va2 s . c  o  m*/
        }
    }
    return result;
}

From source file:Main.java

/**
 * @return the first element to match given predicate, null otherwise.
 *//*w w w .  java  2  s .co  m*/
public static <T> T findElement(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
    while (iterator.hasNext()) {
        T element = iterator.next();
        if (predicate.test(element)) {
            return element;
        }
    }
    return null;
}

From source file:Main.java

/**
 * /* w  w w .  j a va  2  s . c  o m*/
 * <pre>
 * {@code
 * filter(listOfStrings, (String str) -> !str.isEmpty());
 * 
 * IntPredicate evnNumbers = (int i) -> i % 2 == 0; //to avoid autoboxing rather than (Integer i) -> i % 2 == 0;
 * filter(listOfIntegers, evenNumbers);
 * }
 * </pre>
 * 
 * @param list
 * @param predicate
 * @return
 */
public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
    List<T> filtered = new LinkedList<>();
    for (T entry : list) {
        if (predicate.test(entry)) {
            filtered.add(entry);
        }
    }
    return filtered;
}

From source file:Main.java

public static void processMembersWithFunction(List<Student> roster, Predicate<Student> tester,
        Function<Student, String> mapper, Consumer<String> block) {
    for (Student p : roster) {
        if (tester.test(p)) {
            String data = mapper.apply(p);
            block.accept(data);/*from  w w w .  j  ava2 s .  c  o  m*/
        }
    }
}

From source file:Main.java

/**
 * @return the index in the iteration order of the first element that matches given predicate, or -1 otherwise.
 *///  w w  w  .j av a2 s .  co m
public static <T> int indexUntil(Iterator<? extends T> iterator, Predicate<T> predicate) {
    int ix = 0;
    while (iterator.hasNext()) {
        T element = iterator.next();
        if (predicate.test(element)) {
            return ix;
        }
        ix++;
    }
    return -1;
}