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:Main.java

public static <T> T removeFrom(Iterable<T> collection, Predicate<T> predicate) {
    Iterator<T> it = collection.iterator();

    while (it.hasNext()) {
        T o = it.next();//from   www  .ja  v  a 2  s .c o m

        if (predicate.test(o)) {
            it.remove();
            return o;
        }
    }

    return null;
}

From source file:Main.java

/** Remove from given list all elements that match given predicate. */
public static <E, L extends Iterable<E>> L filter(L list, Predicate<E> predicate) {
    for (Iterator<? extends E> iter = list.iterator(); iter.hasNext();) {
        E obj = iter.next();/*from   w w w  . ja  va2  s  . c o  m*/
        if (predicate.test(obj)) {
            iter.remove();
        }
    }
    return list;
}

From source file:Main.java

/** Removes from given list the first element that matches given predicate. 
 * @return true if an element was removed, false otherwise. */
public static <E> boolean removeElement(List<? extends E> list, Predicate<E> predicate) {
    for (Iterator<? extends E> iter = list.iterator(); iter.hasNext();) {
        E obj = iter.next();/*from   w  w  w. jav  a 2  s .  c  o m*/
        if (predicate.test(obj)) {
            iter.remove();
            return true;
        }
    }
    return false;
}

From source file:Main.java

private static int fibonacciOperation(Predicate<Integer> condition, BinaryOperator<Integer> operation) {

    int i = 0;/*from   w w w .ja v a 2 s  .co m*/
    int result = 0;
    int fibo = fibonacci(i);
    for (int temp = operation.apply(result, fibo); condition
            .test(temp); fibo = fibonacci(i), temp = operation.apply(result, fibo)) {
        System.out.println(fibo + " ");
        result = temp;
        i++;
    }
    return result;
}

From source file:org.apache.solr.security.TestAuthorizationFramework.java

public static void verifySecurityStatus(HttpClient cl, String url, String objPath, Object expected, int count)
        throws Exception {
    boolean success = false;
    String s = null;//from  w  w  w.  j av a  2 s .c  om
    List<String> hierarchy = StrUtils.splitSmart(objPath, '/');
    for (int i = 0; i < count; i++) {
        HttpGet get = new HttpGet(url);
        s = EntityUtils
                .toString(cl.execute(get, HttpClientUtil.createNewHttpClientRequestContext()).getEntity());
        Map m = (Map) Utils.fromJSONString(s);

        Object actual = Utils.getObjectByPath(m, true, hierarchy);
        if (expected instanceof Predicate) {
            Predicate predicate = (Predicate) expected;
            if (predicate.test(actual)) {
                success = true;
                break;
            }
        } else if (Objects.equals(String.valueOf(actual), expected)) {
            success = true;
            break;
        }
        Thread.sleep(50);
    }
    assertTrue("No match for " + objPath + " = " + expected + ", full response = " + s, success);

}

From source file:net.gtaun.wl.race.util.TrackUtils.java

public static List<Track> filterTracks(List<Track> tracks, Predicate<Track> filter) {
    List<Track> filteredTracks = new ArrayList<>();
    for (Track track : tracks)
        if (filter.test(track))
            filteredTracks.add(track);/*from  w  w w  .  j a va  2  s  .c om*/
    return filteredTracks;
}

From source file:org.zalando.logbook.JsonHttpLogFormatter.java

private static <T> void addUnless(final Map<String, Object> target, final String key, final T element,
        final Predicate<T> predicate) {

    if (!predicate.test(element)) {
        target.put(key, element);//from  w w w  .j  a  va2  s . co  m
    }
}

From source file:Main.java

/**
 * Filter the collection by applying a Predicate to each element. If the
 * predicate returns false, remove the element. If the input collection or
 * predicate is null, there is no change made.
 *
 * @param input/*from  w  w w.  ja v  a2 s  . c om*/
 * @param predicate
 * @param <V>
 * @return
 */
public static <V> void filter(Collection<V> input, Predicate<V> predicate) {

    Iterator<V> iterator = input.iterator();

    while (iterator.hasNext()) {

        V value = iterator.next();

        if (!predicate.test(value)) {
            iterator.remove();
        }

    }

}

From source file:Main.java

public static <T> int removeIf(final List<T> values, final Predicate<T> predicate) {
    int size = values.size();
    int total = 0;

    for (int i = 0; i < size;) {
        final T value = values.get(i);
        if (predicate.test(value)) {
            values.remove(i);//from  w  w w.  j  av a 2s.  c o  m
            total++;
            size--;
        } else {
            i++;
        }
    }

    return total;
}

From source file:Main.java

/**
 * Splits list into several sub-lists according to predicate.
 * @param <T>//w  w  w . j a  v a2  s  .  c om
 * @param list
 * @param predicate
 * @return 
 */
public static final <T> List<List<T>> split(List<T> list, Predicate<T> predicate) {
    if (list.isEmpty()) {
        return Collections.EMPTY_LIST;
    }
    List<List<T>> lists = new ArrayList<>();
    boolean b = predicate.test(list.get(0));
    int len = list.size();
    int start = 0;
    for (int ii = 1; ii < len; ii++) {
        boolean t = predicate.test(list.get(ii));
        if (b != t) {
            lists.add(list.subList(start, ii));
            start = ii;
            b = t;
        }
    }
    lists.add(list.subList(start, len));
    return lists;
}