001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: * (C) 2006, Geomatys
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: * This package contains documentation from OpenGIS specifications.
018: * OpenGIS consortium's work is fully acknowledged here.
019: */
020: package org.geotools.coverage.grid;
021:
022: // J2SE dependencies
023: import java.io.Serializable;
024: import java.util.Arrays;
025:
026: // OpenGIS dependencies
027: import org.opengis.coverage.grid.Grid; // For javadoc
028: import org.opengis.coverage.grid.GridPoint; // For javadoc
029: import org.opengis.coverage.grid.GridCoordinates;
030: import org.opengis.geometry.DirectPosition;
031:
032: /**
033: * Holds the set of grid coordinates that specifies the location of the
034: * {@linkplain GridPoint grid point} within the {@linkplain Grid grid}.
035: *
036: * @since 2.4
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/coverage/grid/GeneralGridCoordinates.java $
038: * @version $Id: GeneralGridCoordinates.java 24925 2007-03-27 20:12:08Z jgarnett $
039: * @author Martin Desruisseaux
040: */
041: public class GeneralGridCoordinates implements GridCoordinates,
042: Serializable {
043: /**
044: * For cross-version compatibility.
045: */
046: private static final long serialVersionUID = 8146318677770695383L;
047:
048: /**
049: * The grid coordinates.
050: */
051: private final int[] coordinates;
052:
053: /**
054: * Creates a grid coordinates of the specified dimension.
055: * All coordinates are initially set to 0.
056: */
057: public GeneralGridCoordinates(final int dimension) {
058: coordinates = new int[dimension];
059: }
060:
061: /**
062: * Creates a grid coordinates initialized to the specified values.
063: */
064: public GeneralGridCoordinates(final int[] coordinates) {
065: this .coordinates = (int[]) coordinates.clone();
066: }
067:
068: /**
069: * Creates a grid coordinates initialized to the specified values
070: * in the specified range.
071: */
072: GeneralGridCoordinates(final int[] coordinates, final int lower,
073: final int upper) {
074: final int length = upper - lower;
075: this .coordinates = new int[length];
076: System.arraycopy(coordinates, lower, this .coordinates, 0,
077: length);
078: }
079:
080: /**
081: * Returns the number of dimensions. This method is equivalent to
082: * <code>{@linkplain #getCoordinateValues()}.length</code>. It is
083: * provided for efficienty.
084: */
085: public int getDimension() {
086: return coordinates.length;
087: }
088:
089: /**
090: * Returns one integer value for each dimension of the grid. The ordering of these coordinate
091: * values shall be the same as that of the elements of {@link Grid#getAxisNames}. The value of
092: * a single coordinate shall be the number of offsets from the origin of the grid in the
093: * direction of a specific axis.
094: *
095: * @return A copy of the coordinates. Changes in the returned array will not be reflected
096: * back in this {@code GeneralGridCoordinates} object.
097: */
098: public int[] getCoordinateValues() {
099: return (int[]) coordinates.clone();
100: }
101:
102: /**
103: * Returns the coordinate value at the specified dimension. This method is equivalent to
104: * <code>{@linkplain #getCoordinateValues()}[<var>i</var>]</code>. It is provided for
105: * efficienty.
106: */
107: public int getCoordinateValue(final int i) {
108: return coordinates[i];
109: }
110:
111: /**
112: * Sets the coordinate value at the specified dimension (optional operation).
113: *
114: * @param i The index of the value to set.
115: * @param value The new value.
116: * @throws UnsupportedOperationException if this grid coordinates is not modifiable.
117: */
118: public void setCoordinateValue(final int i, final int value)
119: throws UnsupportedOperationException {
120: coordinates[i] = value;
121: }
122:
123: /**
124: * Returns a string representation of this grid coordinates.
125: */
126: public String toString() {
127: final StringBuffer buffer = new StringBuffer("GridCoordinates");
128: for (int i = 0; i < coordinates.length; i++) {
129: buffer.append(i == 0 ? '[' : ',');
130: buffer.append(coordinates[i]);
131: }
132: buffer.append(']');
133: return buffer.toString();
134: }
135:
136: /**
137: * Returns a hash code value for this object.
138: *
139: * @todo Use {@link Arrays#hashCode(int[])} when we will be allowed to compile for J2SE 1.5.
140: */
141: public int hashCode() {
142: int code = (int) serialVersionUID;
143: for (int i = 0; i < coordinates.length; i++) {
144: code = code * 37 + coordinates[i];
145: }
146: return code;
147: }
148:
149: /**
150: * Compares this grid coordinates with the specified object for equality.
151: */
152: public boolean equals(final Object object) {
153: if (object == this ) {
154: // Slight optimization.
155: return true;
156: }
157: if (object != null && object.getClass().equals(getClass())) {
158: final GeneralGridCoordinates that = (GeneralGridCoordinates) object;
159: return Arrays.equals(this .coordinates, that.coordinates);
160: }
161: return false;
162: }
163:
164: /**
165: * Returns a clone of this coordinates.
166: */
167: public Object clone() {
168: return new GeneralGridCoordinates(coordinates);
169: }
170:
171: /**
172: * An immutable flavor of {@link GridCoordinates}.
173: */
174: static final class Immutable extends GeneralGridCoordinates {
175: /**
176: * For cross-version compatibility.
177: */
178: private static final long serialVersionUID = -7723383411061425866L;
179:
180: /**
181: * Creates a grid coordinates initialized to the specified values
182: * in the specified range.
183: */
184: Immutable(final int[] coordinates, final int lower,
185: final int upper) {
186: super (coordinates, lower, upper);
187: }
188:
189: /**
190: * Do not allows modification of this grid coordinates.
191: */
192: public void setCoordinateValue(final int i, final int value)
193: throws UnsupportedOperationException {
194: throw new UnsupportedOperationException();
195: }
196: }
197: }
|