Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Created on Apr 27, 2005 | |
3 | * | |
4 | * Copyright (c) 2005, the JUNG Project and the Regents of the University | |
5 | * of California | |
6 | * All rights reserved. | |
7 | * | |
8 | * This software is open-source under the BSD license; see either | |
9 | * "license.txt" or | |
10 | * http://jung.sourceforge.net/license.txt for a description. | |
11 | */ | |
12 | package edu.uci.ics.jung.graph.impl; | |
13 | ||
14 | import java.util.Collections; | |
15 | import java.util.HashSet; | |
16 | import java.util.Iterator; | |
17 | import java.util.Set; | |
18 | ||
19 | import edu.uci.ics.jung.graph.Hyperedge; | |
20 | import edu.uci.ics.jung.graph.Hypergraph; | |
21 | import edu.uci.ics.jung.graph.Hypervertex; | |
22 | import edu.uci.ics.jung.utils.GraphUtils; | |
23 | ||
24 | /** | |
25 | * A basic implementation of <code>Hypergraph</code>. Edges | |
26 | * and vertices are stored as <code>Set</code>s. | |
27 | * | |
28 | * @author Joshua O'Madadhain | |
29 | */ | |
30 | public class SetHypergraph extends AbstractArchetypeGraph | |
31 | implements Hypergraph | |
32 | { | |
33 | protected Set edges; | |
34 | protected Set vertices; | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | public SetHypergraph() | |
40 | { | |
41 | 0 | super(); |
42 | 0 | initialize(); |
43 | 0 | } |
44 | ||
45 | public void initialize() | |
46 | { | |
47 | 0 | edges = new HashSet(); |
48 | 0 | vertices = new HashSet(); |
49 | 0 | super.initialize(); |
50 | 0 | } |
51 | ||
52 | /** | |
53 | * @see edu.uci.ics.jung.graph.Hypergraph#addVertex(edu.uci.ics.jung.graph.Hypervertex) | |
54 | */ | |
55 | public Hypervertex addVertex(Hypervertex v) | |
56 | { | |
57 | 0 | checkConstraints(v, vertex_requirements); |
58 | ||
59 | 0 | if (v instanceof AbstractElement) |
60 | { | |
61 | 0 | AbstractElement ae = (AbstractElement) v; |
62 | 0 | ae.checkIDs(mVertexIDs); |
63 | 0 | ae.addGraph_internal(this); |
64 | } | |
65 | 0 | vertices.add(v); |
66 | 0 | mGraphListenerHandler.handleAdd( v ); |
67 | 0 | return v; |
68 | } | |
69 | ||
70 | /** | |
71 | * Removes the vertex from this graph. If the vertex is an instance of | |
72 | * <code>AbstractElement</code>, notifies it that it | |
73 | * has been removed. Disconnects this vertex from any hyperedges to which | |
74 | * it may be connected. | |
75 | */ | |
76 | public void removeVertex(Hypervertex v) | |
77 | { | |
78 | 0 | if (v.getGraph() != this) |
79 | 0 | throw new IllegalArgumentException("This vertex is not in this graph"); |
80 | ||
81 | 0 | Set v_edges = new HashSet(v.getIncidentEdges()); |
82 | 0 | for (Iterator iter = v_edges.iterator(); iter.hasNext(); ) |
83 | 0 | v.disconnectEdge((Hyperedge)iter.next()); |
84 | ||
85 | 0 | if (v instanceof AbstractElement) |
86 | { | |
87 | 0 | AbstractElement ae = (AbstractElement) v; |
88 | 0 | ae.removeGraph_internal(); |
89 | 0 | mVertexIDs.remove(new Integer(ae.getID())); |
90 | } | |
91 | ||
92 | 0 | vertices.remove(v); |
93 | 0 | mGraphListenerHandler.handleRemove( v ); |
94 | 0 | } |
95 | ||
96 | /** | |
97 | * @see edu.uci.ics.jung.graph.Hypergraph#addEdge(edu.uci.ics.jung.graph.Hyperedge) | |
98 | */ | |
99 | public Hyperedge addEdge(Hyperedge e) | |
100 | { | |
101 | 0 | checkConstraints(e, edge_requirements); |
102 | ||
103 | 0 | if (e instanceof AbstractElement) |
104 | { | |
105 | 0 | AbstractElement ae = (AbstractElement) e; |
106 | 0 | ae.checkIDs(mEdgeIDs); |
107 | 0 | ae.addGraph_internal(this); |
108 | } | |
109 | 0 | edges.add(e); |
110 | 0 | mGraphListenerHandler.handleAdd( e ); |
111 | 0 | return e; |
112 | } | |
113 | ||
114 | /** | |
115 | * Removes the edge from this graph. If the edge is an instance of | |
116 | * <code>AbstractElement</code>, notifies it that it | |
117 | * has been removed. | |
118 | */ | |
119 | public void removeEdge(Hyperedge e) | |
120 | { | |
121 | 0 | if (e.getGraph() != this) |
122 | 0 | throw new IllegalArgumentException("This edge is not in this graph"); |
123 | ||
124 | 0 | Set e_vertices = new HashSet(e.getIncidentVertices()); |
125 | 0 | for (Iterator iter = e_vertices.iterator(); iter.hasNext(); ) |
126 | 0 | e.disconnectVertex((Hypervertex)iter.next()); |
127 | ||
128 | 0 | if (e instanceof AbstractElement) |
129 | { | |
130 | 0 | AbstractElement ae = (AbstractElement)e; |
131 | 0 | ae.removeGraph_internal(); |
132 | 0 | mEdgeIDs.remove(new Integer(ae.getID())); |
133 | } | |
134 | ||
135 | 0 | edges.remove(e); |
136 | 0 | mGraphListenerHandler.handleRemove( e ); |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * @see edu.uci.ics.jung.graph.ArchetypeGraph#getVertices() | |
141 | */ | |
142 | public Set getVertices() | |
143 | { | |
144 | 0 | return Collections.unmodifiableSet(vertices); |
145 | } | |
146 | ||
147 | /** | |
148 | * @see edu.uci.ics.jung.graph.ArchetypeGraph#getEdges() | |
149 | */ | |
150 | public Set getEdges() | |
151 | { | |
152 | 0 | return Collections.unmodifiableSet(edges); |
153 | } | |
154 | ||
155 | /** | |
156 | * Removes all vertices in the specified set from <code>g</code>. Syntactic | |
157 | * sugar for a loop that calls <code>g.removeVertex</code> on all elements | |
158 | * of the set. | |
159 | * If any element of <code>vertices</code> is not part of this graph, | |
160 | * then throws <code>IllegalArgumentException</code>. If this | |
161 | * exception is thrown, any vertices that may have been removed already | |
162 | * are not guaranteed to be restored to the graph. | |
163 | */ | |
164 | public void removeVertices(Set vertices) | |
165 | { | |
166 | 0 | GraphUtils.removeVertices(this, vertices); |
167 | 0 | } |
168 | ||
169 | /** | |
170 | * Removes all vertices in the specified set from <code>g</code>. Syntactic | |
171 | * sugar for a loop that calls <code>g.removeVertex</code> on all elements | |
172 | * of the set. | |
173 | * If any element of <code>edges</code> is not part of this graph, | |
174 | * then throws <code>IllegalArgumentException</code>. If this | |
175 | * exception is thrown, any edges that may have been removed already | |
176 | * are not guaranteed to be restored to the graph. | |
177 | */ | |
178 | public void removeEdges(Set edges) | |
179 | { | |
180 | 0 | GraphUtils.removeEdges(this, edges); |
181 | 0 | } |
182 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |