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.lang.reflect.Array;
023: import java.util.AbstractList;
024:
025: /**
026: * Used to provide a List API of an ordinate array.
027: * <p>
028: * Insertions are not supported</p>
029: *
030: * @see net.refractions.jspatial.jts
031: * @author jgarnett, Refractions Reasearch Inc.
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/oracle-spatial/src/main/java/org/geotools/data/oracle/sdo/AttributeList.java $
033: * @version CVS Version
034: */
035: public class AttributeList extends AbstractList {
036: final Object ARRAY;
037: final int OFFSET;
038: final int LEN;
039: final int SIZE;
040: final int START;
041: final int END;
042: final int STEP;
043:
044: public AttributeList(Object array) {
045: this (array, 0, 1);
046: }
047:
048: public AttributeList(Object array, int offset, int len) {
049: this (array, offset, len, 0, Array.getLength(array));
050: }
051:
052: public AttributeList(Object array, int offset, int len, int start,
053: int end) {
054: START = start;
055: END = end;
056: ARRAY = array;
057: OFFSET = offset;
058: LEN = len;
059: SIZE = Math.abs(START - END) / LEN;
060: STEP = START < END ? LEN : -LEN;
061:
062: if (!ARRAY.getClass().isArray())
063: throw new IllegalArgumentException(
064: "Provided argument was not an array");
065:
066: if (Array.getLength(ARRAY) % LEN != 0) {
067: throw new IllegalArgumentException(
068: "You have requested Coordiantes of "
069: + LEN
070: + " ordinates. "
071: + "This is inconsistent with an array of length "
072: + Array.getLength(ARRAY));
073: }
074: }
075:
076: /**
077: * Used to grab value from array.
078: * <p>
079: * Description of get.
080: * </p>
081: * @param index
082: *
083: * @see java.util.List#get(int)
084: */
085: public Object get(int index) {
086: rangeCheck(index);
087: return Array.get(ARRAY, START + STEP * index + OFFSET);
088: }
089:
090: /** Quick double access */
091: public double getDouble(int index) {
092: rangeCheck(index);
093: return Array.getDouble(ARRAY, START + STEP * index + OFFSET);//ARRAY[ START+STEP*index + OFFSET ];
094: }
095:
096: public String getString(int index) {
097: rangeCheck(index);
098: return Array.get(ARRAY, START + STEP * index + OFFSET)
099: .toString();
100: }
101:
102: public double[] toDoubleArray() {
103: double array[] = new double[size()];
104: for (int i = 0; i < size(); i++) {
105: array[i] = getDouble(i);
106: }
107: return array;
108: }
109:
110: public Object[] toObjectArray() {
111: Object array[] = new Object[size()];
112: for (int i = 0; i < size(); i++) {
113: array[i] = get(i);
114: }
115: return array;
116: }
117:
118: /**
119: * Check if the given index is in range. If not, throw an appropriate
120: * runtime exception. This method does *not* check if the index is
121: * negative: It is always used immediately prior to an array access,
122: * which throws an ArrayIndexOutOfBoundsException if index is negative.
123: */
124: private void rangeCheck(int index) {
125: if (index >= SIZE)
126: throw new IndexOutOfBoundsException("Index: " + index
127: + ", Size: " + SIZE);
128: }
129:
130: /**
131: * Used to
132: * <p>
133: * Description of size.
134: * </p>
135: *
136: * @see java.util.Collection#size()
137: */
138: public int size() {
139: return SIZE;
140: }
141: }
|