Coverage details for edu.uci.ics.jung.graph.decorators.StringLabeller

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  */
10 /*
11  * Created on Jun 13, 2003
12  *
13  */
14 package edu.uci.ics.jung.graph.decorators;
15  
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Map;
19 import java.util.Set;
20  
21 import edu.uci.ics.jung.exceptions.FatalException;
22 import edu.uci.ics.jung.graph.ArchetypeVertex;
23 import edu.uci.ics.jung.graph.Graph;
24 import edu.uci.ics.jung.graph.Vertex;
25 import edu.uci.ics.jung.utils.UserData;
26  
27 /**
28  *
29  * A StringLabeller applies a set of labels to a Graph. The Labeller,
30  * specifically, attaches itself to a Graph's UserData, and maintains an index
31  * of Strings that are labels. Note that the strings must be unique so that
32  * getVertex( label ) will work.
33  *
34  * @author danyelf
35  *
36  */
37 public class StringLabeller implements VertexStringer {
38  
39     /**
40      * The key that hasLabeller() and getLabeller() use.
41      */
4222    public static final Object DEFAULT_STRING_LABELER_KEY = "StringLabeller.LabelDefaultKey";
43111    protected Map labelToVertex = new HashMap();
44111    protected Map vertexToLabel = new HashMap();
45     protected Graph graph;
46 // protected WeakReference graph;
47  
48     /**
49      * @param g
50      * The graph to which this labeller should attach itself
51      */
52111    protected StringLabeller(Graph g) {
53111        this.graph = g;
54 // this.graph = new WeakReference(g);
55111    }
56  
57     /**
58      * Gets a labeller associated with this graph. If no Labeller is associated,
59      * creates one and returns it. This method is the same as getLabeller with a
60      * key argument, but uses the DEFAULT_STRING_LABELER_KEY as its UserData
61      * key.
62      *
63      * param g The Graph to check.
64      */
65     public static StringLabeller getLabeller(Graph g) {
66120        return getLabeller(g, DEFAULT_STRING_LABELER_KEY);
67     }
68  
69     /**
70      * Checks if a labeller is associated with this graph.
71      *
72      * @param g
73      * The graph to check.
74      */
75     public static boolean hasStringLabeller(Graph g) {
760        return hasStringLabeller(g, DEFAULT_STRING_LABELER_KEY);
77     }
78  
79     /**
80      * Checks for a labeller attached to a particular key in the graph. Useful
81      * for creating more than one Labeller for a particular Graph.
82      *
83      * @param g
84      * the Graph
85      * @param key
86      * the UserData key to which it is attached
87      * @return true if the graph has this labeller.
88      */
89     public static boolean hasStringLabeller(Graph g, Object key) {
900        StringLabeller id = (StringLabeller) g.getUserDatum(key);
910        return (id != null);
92     }
93  
94     /**
95      * Returns a labeller attached to a particular key in the graph. Useful for
96      * creating more than one Labeller for a particular Graph.
97      *
98      * @param g
99      * the Graph
100      * @param key
101      * the UserData key to which it is attached
102      * @return a StringLabeller
103      */
104     public static StringLabeller getLabeller(Graph g, Object key) {
105157        StringLabeller id = (StringLabeller) g.getUserDatum(key);
106157        if (id != null)
10749            return id;
108108        id = new StringLabeller(g);
109108        g.addUserDatum(key, id, UserData.REMOVE);
110108        return id;
111     }
112  
113     /**
114      * Gets the graph associated with this StringLabeller
115      *
116      * @return a Graph that uses this StringLabeller.
117      */
118     public Graph getGraph() {
119366        return graph;
120 // return (Graph)graph.get();
121     }
122  
123     /**
124      * Gets the String label associated with a particular Vertex.
125      *
126      * @param v
127      * a Vertex inside the Graph.
128      * @throws FatalException
129      * if the Vertex is not in the Graph associated with this
130      * Labeller.
131      */
132     public String getLabel(ArchetypeVertex v) {
133363        if (getGraph().getVertices().contains(v)) {
134363            return (String) vertexToLabel.get(v);
135         } else
1360            throw new FatalException("Vertex not in my graph!");
137     }
138  
139     /**
140      * Gets the Vertex from the graph associated with this label.
141      *
142      * @param label
143      */
144     public Vertex getVertex(String label) {
145777        return (Vertex) labelToVertex.get(label);
146     }
147  
148     /**
149      * Associates a Vertex with a Label, overrwriting any previous labels on
150      * this vertex.
151      *
152      * @param v
153      * a Vertex in the labeller's graph
154      * @param l
155      * a Label to be associated with this vertex
156      * @throws FatalException
157      * thrown if this vertex isn't in the Labeller's graph
158      * @throws UniqueLabelException
159      * thrown if this label is already associated with some other
160      * vertex.
161      */
162     public void setLabel(Vertex v, String l) throws UniqueLabelException {
163  
1641300        if (v.getGraph() == graph) {
1651300            if (labelToVertex.containsKey(l)) {
166                 // we already have a vertex with this label
1671                throw new UniqueLabelException(l + " is already on vertex "
168                         + labelToVertex.get(l));
169             }
170             // ok, we know we don't have this label anywhere yet
1711299            if (vertexToLabel.containsKey(v)) {
1727                Object junk = vertexToLabel.get(v);
1737                labelToVertex.remove(junk);
174             }
1751299            vertexToLabel.put(v, l);
1761299            labelToVertex.put(l, v);
177         } else {
178             // throw some sort of exception here
1790            throw new FatalException("This vertex is not a part of this graph");
180         }
181  
1821299    }
183  
184     /**
185      * Assigns textual labels to every vertex passed in. Walks through the graph
186      * in iterator order, assigning labels "offset", "offset+1" "offset+2". The
187      * count starts at offset.
188      *
189      * @param vertices
190      * The set of Vertices to label. All must be part of this graph.
191      * @param offset
192      * The starting value to number vertices from
193      * @throws UniqueLabelException
194      * Is thrown if some other vertexc is already numbered.
195      * @throws FatalException
196      * if any Vertex is not part of the Graph.
197      */
198     public void assignDefaultLabels(Set vertices, int offset)
199             throws UniqueLabelException {
2000        int labelIdx = offset;
2010        for (Iterator udcIt = vertices.iterator(); udcIt.hasNext();) {
2020            Vertex v = (Vertex) udcIt.next();
2030            String label = String.valueOf(labelIdx);
2040            setLabel(v, label);
2050            labelIdx++;
206         }
2070    }
208  
209     /**
210      * A minor class to store exceptions from duplicate labels in the Graph.
211      *
212      * @author danyelf
213      */
214     public static class UniqueLabelException extends Exception {
215  
216         public UniqueLabelException(String string) {
217             super(string);
218         }
219  
220     }
221  
222     /**
223      * @param string
224      */
225     public Vertex removeLabel(String string) {
2263        if (labelToVertex.containsKey(string)) {
2273            Vertex v = (Vertex) labelToVertex.get(string);
2283            labelToVertex.remove(string);
2293            vertexToLabel.remove(v);
2303            return v;
231         } else {
2320            return null;
233         }
234  
235     }
236  
237     /**
238      * Wipes the entire table. Resets everything.
239      */
240     public void clear() {
2416        vertexToLabel.clear();
2426        labelToVertex.clear();
2436    }
244  
245 }

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.