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.Iterator;
021:
022: import org.geotools.graph.structure.Edge;
023: import org.geotools.graph.structure.Node;
024:
025: /**
026: * Basic implementation of Edge.
027: *
028: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
029: *
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/structure/basic/BasicEdge.java $
031: */
032: public class BasicEdge extends BasicGraphable implements Edge {
033:
034: /** A node */
035: private Node m_nodeA;
036:
037: /** B node */
038: private Node m_nodeB;
039:
040: /**
041: * Constructs a new edge.
042: *
043: * @param nodeA A node of edge.
044: * @param nodeB B node of edge.
045: */
046: public BasicEdge(Node nodeA, Node nodeB) {
047: super ();
048: m_nodeA = nodeA;
049: m_nodeB = nodeB;
050: }
051:
052: /**
053: * @see Edge#getNodeA()
054: */
055: public Node getNodeA() {
056: return (m_nodeA);
057: }
058:
059: /**
060: * @see Edge#getNodeB()
061: */
062: public Node getNodeB() {
063: return (m_nodeB);
064: }
065:
066: /**
067: * Returns null if the specified node is neither the A node or the B node.
068: *
069: * @see Edge#getOtherNode(Node)
070: */
071: public Node getOtherNode(Node node) {
072: if (node.equals(m_nodeA))
073: return (m_nodeB);
074: if (node.equals(m_nodeB))
075: return (m_nodeA);
076: return (null);
077: }
078:
079: /**
080: * Returns all edges that are adjacent to both the A and B nodes. This
081: * iterator is generated by calculating an underlying collection upon each
082: * method call.
083: *
084: * @see org.geotools.graph.structure.Graphable#getRelated()
085: */
086: public Iterator getRelated() {
087: ArrayList adj = new ArrayList();
088:
089: for (Iterator itr = m_nodeA.getEdges().iterator(); itr
090: .hasNext();) {
091: Edge e = (Edge) itr.next();
092: switch (e.compareNodes(this )) {
093: case EQUAL_NODE_ORIENTATION: //same node orientation
094: if (e.equals(this ))
095: continue;
096: case OPPOSITE_NODE_ORIENTATION: //opposite node orientation
097: case UNEQUAL_NODE_ORIENTATION: //different
098: adj.add(e);
099: }
100: }
101:
102: for (Iterator itr = m_nodeB.getEdges().iterator(); itr
103: .hasNext();) {
104: Edge e = (Edge) itr.next();
105: switch (e.compareNodes(this )) {
106: case EQUAL_NODE_ORIENTATION:
107: case OPPOSITE_NODE_ORIENTATION:
108: continue; //edges already added from other node
109: case UNEQUAL_NODE_ORIENTATION:
110: adj.add(e);
111: }
112: }
113:
114: return (adj.iterator());
115: }
116:
117: /**
118: * @see Edge#reverse()
119: */
120: public void reverse() {
121: Node n = m_nodeA;
122: m_nodeA = m_nodeB;
123: m_nodeB = n;
124: }
125:
126: /**
127: * @see Edge#compareNodes(Edge)
128: */
129: public int compareNodes(Edge other) {
130: if (m_nodeA.equals(other.getNodeA())
131: && m_nodeB.equals(other.getNodeB()))
132: return (EQUAL_NODE_ORIENTATION);
133:
134: if ((m_nodeA.equals(other.getNodeA()) && m_nodeB.equals(other
135: .getNodeB()))
136: || (m_nodeA.equals(other.getNodeB()) && m_nodeB
137: .equals(other.getNodeA())))
138: return (OPPOSITE_NODE_ORIENTATION);
139:
140: return (UNEQUAL_NODE_ORIENTATION);
141: }
142:
143: /**
144: * Returns ([A node.toString()],[B node.toString()]).
145: */
146: public String toString() {
147: return (super .toString() + " (" + m_nodeA.getID() + ","
148: + m_nodeB.getID() + ")");
149: }
150: }
|