Class CoordinateArraySequenceFactory

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.geom.impl;
 13  
 14 import java.io.Serializable;
 15  
 16 import org.locationtech.jts.geom.Coordinate;
 17 import org.locationtech.jts.geom.CoordinateSequence;
 18 import org.locationtech.jts.geom.CoordinateSequenceFactory;
 19  
 20 /**
 21  * Creates {@link CoordinateSequence}s represented as an array of {@link Coordinate}s.
 22  *
 23  * @version 1.7
 24  */
 25 public final class CoordinateArraySequenceFactory
 26     implements CoordinateSequenceFactory, Serializable
 27 {
 28   private static final long serialVersionUID = -4099577099607551657L;
 29   private static final CoordinateArraySequenceFactory instanceObject = new CoordinateArraySequenceFactory();
 30  
 31   private CoordinateArraySequenceFactory() {
 32   }
 33  
 34   private Object readResolve() {
 35       // http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
 36     return CoordinateArraySequenceFactory.instance();
 37   }
 38  
 39   /**
 40    * Returns the singleton instance of {@link CoordinateArraySequenceFactory}
 41    */
 42   public static CoordinateArraySequenceFactory instance() {
 43     return instanceObject;
 44   }
 45  
 46   /**
 47    * Returns a {@link CoordinateArraySequence} based on the given array (the array is
 48    * not copied).
 49    *
 50    * @param coordinates
 51    *            the coordinates, which may not be null nor contain null
 52    *            elements
 53    */
 54   public CoordinateSequence create(Coordinate[] coordinates) {
 55     return new CoordinateArraySequence(coordinates);
 56   }
 57  
 58   /**
 59    * @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(org.locationtech.jts.geom.CoordinateSequence)
 60    */
 61   public CoordinateSequence create(CoordinateSequence coordSeq) {
 62     return new CoordinateArraySequence(coordSeq);
 63   }
 64  
 65   /**
 66    * The created sequence dimension is clamped to be <= 3.
 67    * 
 68    * @see org.locationtech.jts.geom.CoordinateSequenceFactory#create(int, int)
 69    *
 70    */
 71   public CoordinateSequence create(int size, int dimension) {
 72     if (dimension > 3)
 73       dimension = 3;
 74       //throw new IllegalArgumentException("dimension must be <= 3");
 75     
 76     // handle bogus dimension
 77     if (dimension < 2)
 78       dimension = 2;      
 79     
 80     return new CoordinateArraySequence(size, dimension);
 81   }
 82   
 83   public CoordinateSequence create(int size, int dimension, int measures) {
 84     int spatial = dimension - measures;
 85     
 86     if (measures > 1) {
 87       measures = 1// clip measures
 88       //throw new IllegalArgumentException("measures must be <= 1");
 89     }
 90     if ((spatial) > 3) {
 91       spatial = 3// clip spatial dimension
 92       //throw new IllegalArgumentException("spatial dimension must be <= 3");
 93     }
 94     
 95     if (spatial < 2)
 96       spatial = 2// handle bogus spatial dimension
 97     
 98     return new CoordinateArraySequence(size, spatial+measures, measures);
 99   }
100 }
101