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

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.Map;
18  
19 import edu.uci.ics.jung.graph.ArchetypeEdge;
20 import edu.uci.ics.jung.graph.Graph;
21 import edu.uci.ics.jung.utils.UserData;
22  
23 /**
24  *
25  * A EdgeWeightLabeller applies a label to the edges of a Graph.
26  * All edge weights are integers; weights need not be unique.
27  * (The cost of not being unique is that there's no way to
28  * look up edges by weight.)
29  *
30  * Note that this stores information with the graph, and
31  * as such is not flexible to addition and substraction of
32  * nodes.
33  *
34  * @author danyelf
35  *
36  * TODO : Should store weight in a decorator, per-Edge instead of
37  * per-Graph.
38  *
39  */
40 public class EdgeWeightLabeller implements NumberEdgeValue {
41  
42     /**
43      * The UserData key that stores edge weights.
44      */
455    public static final Object WEIGHT_DEFAULT_KEY = "WeightDefaultKey";
46  
47     /**
48      * Finds or creates an edge labeller for the graph, using
49      * the default user data key.
50      * @param g
51      * @return the labeller
52      */
53     public static EdgeWeightLabeller getLabeller( Graph g ) {
5443        return getLabeller( g , WEIGHT_DEFAULT_KEY );
55     }
56     
57     /**
58      * Checks for an edge labeleller for the graph, using
59      * the default user data key.
60      * @param g
61      * @return the labeller
62      */
63     public static boolean hasWeightLabeller ( Graph g ) {
640        return hasWeightLabeller( g , WEIGHT_DEFAULT_KEY );
65     }
66  
67     /**
68      * Checks an edge labeleller for the graph at the given key.
69      * @return the labeller
70      */
71     public static boolean hasWeightLabeller( Graph g, Object key ) {
720        EdgeWeightLabeller id = (EdgeWeightLabeller) g.getUserDatum( key );
730        return (id != null);
74     }
75  
76     /**
77      * Finds or creates an edge labeleller for the graph, using
78      * the given userdata key.
79      * @param g
80      * @return the labeller
81      */
82     public static EdgeWeightLabeller getLabeller( Graph g, Object key ) {
8343        EdgeWeightLabeller id = (EdgeWeightLabeller) g.getUserDatum( key );
8443        if (id != null)
8517            return id;
8626        id = new EdgeWeightLabeller( g );
8726        g.addUserDatum( key, id, UserData.REMOVE );
8826        return id;
89     }
90  
91     /**
92      * Gets the weight of a particualr edge. Throws an exception if
93      * the edge is not weighted, or if the edge is not a part of
94      * the graph.
95      * @param e an edge that has been weighted.
96      */
97     public int getWeight( ArchetypeEdge e ) {
98522        if (! edgeToWeight.containsKey( e )) {
990            throw new IllegalArgumentException("This edge has no assigned weight");
100         }
101522        return ((Number) edgeToWeight.get( e )).intValue();
102     }
103  
104     /**
105      * Returns the graph associated with this particular
106      * labeller.
107      */
108     public Graph getGraph() {
109368        return graph;
110     }
111  
112     /**
113      * Sets an edge to this weight.
114      * @param e the edge
115      * @param i the weight
116      * @throws if the edge is not part of the graph
117      */
118     public void setWeight(ArchetypeEdge e, int i) {
119167        if (graph.getEdges().contains( e )) {
120167            edgeToWeight.put( e, new Integer( i ));
121         } else {
122             // throw some sort of exception here
1230            throw new IllegalArgumentException("This edge is not a part of this graph");
124         }
125167    }
126     
127     /**
128      * Removes the weight stored by this decorator for the indicated edge <code>e</code>,
129      * and returns the value of this weight (or <code>null</code> if there was no
130      * such weight for this edge).
131      */
132     public Number removeWeight(ArchetypeEdge e)
133     {
1340        return (Number)edgeToWeight.remove(e);
135     }
136  
137     /**
138      * Clears all weights stored by this decorator.
139      */
140     public void clear()
141     {
1420        edgeToWeight.clear();
1430    }
144     
14526    private Map edgeToWeight = new HashMap();
146     private Graph graph;
147  
148     /**
149      * @param g
150      */
15126    private EdgeWeightLabeller(Graph g) {
15226        this.graph = g;
15326    }
154  
155     /**
156      * @see edu.uci.ics.jung.graph.decorators.NumberEdgeValue#getNumber(edu.uci.ics.jung.graph.ArchetypeEdge)
157      */
158     public Number getNumber(ArchetypeEdge e)
159     {
1600        Number value = (Number)edgeToWeight.get(e);
1610        if (value == null)
1620            throw new IllegalArgumentException("This edge is unweighted");
1630        return value;
164     }
165  
166     /**
167      * @see edu.uci.ics.jung.graph.decorators.NumberEdgeValue#setNumber(edu.uci.ics.jung.graph.ArchetypeEdge, java.lang.Number)
168      */
169     public void setNumber(ArchetypeEdge e, Number n)
170     {
1710        if (graph.getEdges().contains( e )) {
1720            edgeToWeight.put( e, n);
173         } else {
174             // throw some sort of exception here
1750            throw new IllegalArgumentException("This edge is not a part of this graph");
176         }
1770    }
178  
179 }

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.