Class EdgeList

  1  
  2  
  3  
  4 /*
  5  * Copyright (c) 2016 Vivid Solutions.
  6  *
  7  * All rights reserved. This program and the accompanying materials
  8  * are made available under the terms of the Eclipse Public License 2.0
  9  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 10  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 11  * and the Eclipse Distribution License is available at
 12  *
 13  * http://www.eclipse.org/org/documents/edl-v10.php.
 14  */
 15 package org.locationtech.jts.geomgraph;
 16  
 17 import java.io.PrintStream;
 18 import java.util.ArrayList;
 19 import java.util.Collection;
 20 import java.util.Iterator;
 21 import java.util.List;
 22 import java.util.Map;
 23 import java.util.TreeMap;
 24  
 25 import org.locationtech.jts.geom.Coordinate;
 26 import org.locationtech.jts.noding.OrientedCoordinateArray;
 27  
 28  
 29 /**
 30  * A EdgeList is a list of Edges.  It supports locating edges
 31  * that are pointwise equals to a target edge.
 32  * @version 1.7
 33  */
 34 public class EdgeList
 35 {
 36   private List edges = new ArrayList();
 37   /**
 38    * An index of the edges, for fast lookup.
 39    *
 40    */
 41   private Map ocaMap = new TreeMap();
 42  
 43   public EdgeList() {
 44   }
 45  
 46   /**
 47    * Insert an edge unless it is already in the list
 48    */
 49   public void add(Edge e)
 50   {
 51     edges.add(e);
 52     OrientedCoordinateArray oca = new OrientedCoordinateArray(e.getCoordinates());
 53     ocaMap.put(oca, e);
 54   }
 55  
 56   public void addAll(Collection edgeColl)
 57   {
 58     for (Iterator i = edgeColl.iterator(); i.hasNext(); ) {
 59       add((Edge) i.next());
 60     }
 61   }
 62  
 63   public List getEdges() { return edges; }
 64  
 65   /**
 66    * If there is an edge equal to e already in the list, return it.
 67    * Otherwise return null.
 68    * @return  equal edge, if there is one already in the list
 69    *          null otherwise
 70    */
 71   public Edge findEqualEdge(Edge e)
 72   {
 73     OrientedCoordinateArray oca = new OrientedCoordinateArray(e.getCoordinates());
 74     // will return null if no edge matches
 75     Edge matchEdge = (Edge) ocaMap.get(oca);
 76     return matchEdge; 
 77   }
 78   
 79   public Iterator iterator() { return edges.iterator(); }
 80  
 81   public Edge get(int i) { return (Edge) edges.get(i); }
 82  
 83   /**
 84    * If the edge e is already in the list, return its index.
 85    * @return  index, if e is already in the list
 86    *          -1 otherwise
 87    */
 88   public int findEdgeIndex(Edge e)
 89   {
 90     for (int i = 0; i < edges.size(); i++) {
 91       if ( ((Edge) edges.get(i)).equals(e) ) return i;
 92     }
 93     return -1;
 94   }
 95  
 96   public void print(PrintStream out)
 97   {
 98     out.print("MULTILINESTRING ( ");
 99     for (int j = 0; j < edges.size(); j++) {
100       Edge e = (Edge) edges.get(j);
101       if (j > 0) out.print(",");
102       out.print("(");
103       Coordinate[] pts = e.getCoordinates();
104       for (int i = 0; i < pts.length; i++) {
105         if (i > 0) out.print(",");
106         out.print(pts[i].x + " " + pts[i].y);
107       }
108       out.println(")");
109     }
110     out.print(")  ");
111   }
112  
113  
114 }
115