| Line | Hits | Source |
|---|---|---|
| 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 | */ | |
| 42 | 22 | public static final Object DEFAULT_STRING_LABELER_KEY = "StringLabeller.LabelDefaultKey"; |
| 43 | 111 | protected Map labelToVertex = new HashMap(); |
| 44 | 111 | 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 | */ | |
| 52 | 111 | protected StringLabeller(Graph g) { |
| 53 | 111 | this.graph = g; |
| 54 | // this.graph = new WeakReference(g); | |
| 55 | 111 | } |
| 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) { | |
| 66 | 120 | 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) { | |
| 76 | 0 | 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) { | |
| 90 | 0 | StringLabeller id = (StringLabeller) g.getUserDatum(key); |
| 91 | 0 | 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) { | |
| 105 | 157 | StringLabeller id = (StringLabeller) g.getUserDatum(key); |
| 106 | 157 | if (id != null) |
| 107 | 49 | return id; |
| 108 | 108 | id = new StringLabeller(g); |
| 109 | 108 | g.addUserDatum(key, id, UserData.REMOVE); |
| 110 | 108 | 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() { | |
| 119 | 366 | 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) { | |
| 133 | 363 | if (getGraph().getVertices().contains(v)) { |
| 134 | 363 | return (String) vertexToLabel.get(v); |
| 135 | } else | |
| 136 | 0 | 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) { | |
| 145 | 777 | 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 | ||
| 164 | 1300 | if (v.getGraph() == graph) { |
| 165 | 1300 | if (labelToVertex.containsKey(l)) { |
| 166 | // we already have a vertex with this label | |
| 167 | 1 | 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 | |
| 171 | 1299 | if (vertexToLabel.containsKey(v)) { |
| 172 | 7 | Object junk = vertexToLabel.get(v); |
| 173 | 7 | labelToVertex.remove(junk); |
| 174 | } | |
| 175 | 1299 | vertexToLabel.put(v, l); |
| 176 | 1299 | labelToVertex.put(l, v); |
| 177 | } else { | |
| 178 | // throw some sort of exception here | |
| 179 | 0 | throw new FatalException("This vertex is not a part of this graph"); |
| 180 | } | |
| 181 | ||
| 182 | 1299 | } |
| 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 { | |
| 200 | 0 | int labelIdx = offset; |
| 201 | 0 | for (Iterator udcIt = vertices.iterator(); udcIt.hasNext();) { |
| 202 | 0 | Vertex v = (Vertex) udcIt.next(); |
| 203 | 0 | String label = String.valueOf(labelIdx); |
| 204 | 0 | setLabel(v, label); |
| 205 | 0 | labelIdx++; |
| 206 | } | |
| 207 | 0 | } |
| 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) { | |
| 226 | 3 | if (labelToVertex.containsKey(string)) { |
| 227 | 3 | Vertex v = (Vertex) labelToVertex.get(string); |
| 228 | 3 | labelToVertex.remove(string); |
| 229 | 3 | vertexToLabel.remove(v); |
| 230 | 3 | return v; |
| 231 | } else { | |
| 232 | 0 | return null; |
| 233 | } | |
| 234 | ||
| 235 | } | |
| 236 | ||
| 237 | /** | |
| 238 | * Wipes the entire table. Resets everything. | |
| 239 | */ | |
| 240 | public void clear() { | |
| 241 | 6 | vertexToLabel.clear(); |
| 242 | 6 | labelToVertex.clear(); |
| 243 | 6 | } |
| 244 | ||
| 245 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |