001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2003 Refractions Research 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; either
010: * version 2.1 of the License, or (at your option) any later version.
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: * Refractions Research Inc. Can be found on the web at:
018: * http://www.refractions.net/
019: */
020: package org.geotools.data.oracle.sdo;
021:
022: import java.util.AbstractList;
023:
024: /**
025: * Used to provide a List API of an ordinate array.
026: * <p>
027: * Insertions are not supported</p>
028: *
029: * @see net.refractions.jspatial.jts
030: * @author jgarnett, Refractions Reasearch Inc.
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/oracle-spatial/src/main/java/org/geotools/data/oracle/sdo/OrdinateList.java $
032: * @version CVS Version
033: */
034: public class OrdinateList extends AbstractList {
035: final double ARRAY[];
036: final int OFFSET;
037: final int LEN; // number of things in stuff
038: final int SIZE; // number of "elements"
039: final int START;
040: final int END;
041: final int STEP;
042:
043: public OrdinateList(double array[]) {
044: this (array, 0, 1);
045: }
046:
047: public OrdinateList(double array[], int offset, int len) {
048: this (array, offset, len, 0, array.length);
049: }
050:
051: public OrdinateList(double array[], int offset, int len, int start,
052: int end) {
053: START = start;
054: END = end;
055: ARRAY = array;
056: OFFSET = offset;
057: LEN = len;
058: SIZE = Math.abs(START - END) / LEN;
059: STEP = START < END ? LEN : -LEN;
060:
061: if (ARRAY.length % LEN != 0) {
062: throw new IllegalArgumentException(
063: "You have requested Coordiantes of "
064: + LEN
065: + " ordinates. "
066: + "This is inconsistent with an array of length "
067: + ARRAY.length);
068: }
069: }
070:
071: /**
072: * Used to grab value from array.
073: * <p>
074: * Description of get.
075: * </p>
076: * @param index
077: *
078: * @see java.util.List#get(int)
079: */
080: public Object get(int index) {
081: return new Double(getDouble(index));
082: }
083:
084: /** Quick double access */
085: public double getDouble(int index) {
086: rangeCheck(index);
087: return ARRAY[START + STEP * index + OFFSET];
088: }
089:
090: public double[] toDoubleArray() {
091: double array[] = new double[size()];
092: for (int i = 0; i < size(); i++) {
093: array[i] = getDouble(i);
094: }
095: return array;
096: }
097:
098: /**
099: * Check if the given index is in range. If not, throw an appropriate
100: * runtime exception. This method does *not* check if the index is
101: * negative: It is always used immediately prior to an array access,
102: * which throws an ArrayIndexOutOfBoundsException if index is negative.
103: */
104: private void rangeCheck(int index) {
105: if (index >= SIZE)
106: throw new IndexOutOfBoundsException("Index: " + index
107: + ", Size: " + SIZE);
108: }
109:
110: /**
111: * Used to
112: * <p>
113: * Description of size.
114: * </p>
115: *
116: * @see java.util.Collection#size()
117: */
118: public int size() {
119: return SIZE;
120: }
121: }
|