1 25 40 package org.jgrapht.experimental; 41 42 import java.util.*; 43 44 import org.jgrapht.*; 45 import org.jgrapht.event.*; 46 import org.jgrapht.graph.*; 47 48 49 55 public class GraphSquare<V, E> 56 extends AbstractBaseGraph<V, E> 57 { 58 59 61 private static final long serialVersionUID = -2642034600395594304L; 62 private static final String UNMODIFIABLE = "this graph is unmodifiable"; 63 64 66 72 public GraphSquare(final Graph<V, E> g, final boolean createLoops) 73 { 74 super(g.getEdgeFactory(), false, createLoops); 75 Graphs.addAllVertices(this, g.vertexSet()); 76 addSquareEdges(g, createLoops); 77 78 if (g instanceof ListenableGraph) { 79 ((ListenableGraph<V, E>) g).addGraphListener( 80 new GraphListener<V, E>() { 81 public void edgeAdded(GraphEdgeChangeEvent<V, E> e) 82 { 83 E edge = e.getEdge(); 84 addEdgesStartingAt( 85 g, 86 g.getEdgeSource(edge), 87 g.getEdgeTarget(edge), 88 createLoops); 89 addEdgesStartingAt( 90 g, 91 g.getEdgeTarget(edge), 92 g.getEdgeSource(edge), 93 createLoops); 94 } 95 96 public void edgeRemoved(GraphEdgeChangeEvent<V, E> e) 97 { GraphSquare.super.removeAllEdges(edgeSet()); 99 addSquareEdges(g, createLoops); 100 } 101 102 public void vertexAdded(GraphVertexChangeEvent<V> e) 103 { 104 } 105 106 public void vertexRemoved(GraphVertexChangeEvent<V> e) 107 { 108 } 109 }); 110 } 111 } 112 113 115 118 public E addEdge(V sourceVertex, V targetVertex) 119 { 120 throw new UnsupportedOperationException (UNMODIFIABLE); 121 } 122 123 126 public boolean addEdge(V sourceVertex, V targetVertex, E e) 127 { 128 throw new UnsupportedOperationException (UNMODIFIABLE); 129 } 130 131 134 public boolean addVertex(V v) 135 { 136 throw new UnsupportedOperationException (UNMODIFIABLE); 137 } 138 139 142 public boolean removeAllEdges(Collection<? extends E> edges) 143 { 144 throw new UnsupportedOperationException (UNMODIFIABLE); 145 } 146 147 150 public Set<E> removeAllEdges(V sourceVertex, V targetVertex) 151 { 152 throw new UnsupportedOperationException (UNMODIFIABLE); 153 } 154 155 158 public boolean removeAllVertices(Collection<? extends V> vertices) 159 { 160 throw new UnsupportedOperationException (UNMODIFIABLE); 161 } 162 163 166 public boolean removeEdge(E e) 167 { 168 throw new UnsupportedOperationException (UNMODIFIABLE); 169 } 170 171 174 public E removeEdge(V sourceVertex, V targetVertex) 175 { 176 throw new UnsupportedOperationException (UNMODIFIABLE); 177 } 178 179 182 public boolean removeVertex(V v) 183 { 184 throw new UnsupportedOperationException (UNMODIFIABLE); 185 } 186 187 private void addEdgesStartingAt( 188 final Graph<V, E> g, 189 final V v, 190 final V u, 191 boolean createLoops) 192 { 193 if (!g.containsEdge(v, u)) { 194 return; 195 } 196 197 final List<V> adjVertices = Graphs.neighborListOf(g, u); 198 199 for (int i = 0; i < adjVertices.size(); i++) { 200 final V w = adjVertices.get(i); 201 202 if (g.containsEdge(u, w) && ((v != w) || createLoops)) { 203 super.addEdge(v, w); 204 } 205 } 206 } 207 208 private void addSquareEdges(Graph<V, E> g, boolean createLoops) 209 { 210 for (V v : g.vertexSet()) { 211 List<V> adjVertices = Graphs.neighborListOf(g, v); 212 213 for (int i = 0; i < adjVertices.size(); i++) { 214 addEdgesStartingAt(g, v, adjVertices.get(i), createLoops); 215 } 216 } 217 } 218 } 219 | Popular Tags |