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

LineHitsSource
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     {
6618        super();
6718        g.addUserDatum(ArchetypeGraph.SUBSET_MANAGER, this, UserData.REMOVE);
6818        g.addListener(this, GraphEventType.ALL_SINGLE_EVENTS);
6918        this.g = g;
7018    }
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     {
7918        SubsetManager sm = (SubsetManager)g.getUserDatum(ArchetypeGraph.SUBSET_MANAGER);
8018        if (sm == null)
8118            sm = new SubsetManager(g);
8218        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     {
9061        ArchetypeVertex v = (ArchetypeVertex)event.getGraphElement();
9161        Map vMap = getVertexMap();
9261        for (Iterator iter = vMap.keySet().iterator(); iter.hasNext(); )
93         {
94 // VertexPredicate p = (VertexPredicate)iter.next();
95122            Predicate p = (Predicate)iter.next();
96 // if (p.evaluateVertex(v))
97122            if (p.evaluate(v))
98             {
9961                Set s = (Set)vMap.get(p);
10061                s.add(v);
101             }
102         }
10361    }
104  
105     /**
106      * Removes the vertex whose event this is from all appropriate subsets.
107      */
108     public void vertexRemoved(GraphEvent event)
109     {
1100        ArchetypeVertex v = (ArchetypeVertex)event.getGraphElement();
1110        Map vMap = getVertexMap();
1120        for (Iterator iter = vMap.keySet().iterator(); iter.hasNext(); )
113         {
1140            Set s = (Set)vMap.get(iter.next());
1150            s.remove(v);
116         }
1170    }
118  
119     /**
120      * Adds the edge whose event this is to all appropriate subsets.
121      */
122     public void edgeAdded(GraphEvent event)
123     {
12441        ArchetypeEdge e = (ArchetypeEdge)event.getGraphElement();
12541        Map eMap = getEdgeMap();
12641        for (Iterator iter = eMap.keySet().iterator(); iter.hasNext(); )
127         {
128 // EdgePredicate p = (EdgePredicate)iter.next();
1290            Predicate p = (Predicate)iter.next();
130 // if (p.evaluateEdge(e))
1310            if (p.evaluate(e))
132             {
1330                Set s = (Set)eMap.get(p);
1340                s.add(e);
135             }
136         }
13741    }
138  
139     /**
140      * Removes the edge whose event this is from all appropriate subsets.
141      */
142     public void edgeRemoved(GraphEvent event)
143     {
1440        ArchetypeEdge e = (ArchetypeEdge)event.getGraphElement();
1450        Map eMap = getEdgeMap();
1460        for (Iterator iter = eMap.keySet().iterator(); iter.hasNext(); )
147         {
1480            Set s = (Set)eMap.get(iter.next());
1490            s.remove(e);
150         }
1510    }
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     {
1614        Set s = (Set)getVertexMap().get(p);
1624        if (s != null)
1634            return Collections.unmodifiableSet(s);
164         else
1650            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     {
1760        Set s = (Set)getEdgeMap().get(p);
1770        if (s != null)
1780            return Collections.unmodifiableSet(s);
179         else
1800            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     {
19036        Map vMap = getVertexMap();
19136        if (! vMap.containsKey(p))
192         {
19335            Set subset = new HashSet();
19435            vMap.put(p, subset);
19535            for (Iterator v_iter = g.getVertices().iterator(); v_iter.hasNext(); )
196             {
1970                ArchetypeVertex v = (ArchetypeVertex)v_iter.next();
1980                if (p.evaluate(v))
1990                    subset.add(v);
200             }
20135            return true;
202         }
2031        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     {
2130        Map eMap = getEdgeMap();
2140        if (! eMap.containsKey(p))
215         {
2160            Set subset = new HashSet();
2170            eMap.put(p, subset);
2180            for (Iterator e_iter = g.getEdges().iterator(); e_iter.hasNext(); )
219             {
2200                ArchetypeEdge e = (ArchetypeEdge)e_iter.next();
2210                if (p.evaluate(e))
2220                    subset.add(e);
223             }
2240            return true;
225         }
2260        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     {
2350        getVertexMap().remove(p);
2360    }
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     {
2440        getVertexMap().remove(p);
2450    }
246  
247     protected Map getVertexMap()
248     {
249101        if (vertexMap == null)
25018            vertexMap = new HashMap();
251101        return vertexMap;
252     }
253  
254     protected Map getEdgeMap()
255     {
25641        if (edgeMap == null)
25713            edgeMap = new HashMap();
25841        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.