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.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 | */ | |
45 | 5 | 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 ) { | |
54 | 43 | 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 ) { | |
64 | 0 | 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 ) { | |
72 | 0 | EdgeWeightLabeller id = (EdgeWeightLabeller) g.getUserDatum( key ); |
73 | 0 | 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 ) { | |
83 | 43 | EdgeWeightLabeller id = (EdgeWeightLabeller) g.getUserDatum( key ); |
84 | 43 | if (id != null) |
85 | 17 | return id; |
86 | 26 | id = new EdgeWeightLabeller( g ); |
87 | 26 | g.addUserDatum( key, id, UserData.REMOVE ); |
88 | 26 | 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 ) { | |
98 | 522 | if (! edgeToWeight.containsKey( e )) { |
99 | 0 | throw new IllegalArgumentException("This edge has no assigned weight"); |
100 | } | |
101 | 522 | return ((Number) edgeToWeight.get( e )).intValue(); |
102 | } | |
103 | ||
104 | /** | |
105 | * Returns the graph associated with this particular | |
106 | * labeller. | |
107 | */ | |
108 | public Graph getGraph() { | |
109 | 368 | 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) { | |
119 | 167 | if (graph.getEdges().contains( e )) { |
120 | 167 | edgeToWeight.put( e, new Integer( i )); |
121 | } else { | |
122 | // throw some sort of exception here | |
123 | 0 | throw new IllegalArgumentException("This edge is not a part of this graph"); |
124 | } | |
125 | 167 | } |
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 | { | |
134 | 0 | return (Number)edgeToWeight.remove(e); |
135 | } | |
136 | ||
137 | /** | |
138 | * Clears all weights stored by this decorator. | |
139 | */ | |
140 | public void clear() | |
141 | { | |
142 | 0 | edgeToWeight.clear(); |
143 | 0 | } |
144 | ||
145 | 26 | private Map edgeToWeight = new HashMap(); |
146 | private Graph graph; | |
147 | ||
148 | /** | |
149 | * @param g | |
150 | */ | |
151 | 26 | private EdgeWeightLabeller(Graph g) { |
152 | 26 | this.graph = g; |
153 | 26 | } |
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 | { | |
160 | 0 | Number value = (Number)edgeToWeight.get(e); |
161 | 0 | if (value == null) |
162 | 0 | throw new IllegalArgumentException("This edge is unweighted"); |
163 | 0 | 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 | { | |
171 | 0 | if (graph.getEdges().contains( e )) { |
172 | 0 | edgeToWeight.put( e, n); |
173 | } else { | |
174 | // throw some sort of exception here | |
175 | 0 | throw new IllegalArgumentException("This edge is not a part of this graph"); |
176 | } | |
177 | 0 | } |
178 | ||
179 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |