Example usage for org.apache.commons.collections Predicate evaluate

List of usage examples for org.apache.commons.collections Predicate evaluate

Introduction

In this page you can find the example usage for org.apache.commons.collections Predicate evaluate.

Prototype

public boolean evaluate(T object);

Source Link

Document

Use the specified parameter to perform a test that returns true or false.

Usage

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

/**
 * Returns <code>true</code> if all elements of <code>c</code>
 * satisfy <code>p</code>./*from w  w w  . j a  va  2 s .c o  m*/
 */
public static boolean satisfiesPredicate(Collection c, Predicate p) {
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        if (!p.evaluate(iter.next()))
            return false;
    }
    return true;
}

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

public static Collection getSatisfyingElements(Collection c, Predicate p) {
    Collection satisfied = new LinkedList();
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        Object o = iter.next();//from  w ww.j a v  a 2  s.  com
        if (p.evaluate(o))
            satisfied.add(o);
    }
    return satisfied;
}

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

/**
 * Returns a <code>Map</code> of each constituent predicate of <code>p</code>
 * (if any) to the result of evaluating this predicate on <code>o</code>.  
 * If <code>p</code> is a <code>PredicateDecorator</code>, i.e., a predicate
 * that operates on other <code>Predicate</code>s, the output will consist of
 * the results of evaluting the constituents of <code>p</code> on <code>o</code>;
 * otherwise, the output will be the result of evaluating <code>p</code> itself
 * on <code>o</code>./*from  w ww.  j a v  a 2s .c o  m*/
 */
public static Map evaluateNestedPredicates(Predicate p, Object o) {
    Map evaluations = new HashMap();
    if (p instanceof PredicateDecorator) {
        Predicate[] nested_preds = ((PredicateDecorator) p).getPredicates();
        for (int i = 0; i < nested_preds.length; i++)
            evaluations.put(nested_preds[i], new Boolean(nested_preds[i].evaluate(o)));
    } else
        evaluations.put(p, new Boolean(p.evaluate(o)));
    return evaluations;
}

From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

/**
 * Filter a list based on a {@link Predicate}
 *
 * @param <T>/*from w  w  w.  j  a  v  a  2s  . c  o m*/
 * @param target
 * @param predicate
 * @return
 */
public static <T> List<T> filterList(final Collection<T> target, final Predicate predicate) {
    final List<T> result = new ArrayList<T>();
    for (final T element : target) {
        if (predicate.evaluate(element)) {
            result.add(element);
        }
    }
    return result;
}

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

/**
 * Returns a <code>Set</code> consisting of all edges <code>e</code> 
 * in graph <code>g</code> that satisfy predicate <code>p</code>,
 * that is, those for which <code>p.evaluate(e)</code> returns true.
 *//*from  w  w w . j av a2 s  .  c  o  m*/
public static Set getEdges(ArchetypeGraph g, Predicate p) {
    SubsetManager sm = (SubsetManager) g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);
    if (sm != null) {
        Set s = sm.getEdges(p);
        if (s != null)
            return s;
    }

    Set s = new HashSet();
    Set edges = g.getEdges();
    for (Iterator e_it = edges.iterator(); e_it.hasNext();) {
        ArchetypeEdge e = (ArchetypeEdge) e_it.next();
        if (p.evaluate(e))
            s.add(e);
    }
    return Collections.unmodifiableSet(s);
}

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

/**
 * <p>Returns a <code>Set</code> consisting of all vertices <code>v</code> 
 * in graph <code>g</code> that satisfy predicate <code>p</code>,
 * that is, those for which <code>p.evaluate(v)</code> returns true.</p>
 * //from   w  w w .  ja  v a  2 s  .c o m
 * <p>If <code>g</code> has a <code>SubsetManager</code> that defines
 * a cached subset based on <code>p</code>, that subset is returned.
 */
public static Set getVertices(ArchetypeGraph g, Predicate p) {
    SubsetManager sm = (SubsetManager) g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);
    if (sm != null) {
        Set s = sm.getVertices(p);
        if (s != null)
            return s;
    }

    Set s = new HashSet();
    Set vertices = g.getVertices();
    for (Iterator v_it = vertices.iterator(); v_it.hasNext();) {
        ArchetypeVertex v = (ArchetypeVertex) v_it.next();
        if (p.evaluate(v))
            s.add(v);
    }
    return Collections.unmodifiableSet(s);
}

From source file:module.siadap.domain.ExceedingQuotaProposal.java

private static List<ExceedingQuotaProposal> getQuotaProposalsByPredicate(
        Collection<ExceedingQuotaProposal> quotaProposalsToFilter, Predicate filterPredicate) {
    List<ExceedingQuotaProposal> exceedingQuotaProposalsToReturn = new ArrayList<ExceedingQuotaProposal>();
    for (ExceedingQuotaProposal proposal : quotaProposalsToFilter) {
        if (filterPredicate.evaluate(proposal)) {
            exceedingQuotaProposalsToReturn.add(proposal);
        }/*from  w w  w. j av a2 s.  c o m*/
    }

    return exceedingQuotaProposalsToReturn;

}

From source file:com.dtolabs.utils.Mapper.java

/**
 * Return a mapper than maps an object to itself if the predicate evaluates to true,
 * and to null otherwise./*from  w w  w .j a  va2 s.co  m*/
 * @param pred
 * @return
 */
public static Mapper filterMapper(final Predicate pred) {
    return new Mapper() {
        public Object map(Object a) {
            return pred.evaluate(a) ? a : null;
        }
    };
}

From source file:edu.uci.ics.jung.graph.predicates.KPartiteEdgePredicate.java

private Predicate getSatisfyingPredicate(Vertex v) {
    for (Iterator p_iter = vertex_partitions.iterator(); p_iter.hasNext();) {
        Predicate p = (Predicate) p_iter.next();
        if (p.evaluate(v))
            return p;
    }/*from   www.  jav  a 2  s.c o m*/
    return null;
}

From source file:lt.kape1395.jenkins.ditz.model.IssueOpenPredicateTest.java

/**
 * By all statuses. /*from   w  w  w . j av a 2  s  . co m*/
 */
@Test
public void testEvaluate() {
    Predicate p = new IssueOpenPredicate();

    assertThat(p.evaluate(new Issue("a", "b", "c", Status.CLOSED, "d")), is(false));
    assertThat(p.evaluate(new Issue("a", "b", "c", Status.IN_PROGRESS, "d")), is(true));
    assertThat(p.evaluate(new Issue("a", "b", "c", Status.PAUSED, "d")), is(true));
    assertThat(p.evaluate(new Issue("a", "b", "c", Status.UNSTARTED, "d")), is(true));
    assertThat(p.evaluate(new Issue("a", "b", "c", "some", "d")), is(true));
}