01: package com.vividsolutions.jts.planargraph;
02:
03: import java.util.*;
04:
05: /**
06: * A subgraph of a {@link PlanarGraph}.
07: * A subgraph may contain any subset of {@link Edge}s
08: * from the parent graph.
09: * It will also automatically contain all {@link DirectedEdge}s
10: * and {@link Node}s associated with those edges.
11: * No new objects are created when edges are added -
12: * all associated components must already exist in the parent graph.
13: */
14: public class Subgraph {
15: protected PlanarGraph parentGraph;
16: protected Set edges = new HashSet();
17: protected List dirEdges = new ArrayList();
18: protected NodeMap nodeMap = new NodeMap();
19:
20: /**
21: * Creates a new subgraph of the given {@link PlanarGraph}
22: *
23: * @param parentGraph the parent graph
24: */
25: public Subgraph(PlanarGraph parentGraph) {
26: this .parentGraph = parentGraph;
27: }
28:
29: /**
30: * Gets the {@link PlanarGraph} which this subgraph
31: * is part of.
32: *
33: * @return the parent PlanarGraph
34: */
35: public PlanarGraph getParent() {
36: return parentGraph;
37: }
38:
39: /**
40: * Adds an {@link Edge} to the subgraph.
41: * The associated {@link DirectedEdge}s and {@link Node}s
42: * are also added.
43: *
44: * @param e the edge to add
45: */
46: public void add(Edge e) {
47: if (edges.contains(e))
48: return;
49:
50: edges.add(e);
51: dirEdges.add(e.getDirEdge(0));
52: dirEdges.add(e.getDirEdge(1));
53: nodeMap.add(e.getDirEdge(0).getFromNode());
54: nodeMap.add(e.getDirEdge(1).getFromNode());
55: }
56:
57: /**
58: * Returns an {@link Iterator} over the {@link DirectedEdge}s in this graph,
59: * in the order in which they were added.
60: *
61: * @return an iterator over the directed edges
62: *
63: * @see #add(Edge)
64: */
65: public Iterator dirEdgeIterator() {
66: return dirEdges.iterator();
67: }
68:
69: /**
70: * Returns an {@link Iterator} over the {@link Edge}s in this graph,
71: * in the order in which they were added.
72: *
73: * @return an iterator over the edges
74: *
75: * @see #add(Edge)
76: */
77: public Iterator edgeIterator() {
78: return edges.iterator();
79: }
80:
81: /**
82: * Returns an {@link Iterator} over the {@link Node}s in this graph.
83: * @return an iterator over the nodes
84: */
85: public Iterator nodeIterator() {
86: return nodeMap.iterator();
87: }
88:
89: /**
90: * Tests whether an {@link Edge} is contained in this subgraph
91: * @param e the edge to test
92: * @return <code>true</code> if the edge is contained in this subgraph
93: */
94: public boolean contains(Edge e) {
95: return edges.contains(e);
96: }
97:
98: }
|