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

LineHitsSource
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() {
352        super(null);
362    }
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) {
435        StringLabeller sl = GlobalStringLabeller.getInstance();
445        if (key != null) g.addUserDatum(key, sl, UserData.REMOVE);
455        return sl;
46     }
47  
48     /**
49      * @return a <code>GlobalStringLabeller</code> instance
50      */
51     public synchronized static StringLabeller getInstance() {
529        if (instance == null) {
532            instance = new GlobalStringLabeller();
54         }
559        return instance;
56     }
57     
58     public static StringLabeller getLabeller( Graph g ) {
595        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) {
665        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) {
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        GlobalStringLabeller id = (GlobalStringLabeller) g.getUserDatum(key);
910        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) {
10228        if (vertexToLabel.containsKey(v)) {
10327            return (String) vertexToLabel.get(v);
104         } else
1051            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  
12220        if (labelToVertex.containsKey(l)) {
123         // we already have a vertex with this label
1241        throw new UniqueLabelException(l + " is already on vertex "
125                 + labelToVertex.get(l)); }
126         // ok, we know we don't have this label anywhere yet
12719        if (vertexToLabel.containsKey(v)) {
1282            Object junk = vertexToLabel.get(v);
1292            labelToVertex.remove(junk);
130         }
13119        vertexToLabel.put(v, l);
13219        labelToVertex.put(l, v);
133  
13419    }
135 }

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.