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 | /* | |
9 | * Created on Jun 13, 2003 | |
10 | * | |
11 | */ | |
12 | package edu.uci.ics.jung.graph.decorators; | |
13 | ||
14 | import edu.uci.ics.jung.exceptions.FatalException; | |
15 | import edu.uci.ics.jung.graph.ArchetypeVertex; | |
16 | import edu.uci.ics.jung.graph.Graph; | |
17 | import edu.uci.ics.jung.graph.Vertex; | |
18 | import edu.uci.ics.jung.utils.UserData; | |
19 | ||
20 | /** | |
21 | * | |
22 | * The GlobalStringLabeller applies labels to all vertices in a series of | |
23 | * graphs. That is, rather than storing one instance per graph, it stores one | |
24 | * instance globally that maps from vertex ID to label. | |
25 | * | |
26 | * @author danyelf | |
27 | * | |
28 | */ | |
29 | ||
30 | public class GlobalStringLabeller extends StringLabeller { | |
31 | ||
32 | protected static GlobalStringLabeller instance; | |
33 | ||
34 | protected GlobalStringLabeller() { | |
35 | 2 | super(null); |
36 | 2 | } |
37 | ||
38 | /** | |
39 | * Sets the StringLabeller of this graph, at this key, to be a | |
40 | * ToStringLabeller. | |
41 | */ | |
42 | public static StringLabeller setLabellerTo(Graph g, Object key) { | |
43 | 5 | StringLabeller sl = GlobalStringLabeller.getInstance(); |
44 | 5 | if (key != null) g.addUserDatum(key, sl, UserData.REMOVE); |
45 | 5 | return sl; |
46 | } | |
47 | ||
48 | /** | |
49 | * @return a <code>GlobalStringLabeller</code> instance | |
50 | */ | |
51 | public synchronized static StringLabeller getInstance() { | |
52 | 9 | if (instance == null) { |
53 | 2 | instance = new GlobalStringLabeller(); |
54 | } | |
55 | 9 | return instance; |
56 | } | |
57 | ||
58 | public static StringLabeller getLabeller( Graph g ) { | |
59 | 5 | return setLabellerTo(g); |
60 | } | |
61 | ||
62 | /** | |
63 | * Sets the default StringLabeller of this graph to be a ToStringLabeller. | |
64 | */ | |
65 | public static StringLabeller setLabellerTo(Graph g) { | |
66 | 5 | return setLabellerTo(g, StringLabeller.DEFAULT_STRING_LABELER_KEY); |
67 | } | |
68 | ||
69 | /** | |
70 | * Checks if a labeller--any 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 | GlobalStringLabeller id = (GlobalStringLabeller) g.getUserDatum(key); |
91 | 0 | return (id != null); |
92 | } | |
93 | ||
94 | /** | |
95 | * Gets the String label associated with a particular Vertex. | |
96 | * | |
97 | * @param v a Vertex | |
98 | * @return the label associated with that vertex | |
99 | * If the vertex is not in the graph, throws a FatalException | |
100 | */ | |
101 | public String getLabel(ArchetypeVertex v) { | |
102 | 28 | if (vertexToLabel.containsKey(v)) { |
103 | 27 | return (String) vertexToLabel.get(v); |
104 | } else | |
105 | 1 | throw new FatalException("Vertex not registered in GlobalStringLabeller!"); |
106 | } | |
107 | ||
108 | /** | |
109 | * Associates a Vertex with a Label, overrwriting any previous labels on | |
110 | * this vertex or vertices equal to it. | |
111 | * | |
112 | * @param v | |
113 | * a Vertex | |
114 | * @param l | |
115 | * a Label to be associated with this vertex | |
116 | * @throws UniqueLabelException | |
117 | * thrown if this label is already associated with some other | |
118 | * vertex. | |
119 | */ | |
120 | public void setLabel(Vertex v, String l) throws UniqueLabelException { | |
121 | ||
122 | 20 | if (labelToVertex.containsKey(l)) { |
123 | // we already have a vertex with this label | |
124 | 1 | throw new UniqueLabelException(l + " is already on vertex " |
125 | + labelToVertex.get(l)); } | |
126 | // ok, we know we don't have this label anywhere yet | |
127 | 19 | if (vertexToLabel.containsKey(v)) { |
128 | 2 | Object junk = vertexToLabel.get(v); |
129 | 2 | labelToVertex.remove(junk); |
130 | } | |
131 | 19 | vertexToLabel.put(v, l); |
132 | 19 | labelToVertex.put(l, v); |
133 | ||
134 | 19 | } |
135 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |