001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.structure.basic;
018:
019: import java.util.ArrayList;
020: import java.util.HashSet;
021: import java.util.Iterator;
022:
023: import org.geotools.graph.structure.DirectedEdge;
024: import org.geotools.graph.structure.DirectedNode;
025: import org.geotools.graph.structure.Edge;
026: import org.geotools.graph.structure.Node;
027:
028: /**
029: * Basic implementation of DirectedEdge.
030: *
031: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
032: *
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/structure/basic/BasicDirectedEdge.java $
034: */
035: public class BasicDirectedEdge extends BasicGraphable implements
036: DirectedEdge {
037:
038: /** in node **/
039: private DirectedNode m_in;
040:
041: /** out node **/
042: private DirectedNode m_out;
043:
044: /** Contstructs a new DirectedEdge.
045: *
046: * @param in The in node of the edge.
047: * @param out The out node of the edge.
048: */
049: public BasicDirectedEdge(DirectedNode in, DirectedNode out) {
050: super ();
051: m_in = in;
052: m_out = out;
053: }
054:
055: /**
056: * @see DirectedEdge#getInNode()
057: */
058: public DirectedNode getInNode() {
059: return (m_in);
060: }
061:
062: /**
063: * @see DirectedEdge#getOutNode()
064: */
065: public DirectedNode getOutNode() {
066: return (m_out);
067: }
068:
069: /**
070: * Returns the in node.
071: *
072: * @see Edge#getNodeA()
073: */
074: public Node getNodeA() {
075: return (m_in);
076: }
077:
078: /**
079: * Returns the out node.
080: *
081: * @see Edge#getNodeB()
082: */
083: public Node getNodeB() {
084: return (m_out);
085: }
086:
087: /**
088: * @see Edge#getOtherNode(Node)
089: */
090: public Node getOtherNode(Node node) {
091: return (m_in.equals(node) ? m_out : m_out.equals(node) ? m_in
092: : null);
093: }
094:
095: /**
096: * Removes the edge from the out list of the in node and from the in list of
097: * the out node. Nodes are switched and then the edge is added to the in list
098: * of the new out node, and to the out list of the new in node.
099: *
100: * @see Edge#reverse()
101: *
102: */
103: public void reverse() {
104: //remove edge from adjacent nodes
105: m_in.removeOut(this );
106: m_out.removeIn(this );
107:
108: //swap nodes
109: DirectedNode tmp = m_in;
110: m_in = m_out;
111: m_out = tmp;
112:
113: //re add nodes
114: m_in.addOut(this );
115: m_out.addIn(this );
116: }
117:
118: /**
119: * Returns an iterator over all edges incident to both the in and out nodes.
120: *
121: * @see org.geotools.graph.structure.Graphable#getRelated()
122: */
123: public Iterator getRelated() {
124: HashSet related = new HashSet();
125:
126: //add all edges incident to both nodes
127: related.addAll(m_in.getEdges());
128: related.addAll(m_out.getEdges());
129:
130: //remove this edge
131: related.remove(this );
132:
133: return (related.iterator());
134:
135: //
136: // ArrayList related = new ArrayList();
137: //
138: // related.addAll(m_in.getInEdges());
139: //
140: // //add out edges, look for an opposite edge, it will have already
141: // // been added so dont add it
142: // for (Iterator itr = m_out.getOutEdges().iterator(); itr.hasNext();) {
143: // DirectedEdge de = (DirectedEdge)itr.next();
144: // switch(de.compareNodes(this)) {
145: // case OPPOSITE_NODE_ORIENTATION: continue;
146: // }
147: // related.add(de);
148: // }
149: //
150: // //look for duplicate edges (same direction) if not equal add
151: // // dont add opposite edges
152: // // dont add loops
153: // for (Iterator itr = m_in.getOutEdges().iterator(); itr.hasNext();) {
154: // DirectedEdge de = (DirectedEdge)itr.next();
155: // switch(de.compareNodes(this)) {
156: // case EQUAL_NODE_ORIENTATION:
157: // if (!de.equals(this))
158: // related.add(de);
159: // continue;
160: // case OPPOSITE_NODE_ORIENTATION:
161: // continue;
162: // case UNEQUAL_NODE_ORIENTATION:
163: // if (de.getNodeA().equals(de.getNodeB())) continue;
164: // related.add(de);
165: // }
166: // }
167: //
168: // return(related.iterator());
169: }
170:
171: /**
172: * Returns an iterator over the in edges of the in node.
173: *
174: * @see org.geotools.graph.structure.DirectedGraphable#getInRelated()
175: */
176: public Iterator getInRelated() {
177: ArrayList in = new ArrayList();
178: for (Iterator itr = m_in.getInEdges().iterator(); itr.hasNext();) {
179: DirectedEdge de = (DirectedEdge) itr.next();
180: //this check has to be because the edge could be a loop in which case
181: // it is in related to itself
182: if (!de.equals(this ))
183: in.add(de);
184: }
185: return (in.iterator());
186: }
187:
188: /**
189: * Returns an iterator over the out edges of the out node.
190: *
191: * @see org.geotools.graph.structure.DirectedGraphable#getOutRelated()
192: */
193: public Iterator getOutRelated() {
194: ArrayList out = new ArrayList();
195: for (Iterator itr = m_out.getOutEdges().iterator(); itr
196: .hasNext();) {
197: DirectedEdge de = (DirectedEdge) itr.next();
198: //this check has to be because the edge could be a loop in which case
199: // it is in related to itself
200: if (!de.equals(this ))
201: out.add(de);
202: }
203: return (out.iterator());
204: }
205:
206: /**
207: * @see Edge#compareNodes(Edge)
208: */
209: public int compareNodes(Edge other) {
210: if (other instanceof DirectedEdge) {
211: DirectedEdge de = (DirectedEdge) other;
212: if (de.getInNode().equals(m_in)
213: && de.getOutNode().equals(m_out))
214: return (EQUAL_NODE_ORIENTATION);
215: if (de.getInNode().equals(m_out)
216: && de.getOutNode().equals(m_in))
217: return (OPPOSITE_NODE_ORIENTATION);
218: }
219: return (UNEQUAL_NODE_ORIENTATION);
220: }
221: }
|