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.opt;
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: * Optimized implementation of Edge.
027: *
028: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
029: * @see Edge
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/structure/opt/OptEdge.java $
031: */
032: public class OptEdge extends OptGraphable implements Edge {
033:
034: /** a node **/
035: private OptNode m_nodeA;
036:
037: /** b node **/
038: private OptNode m_nodeB;
039:
040: /**
041: * Constructs a new optimized edge.
042: *
043: * @param nodeA A node of edge.
044: * @param nodeB B node of edge.
045: */
046: public OptEdge(OptNode nodeA, OptNode nodeB) {
047: m_nodeA = nodeA;
048: m_nodeB = nodeB;
049: }
050:
051: /**
052: * @see Edge#getNodeA()
053: */
054: public Node getNodeA() {
055: return (m_nodeA);
056: }
057:
058: /**
059: * @see Edge#getNodeB()
060: */
061: public Node getNodeB() {
062: return (m_nodeB);
063: }
064:
065: /**
066: * @see Edge#getOtherNode(Node)
067: */
068: public Node getOtherNode(Node node) {
069: return (m_nodeA.equals(node) ? m_nodeB
070: : m_nodeB.equals(node) ? m_nodeA : null);
071: }
072:
073: /**
074: * @see Edge#reverse()
075: */
076: public void reverse() {
077: OptNode tmp = m_nodeA;
078: m_nodeA = m_nodeB;
079: m_nodeB = tmp;
080: }
081:
082: /**
083: * @see Edge#compareNodes(Edge)
084: */
085: public int compareNodes(Edge other) {
086: if (m_nodeA.equals(other.getNodeA())
087: && m_nodeB.equals(other.getNodeB()))
088: return (Edge.EQUAL_NODE_ORIENTATION);
089:
090: if (m_nodeB.equals(other.getNodeA())
091: && m_nodeA.equals(other.getNodeB()))
092: return (Edge.OPPOSITE_NODE_ORIENTATION);
093:
094: return (Edge.UNEQUAL_NODE_ORIENTATION);
095: }
096:
097: /**
098: * @see org.geotools.graph.structure.Graphable#getRelated()
099: */
100: public Iterator getRelated() {
101: return (new RelatedIterator(this ));
102: }
103:
104: public class RelatedIterator implements Iterator {
105:
106: private Iterator m_itr;
107:
108: public RelatedIterator(OptEdge edge) {
109: ArrayList edges = new ArrayList(m_nodeA.getDegree()
110: + m_nodeB.getDegree() - 2
111: - m_nodeA.getEdges(m_nodeB).size());
112:
113: //add all edges of node A except this edge
114: for (int i = 0; i < m_nodeA.getEdgeArray().length; i++) {
115: Edge e = (Edge) m_nodeA.getEdgeArray()[i];
116: if (!e.equals(edge))
117: edges.add(m_nodeA.getEdgeArray()[i]);
118: }
119:
120: //add only edges from node b that are node shared with node a
121: for (int i = 0; i < m_nodeB.getEdgeArray().length; i++) {
122: Edge e = (Edge) m_nodeB.getEdgeArray()[i];
123: if (!e.getOtherNode(m_nodeB).equals(m_nodeA))
124: edges.add(e);
125: }
126:
127: m_itr = edges.iterator();
128: }
129:
130: public void remove() {
131: throw new UnsupportedOperationException(getClass()
132: .getName()
133: + "#remove() not supported.");
134: }
135:
136: public boolean hasNext() {
137: return (m_itr.hasNext());
138: }
139:
140: public Object next() {
141: return (m_itr.next());
142: }
143:
144: }
145: }
|