Coverage details for edu.uci.ics.jung.utils.PredicateUtils

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  */
8 package edu.uci.ics.jung.utils;
9  
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.LinkedList;
16 import java.util.Map;
17 import java.util.Set;
18  
19 import org.apache.commons.collections.Predicate;
20 import org.apache.commons.collections.functors.PredicateDecorator;
21  
22 import edu.uci.ics.jung.graph.ArchetypeEdge;
23 import edu.uci.ics.jung.graph.ArchetypeGraph;
24 import edu.uci.ics.jung.graph.ArchetypeVertex;
25 import edu.uci.ics.jung.graph.Graph;
26  
27  
28 /**
29  * Convenience methods for handling Predicates in JUNG (as constraints,
30  * as subset specifications, and in general). Not a replacement
31  * for the Jakarta Commons-Collections <code>PredicateUtils</code> class.
32  *
33  * @author Joshua O'Madadhain
34  */
350public class PredicateUtils
36 {
37     /**
38      * <p>Returns a <code>Set</code> consisting of all vertices <code>v</code>
39      * in graph <code>g</code> that satisfy predicate <code>p</code>,
40      * that is, those for which <code>p.evaluate(v)</code> returns true.</p>
41      *
42      * <p>If <code>g</code> has a <code>SubsetManager</code> that defines
43      * a cached subset based on <code>p</code>, that subset is returned.
44      */
45     public static Set getVertices(ArchetypeGraph g, Predicate p)
46     {
477        SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);
487        if (sm != null)
49         {
504            Set s = sm.getVertices(p);
514            if (s != null)
524                return s;
53         }
54         
553        Set s = new HashSet();
563        Set vertices = g.getVertices();
573        for (Iterator v_it = vertices.iterator(); v_it.hasNext(); )
58         {
5910            ArchetypeVertex v = (ArchetypeVertex)v_it.next();
6010            if (p.evaluate(v))
611                s.add(v);
62         }
633        return Collections.unmodifiableSet(s);
64     }
65     
66     /**
67      * Returns a <code>Set</code> consisting of all edges <code>e</code>
68      * in graph <code>g</code> that satisfy predicate <code>p</code>,
69      * that is, those for which <code>p.evaluate(e)</code> returns true.
70      */
71     public static Set getEdges(ArchetypeGraph g, Predicate p)
72     {
7313        SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);
7413        if (sm != null)
75         {
760            Set s = sm.getEdges(p);
770            if (s != null)
780                return s;
79         }
80         
8113        Set s = new HashSet();
8213        Set edges = g.getEdges();
8313        for (Iterator e_it = edges.iterator(); e_it.hasNext(); )
84         {
8566            ArchetypeEdge e = (ArchetypeEdge)e_it.next();
8666            if (p.evaluate(e))
8731                s.add(e);
88         }
8913        return Collections.unmodifiableSet(s);
90     }
91     
92     /**
93      * Creates a vertex subset for <code>g</code> based on <code>p</code>, which will
94      * be maintained by the <code>g</code>'s <code>SubsetManager</code>.
95      * @param p the predicate defining the subset
96      * @return true if a subset was created; false if the subset already existed
97      */
98     public static boolean addVertexSubset(ArchetypeGraph g, Predicate p)
99     {
1000        return SubsetManager.getInstance(g).addVertexSubset(p);
101     }
102  
103     /**
104      * Creates an edge subset for <code>g</code> based on <code>p</code>, which will
105      * be maintained by the <code>g</code>'s <code>SubsetManager</code>.
106      * @param p the predicate defining the subset
107      * @return true if a subset was created; false if the subset already existed
108      */
109     public static boolean addEdgeSubset(ArchetypeGraph g, Predicate p)
110     {
1110        return SubsetManager.getInstance(g).addEdgeSubset(p);
112     }
113     
114     /**
115      * Removes the vertex subset based on <code>p</code> from
116      * <code>g</code>'s <code>SubsetManager</code>.
117      * @param p the predicate defining the subset
118      */
119     public static void removeVertexSubset(ArchetypeGraph g, Predicate p)
120     {
1210        SubsetManager.getInstance(g).removeVertexSubset(p);
1220    }
123  
124     /**
125      * Removes the edge subset based on <code>p</code> from
126      * <code>g</code>'s <code>SubsetManager</code>.
127      * @param p the predicate defining the subset
128      */
129     public static void removeEdgeSubset(ArchetypeGraph g, Predicate p)
130     {
1310        SubsetManager.getInstance(g).removeEdgeSubset(p);
1320    }
133  
134     /**
135      * Returns <code>true</code> if <code>p</code> is an edge
136      * constraint of <code>g</code>, and <code>false</code> otherwise.
137      */
138     public static boolean enforcesEdgeConstraint(ArchetypeGraph g, Predicate p)
139     {
140268478        return g.getEdgeConstraints().contains(p);
141     }
142  
143     /**
144      * Returns <code>true</code> if each edge in <code>g</code>
145      * satisfies <code>p</code>, and false otherwise. (Note: this may be
146      * true even if <code>p</code> is not a constraint of <code>g</code>.)
147      */
148     public static boolean satisfiesEdgeConstraint(ArchetypeGraph g, Predicate p)
149     {
1505        if (PredicateUtils.enforcesEdgeConstraint(g, p))
1510            return true;
152         else
1535            return satisfiesPredicate(g.getEdges(), p);
154     }
155  
156     /**
157      * Returns <code>true</code> if <code>p</code> is an edge
158      * constraint of <code>g</code>, and <code>false</code> otherwise.
159      */
160     public static boolean enforcesVertexConstraint(ArchetypeGraph g, Predicate p)
161     {
1627        return g.getVertexConstraints().contains(p);
163     }
164  
165     /**
166      * Returns <code>true</code> if each vertex in <code>g</code>
167      * satisfies <code>p</code>, and false otherwise. (Note: this may be
168      * true even if <code>p</code> is not a constraint of <code>g</code>.)
169      */
170     public static boolean satisfiesVertexConstraint(ArchetypeGraph g, Predicate p)
171     {
1727        if (PredicateUtils.enforcesVertexConstraint(g, p))
1730            return true;
174         else
1757            return satisfiesPredicate(g.getVertices(), p);
176     }
177  
178     /**
179      * Returns <code>true</code> if all elements of <code>c</code>
180      * satisfy <code>p</code>.
181      */
182     public static boolean satisfiesPredicate(Collection c, Predicate p)
183     {
18412        for (Iterator iter = c.iterator(); iter.hasNext(); )
185         {
18632            if (!p.evaluate(iter.next()))
1873                return false;
188         }
1899        return true;
190     }
191     
192     public static Collection getSatisfyingElements(Collection c, Predicate p)
193     {
1940        Collection satisfied = new LinkedList();
1950        for (Iterator iter = c.iterator(); iter.hasNext(); )
196         {
1970            Object o = iter.next();
1980            if (p.evaluate(o))
1990                satisfied.add(o);
200         }
2010        return satisfied;
202     }
203     
204     /**
205      * Returns <code>true</code> if <code>g</code> is constrained to only
206      * accept directed edges, and false otherwise.
207      */
208     public static boolean enforcesDirected(Graph g)
209     {
21017        return g.getEdgeConstraints().contains(Graph.DIRECTED_EDGE);
211     }
212  
213     /**
214      * Returns <code>true</code> if <code>g</code> is constrained to only
215      * accept undirected edges.
216      */
217     public static boolean enforcesUndirected(Graph g)
218     {
21918        return g.getEdgeConstraints().contains(Graph.UNDIRECTED_EDGE);
220     }
221     
222     /**
223      * Returns <code>true</code> if <code>g</code> is constrained to
224      * reject parallel edges.
225      * @see edu.uci.ics.jung.graph.predicates.ParallelEdgePredicate
226      */
227     public static boolean enforcesNotParallel(Graph g)
228     {
22923        return g.getEdgeConstraints().contains(Graph.NOT_PARALLEL_EDGE);
230     }
231     
232     /**
233      * Returns a <code>Map</code> of each constituent predicate of <code>p</code>
234      * (if any) to the result of evaluating this predicate on <code>o</code>.
235      * If <code>p</code> is a <code>PredicateDecorator</code>, i.e., a predicate
236      * that operates on other <code>Predicate</code>s, the output will consist of
237      * the results of evaluting the constituents of <code>p</code> on <code>o</code>;
238      * otherwise, the output will be the result of evaluating <code>p</code> itself
239      * on <code>o</code>.
240      */
241     public static Map evaluateNestedPredicates(Predicate p, Object o)
242     {
2430        Map evaluations = new HashMap();
2440        if (p instanceof PredicateDecorator)
245         {
2460            Predicate[] nested_preds = ((PredicateDecorator)p).getPredicates();
2470            for (int i = 0; i < nested_preds.length; i++)
2480                evaluations.put(nested_preds[i], new Boolean(nested_preds[i].evaluate(o)));
249         }
250         else
2510            evaluations.put(p, new Boolean(p.evaluate(o)));
2520        return evaluations;
253     }
254 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.