# Coverage details for edu.uci.ics.jung.utils.SubsetManager

Line | Hits | Source
--- | --- | ---
1 |  | `/*`
2 |  | `* Created on Apr 22, 2004`
3 |  | `*`
4 |  | `* Copyright (c) 2004, the JUNG Project and the Regents of the University`
5 |  | `* of California`
6 |  | `* All rights reserved.`
7 |  | `*`
8 |  | `* This software is open-source under the BSD license; see either`
9 |  | `* "license.txt" or`
10 |  | `* http://jung.sourceforge.net/license.txt for a description.`
11 |  | `*/`
12 |  | `package edu.uci.ics.jung.utils;`
13 |  | ``
14 |  | `import java.util.Collections;`
15 |  | `import java.util.HashMap;`
16 |  | `import java.util.HashSet;`
17 |  | `import java.util.Iterator;`
18 |  | `import java.util.Map;`
19 |  | `import java.util.Set;`
20 |  | ``
21 |  | `import org.apache.commons.collections.Predicate;`
22 |  | ``
23 |  | `import edu.uci.ics.jung.graph.ArchetypeEdge;`
24 |  | `import edu.uci.ics.jung.graph.ArchetypeGraph;`
25 |  | `import edu.uci.ics.jung.graph.ArchetypeVertex;`
26 |  | `import edu.uci.ics.jung.graph.event.GraphEvent;`
27 |  | `import edu.uci.ics.jung.graph.event.GraphEventListener;`
28 |  | `import edu.uci.ics.jung.graph.event.GraphEventType;`
29 |  | ``
30 |  | ``
31 |  | `/**`
32 |  | `* <p>A class which allows users to create and maintain`
33 |  | `* <code>Predicate</code>-specified vertex and edge subsets.`
34 |  | `* The subsets are automatically maintained as vertices and`
35 |  | `* edges are added to, and removed from, the constructor-specified`
36 |  | `* graph.</p>`
37 |  | `*`
38 |  | `* <p>A subset is created by providing a <code>Predicate</code>;`
39 |  | `* those graph elements that pass the predicate are added to the`
40 |  | `* subset.</p>`
41 |  | `*`
42 |  | `* @author Joshua O'Madadhain`
43 |  | `*/`
44 |  | `public class SubsetManager implements GraphEventListener`
45 |  | `{`
46 |  | `/**`
47 |  | `* Map specifications (in the form of Predicates) to the corresponding subsets.`
48 |  | `* These are only instantiated if a subset of the appropriate type is created.`
49 |  | `*/`
50 |  | `protected Map vertexMap;`
51 |  | `protected Map edgeMap;`
52 |  | ``
53 |  | `/**`
54 |  | `* The graph for which this instance manages subsets.`
55 |  | `*/`
56 |  | `protected ArchetypeGraph g;`
57 |  | ``
58 |  | `/**`
59 |  | `* Creates a <code>SubsetManager</code>, adds it to the specified`
60 |  | `* graph's user data repository, and adds itself as a listener to`
61 |  | `* the graph's vertex and edge addition and removal events, so that the`
62 |  | `* subsets' memberships can be maintained.`
63 |  | `*/`
64 |  | `protected SubsetManager(ArchetypeGraph g)`
65 |  | `{`
66 | 18 | `super();`
67 | 18 | `g.addUserDatum(ArchetypeGraph.SUBSET_MANAGER, this, UserData.REMOVE);`
68 | 18 | `g.addListener(this, GraphEventType.ALL_SINGLE_EVENTS);`
69 | 18 | `this.g = g;`
70 | 18 | `}`
71 |  | ``
72 |  | `/**`
73 |  | `* Gets this graph's <code>SubsetManager</code>, creating it if necessary.`
74 |  | `*`
75 |  | `* @param g the graph whose subset manager is requested`
76 |  | `*/`
77 |  | `public static SubsetManager getInstance(ArchetypeGraph g)`
78 |  | `{`
79 | 18 | `SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);`
80 | 18 | `if (sm == null)`
81 | 18 | `sm = new SubsetManager(g);`
82 | 18 | `return sm;`
83 |  | `}`
84 |  | ``
85 |  | `/**`
86 |  | `* Adds the vertex whose event this is to all appropriate subsets.`
87 |  | `*/`
88 |  | `public void vertexAdded(GraphEvent event)`
89 |  | `{`
90 | 61 | `ArchetypeVertex v = (ArchetypeVertex)event.getGraphElement();`
91 | 61 | `Map vMap = getVertexMap();`
92 | 61 | `for (Iterator iter = vMap.keySet().iterator(); iter.hasNext(); )`
93 |  | `{`
94 |  | `// VertexPredicate p = (VertexPredicate)iter.next();`
95 | 122 | `Predicate p = (Predicate)iter.next();`
96 |  | `// if (p.evaluateVertex(v))`
97 | 122 | `if (p.evaluate(v))`
98 |  | `{`
99 | 61 | `Set s = (Set)vMap.get(p);`
100 | 61 | `s.add(v);`
101 |  | `}`
102 |  | `}`
103 | 61 | `}`
104 |  | ``
105 |  | `/**`
106 |  | `* Removes the vertex whose event this is from all appropriate subsets.`
107 |  | `*/`
108 |  | `public void vertexRemoved(GraphEvent event)`
109 |  | `{`
110 | 0 | `ArchetypeVertex v = (ArchetypeVertex)event.getGraphElement();`
111 | 0 | `Map vMap = getVertexMap();`
112 | 0 | `for (Iterator iter = vMap.keySet().iterator(); iter.hasNext(); )`
113 |  | `{`
114 | 0 | `Set s = (Set)vMap.get(iter.next());`
115 | 0 | `s.remove(v);`
116 |  | `}`
117 | 0 | `}`
118 |  | ``
119 |  | `/**`
120 |  | `* Adds the edge whose event this is to all appropriate subsets.`
121 |  | `*/`
122 |  | `public void edgeAdded(GraphEvent event)`
123 |  | `{`
124 | 41 | `ArchetypeEdge e = (ArchetypeEdge)event.getGraphElement();`
125 | 41 | `Map eMap = getEdgeMap();`
126 | 41 | `for (Iterator iter = eMap.keySet().iterator(); iter.hasNext(); )`
127 |  | `{`
128 |  | `// EdgePredicate p = (EdgePredicate)iter.next();`
129 | 0 | `Predicate p = (Predicate)iter.next();`
130 |  | `// if (p.evaluateEdge(e))`
131 | 0 | `if (p.evaluate(e))`
132 |  | `{`
133 | 0 | `Set s = (Set)eMap.get(p);`
134 | 0 | `s.add(e);`
135 |  | `}`
136 |  | `}`
137 | 41 | `}`
138 |  | ``
139 |  | `/**`
140 |  | `* Removes the edge whose event this is from all appropriate subsets.`
141 |  | `*/`
142 |  | `public void edgeRemoved(GraphEvent event)`
143 |  | `{`
144 | 0 | `ArchetypeEdge e = (ArchetypeEdge)event.getGraphElement();`
145 | 0 | `Map eMap = getEdgeMap();`
146 | 0 | `for (Iterator iter = eMap.keySet().iterator(); iter.hasNext(); )`
147 |  | `{`
148 | 0 | `Set s = (Set)eMap.get(iter.next());`
149 | 0 | `s.remove(e);`
150 |  | `}`
151 | 0 | `}`
152 |  | ``
153 |  | `/**`
154 |  | `* Returns the vertex subset, if any, which this instance has defined`
155 |  | `* based on <code>p</code>. If this instance has defined no such`
156 |  | `* subset, returns null.`
157 |  | `* @param p the predicate which may define a subset`
158 |  | `*/`
159 |  | `public Set getVertices(Predicate p)`
160 |  | `{`
161 | 4 | `Set s = (Set)getVertexMap().get(p);`
162 | 4 | `if (s != null)`
163 | 4 | `return Collections.unmodifiableSet(s);`
164 |  | `else`
165 | 0 | `return null;`
166 |  | `}`
167 |  | ``
168 |  | `/**`
169 |  | `* Returns the edge subset, if any, which this instance has defined`
170 |  | `* based on <code>p</code>. If this instance has defined no such`
171 |  | `* subset, returns null.`
172 |  | `* @param p the predicate which may define a subset`
173 |  | `*/`
174 |  | `public Set getEdges(Predicate p)`
175 |  | `{`
176 | 0 | `Set s = (Set)getEdgeMap().get(p);`
177 | 0 | `if (s != null)`
178 | 0 | `return Collections.unmodifiableSet(s);`
179 |  | `else`
180 | 0 | `return null;`
181 |  | `}`
182 |  | ``
183 |  | `/**`
184 |  | `* Creates a vertex subset based on <code>p</code>.`
185 |  | `* @param p the predicate defining the subset`
186 |  | `* @return true if a subset was created; false if the subset already existed`
187 |  | `*/`
188 |  | `public boolean addVertexSubset(Predicate p)`
189 |  | `{`
190 | 36 | `Map vMap = getVertexMap();`
191 | 36 | `if (! vMap.containsKey(p))`
192 |  | `{`
193 | 35 | `Set subset = new HashSet();`
194 | 35 | `vMap.put(p, subset);`
195 | 35 | `for (Iterator v_iter = g.getVertices().iterator(); v_iter.hasNext(); )`
196 |  | `{`
197 | 0 | `ArchetypeVertex v = (ArchetypeVertex)v_iter.next();`
198 | 0 | `if (p.evaluate(v))`
199 | 0 | `subset.add(v);`
200 |  | `}`
201 | 35 | `return true;`
202 |  | `}`
203 | 1 | `return false;`
204 |  | `}`
205 |  | ``
206 |  | `/**`
207 |  | `* Creates an edge subset based on <code>p</code>.`
208 |  | `* @param p the predicate defining the subset`
209 |  | `* @return true if a subset was created; false if the subset already existed`
210 |  | `*/`
211 |  | `public boolean addEdgeSubset(Predicate p)`
212 |  | `{`
213 | 0 | `Map eMap = getEdgeMap();`
214 | 0 | `if (! eMap.containsKey(p))`
215 |  | `{`
216 | 0 | `Set subset = new HashSet();`
217 | 0 | `eMap.put(p, subset);`
218 | 0 | `for (Iterator e_iter = g.getEdges().iterator(); e_iter.hasNext(); )`
219 |  | `{`
220 | 0 | `ArchetypeEdge e = (ArchetypeEdge)e_iter.next();`
221 | 0 | `if (p.evaluate(e))`
222 | 0 | `subset.add(e);`
223 |  | `}`
224 | 0 | `return true;`
225 |  | `}`
226 | 0 | `return false;`
227 |  | `}`
228 |  | ``
229 |  | `/**`
230 |  | `* Removes the vertex subset based on <code>p</code>.`
231 |  | `* @param p the predicate defining the subset`
232 |  | `*/`
233 |  | `public void removeVertexSubset(Predicate p)`
234 |  | `{`
235 | 0 | `getVertexMap().remove(p);`
236 | 0 | `}`
237 |  | ``
238 |  | `/**`
239 |  | `* Removes the edge subset based on <code>p</code>.`
240 |  | `* @param p the predicate defining the subset`
241 |  | `*/`
242 |  | `public void removeEdgeSubset(Predicate p)`
243 |  | `{`
244 | 0 | `getVertexMap().remove(p);`
245 | 0 | `}`
246 |  | ``
247 |  | `protected Map getVertexMap()`
248 |  | `{`
249 | 101 | `if (vertexMap == null)`
250 | 18 | `vertexMap = new HashMap();`
251 | 101 | `return vertexMap;`
252 |  | `}`
253 |  | ``
254 |  | `protected Map getEdgeMap()`
255 |  | `{`
256 | 41 | `if (edgeMap == null)`
257 | 13 | `edgeMap = new HashMap();`
258 | 41 | `return edgeMap;`
259 |  | `}`
260 |  | `}`

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.