Line | Hits | Source |
---|---|---|
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 | */ | |
35 | 0 | public 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 | { | |
47 | 7 | SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER); |
48 | 7 | if (sm != null) |
49 | { | |
50 | 4 | Set s = sm.getVertices(p); |
51 | 4 | if (s != null) |
52 | 4 | return s; |
53 | } | |
54 | ||
55 | 3 | Set s = new HashSet(); |
56 | 3 | Set vertices = g.getVertices(); |
57 | 3 | for (Iterator v_it = vertices.iterator(); v_it.hasNext(); ) |
58 | { | |
59 | 10 | ArchetypeVertex v = (ArchetypeVertex)v_it.next(); |
60 | 10 | if (p.evaluate(v)) |
61 | 1 | s.add(v); |
62 | } | |
63 | 3 | 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 | { | |
73 | 13 | SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER); |
74 | 13 | if (sm != null) |
75 | { | |
76 | 0 | Set s = sm.getEdges(p); |
77 | 0 | if (s != null) |
78 | 0 | return s; |
79 | } | |
80 | ||
81 | 13 | Set s = new HashSet(); |
82 | 13 | Set edges = g.getEdges(); |
83 | 13 | for (Iterator e_it = edges.iterator(); e_it.hasNext(); ) |
84 | { | |
85 | 66 | ArchetypeEdge e = (ArchetypeEdge)e_it.next(); |
86 | 66 | if (p.evaluate(e)) |
87 | 31 | s.add(e); |
88 | } | |
89 | 13 | 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 | { | |
100 | 0 | 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 | { | |
111 | 0 | 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 | { | |
121 | 0 | SubsetManager.getInstance(g).removeVertexSubset(p); |
122 | 0 | } |
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 | { | |
131 | 0 | SubsetManager.getInstance(g).removeEdgeSubset(p); |
132 | 0 | } |
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 | { | |
140 | 268478 | 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 | { | |
150 | 5 | if (PredicateUtils.enforcesEdgeConstraint(g, p)) |
151 | 0 | return true; |
152 | else | |
153 | 5 | 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 | { | |
162 | 7 | 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 | { | |
172 | 7 | if (PredicateUtils.enforcesVertexConstraint(g, p)) |
173 | 0 | return true; |
174 | else | |
175 | 7 | 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 | { | |
184 | 12 | for (Iterator iter = c.iterator(); iter.hasNext(); ) |
185 | { | |
186 | 32 | if (!p.evaluate(iter.next())) |
187 | 3 | return false; |
188 | } | |
189 | 9 | return true; |
190 | } | |
191 | ||
192 | public static Collection getSatisfyingElements(Collection c, Predicate p) | |
193 | { | |
194 | 0 | Collection satisfied = new LinkedList(); |
195 | 0 | for (Iterator iter = c.iterator(); iter.hasNext(); ) |
196 | { | |
197 | 0 | Object o = iter.next(); |
198 | 0 | if (p.evaluate(o)) |
199 | 0 | satisfied.add(o); |
200 | } | |
201 | 0 | 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 | { | |
210 | 17 | 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 | { | |
219 | 18 | 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 | { | |
229 | 23 | 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 | { | |
243 | 0 | Map evaluations = new HashMap(); |
244 | 0 | if (p instanceof PredicateDecorator) |
245 | { | |
246 | 0 | Predicate[] nested_preds = ((PredicateDecorator)p).getPredicates(); |
247 | 0 | for (int i = 0; i < nested_preds.length; i++) |
248 | 0 | evaluations.put(nested_preds[i], new Boolean(nested_preds[i].evaluate(o))); |
249 | } | |
250 | else | |
251 | 0 | evaluations.put(p, new Boolean(p.evaluate(o))); |
252 | 0 | return evaluations; |
253 | } | |
254 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |