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.build.basic;
018:
019: import java.util.HashMap;
020:
021: import org.geotools.graph.build.GraphBuilder;
022: import org.geotools.graph.build.GraphGenerator;
023: import org.geotools.graph.structure.Edge;
024: import org.geotools.graph.structure.Graph;
025: import org.geotools.graph.structure.Graphable;
026: import org.geotools.graph.structure.Node;
027:
028: /**
029: * An implementation of GraphGenerator. Graphs are generated as follows:<BR>
030: * <UL>
031: * <LI>Objects added to the generator are 2 element object arrays.</LI>
032: * <LI>The elements of the array represent the objects modelled by the nodes.
033: * <LI>The object array itself is the object modelled by the edges.
034: * <LI>As each object array is added to the generator:
035: * <UL>
036: * <LI>A node lookup table is queried using the elements of the object array.
037: * <LI>If a node lookup returns null, a new node is created for its respective
038: * object.
039: * <LI>A new edge is created incident to the two looked up nodes.
040: * <LI>The underlying object of the edge is set to the be object array.
041: * </UL>
042: * </UL>
043: *
044: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
045: *
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/build/basic/BasicGraphGenerator.java $
047: */
048: public class BasicGraphGenerator implements GraphGenerator {
049:
050: /** The underlying builder **/
051: private GraphBuilder m_builder;
052:
053: /** object to node lookup table **/
054: private HashMap m_obj2graphable;
055:
056: /**
057: * Constructs a new generator.
058: */
059: public BasicGraphGenerator() {
060: m_obj2graphable = new HashMap();
061: setGraphBuilder(new BasicGraphBuilder());
062: }
063:
064: /**
065: * @see GraphGenerator#add(Object)
066: */
067: public Graphable add(Object obj) {
068: Object[] objs = (Object[]) obj;
069:
070: Node n1, n2;
071:
072: //look up first node and create if necessary
073: if ((n1 = (Node) m_obj2graphable.get(objs[0])) == null) {
074: n1 = getGraphBuilder().buildNode();
075: n1.setObject(objs[0]);
076:
077: getGraphBuilder().addNode(n1);
078: m_obj2graphable.put(objs[0], n1);
079: }
080:
081: //look up second node and create if necessary
082: if ((n2 = (Node) m_obj2graphable.get(objs[1])) == null) {
083: n2 = getGraphBuilder().buildNode();
084: n2.setObject(objs[1]);
085:
086: getGraphBuilder().addNode(n2);
087: m_obj2graphable.put(objs[1], n2);
088: }
089:
090: //create edge and set underlying object
091: Edge e = getGraphBuilder().buildEdge(n1, n2);
092: e.setObject(obj);
093:
094: getGraphBuilder().addEdge(e);
095:
096: return (e);
097: }
098:
099: /**
100: * @see GraphGenerator#get(Object)
101: */
102: public Graphable get(Object obj) {
103: Object[] objs = (Object[]) obj;
104: Node n1 = (Node) m_obj2graphable.get(objs[0]);
105: Node n2 = (Node) m_obj2graphable.get(objs[1]);
106:
107: return (n1.getEdge(n2));
108: }
109:
110: /**
111: * @see GraphGenerator#remove(Object)
112: */
113: public Graphable remove(Object obj) {
114: Object[] objs = (Object[]) obj;
115:
116: Node n1 = (Node) m_obj2graphable.get(objs[0]);
117: Node n2 = (Node) m_obj2graphable.get(objs[1]);
118:
119: Edge e = n1.getEdge(n2);
120: getGraphBuilder().removeEdge(e);
121:
122: return (e);
123: }
124:
125: /**
126: * @see GraphGenerator#setGraphBuilder(GraphBuilder)
127: */
128: public void setGraphBuilder(GraphBuilder builder) {
129: m_builder = builder;
130: }
131:
132: /**
133: * @see GraphGenerator#getGraphBuilder()
134: */
135: public GraphBuilder getGraphBuilder() {
136: return (m_builder);
137: }
138:
139: /**
140: * @see GraphGenerator#getGraph()
141: */
142: public Graph getGraph() {
143: return (m_builder.getGraph());
144: }
145:
146: }
|