/* * Geotools 2 - OpenSource mapping toolkit * (C) 2003, Geotools Project Management Committee (PMC) * (C) 2001, Institut de Recherche pour le Développement * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * This package contains documentation from OpenGIS specifications. * OpenGIS consortium's work is fully acknowledged here. */ package org.geotools.gc; // J2SE dependencies import java.awt.Rectangle; import java.awt.image.Raster; import java.awt.image.RenderedImage; import java.io.Serializable; import java.lang.ref.Reference; import java.lang.ref.WeakReference; import java.util.Arrays; import org.geotools.pt.Dimensioned; import org.geotools.resources.Utilities; import org.geotools.resources.i18n.ErrorKeys; import org.geotools.resources.i18n.Errors; import org.opengis.gc.GC_GridRange; /** * Defines a range of grid coverage coordinates. * * @source $URL$ * @version $Id$ * @author OpenGIS * @author Martin Desruisseaux * * @see GC_GridRange * * @deprecated Replaced by {@link org.geotools.coverage.grid.GeneralGridRange}. */ public class GridRange implements Dimensioned, Serializable { /** * Serial number for interoperability with different versions. */ private static final long serialVersionUID = 1452569710967224145L; /** * Minimum and maximum grid ordinates. The first half contains minimum * ordinates, while the last half contains maximum ordinates. */ private final int[] index; /** * OpenGIS object returned by {@link #toOpenGIS}. * It may be a hard or a weak reference. */ transient Object proxy; /** * Check if ordinate values in the minimum index are less than or * equal to the corresponding ordinate value in the maximum index. * * @throws IllegalArgumentException if an ordinate value in the minimum index is not * less than or equal to the corresponding ordinate value in the maximum index. */ private void checkCoherence() throws IllegalArgumentException { final int dimension = index.length/2; for (int i=0; i= 0) { return index[dimension + index.length/2]; } else throw new ArrayIndexOutOfBoundsException(dimension); } /** * Returns the number of integer grid coordinates along the specified dimension. * This is equals to getUpper(dimension)-getLower(dimension). */ public int getLength(final int dimension) { return index[dimension+index.length/2] - index[dimension]; } /** * Returns the valid minimum inclusive grid coordinates along all dimensions. */ public int[] getLowers() { final int[] lo = new int[index.length/2]; System.arraycopy(index, 0, lo, 0, lo.length); return lo; } /** * Returns the valid maximum exclusive grid coordinates along all dimensions. */ public int[] getUppers() { final int[] hi = new int[index.length/2]; System.arraycopy(index, index.length/2, hi, 0, hi.length); return hi; } /** * Returns a new grid range that encompass only some dimensions of this grid range. * This method copy this grid range's index into a new grid range, beginning at * dimension lower and extending to dimension upper-1. * Thus the dimension of the subgrid range is upper-lower. * * @param lower The first dimension to copy, inclusive. * @param upper The last dimension to copy, exclusive. * @return The subgrid range. * @throws IndexOutOfBoundsException if an index is out of bounds. */ public GridRange getSubGridRange(final int lower, final int upper) { final int curDim = index.length/2; final int newDim = upper-lower; if (lower<0 || lower>curDim) { throw new IndexOutOfBoundsException(Errors.format( ErrorKeys.ILLEGAL_ARGUMENT_$2, "lower", new Integer(lower))); } if (newDim<0 || upper>curDim) { throw new IndexOutOfBoundsException(Errors.format( ErrorKeys.ILLEGAL_ARGUMENT_$2, "upper", new Integer(upper))); } final GridRange gridRange = new GridRange(newDim); System.arraycopy(index, lower, gridRange.index, 0, newDim); System.arraycopy(index, lower+curDim, gridRange.index, newDim, newDim); return gridRange; } /** * Returns a {@link Rectangle} with the same bounds as this GridRange. * This is a convenience method for interoperability with Java2D. * * @throws IllegalStateException if this grid range is not two-dimensional. */ public Rectangle toRectangle() throws IllegalStateException { if (index.length == 4) { return new Rectangle(index[0], index[1], index[2]-index[0], index[3]-index[1]); } else { throw new IllegalStateException(Errors.format( ErrorKeys.NOT_TWO_DIMENSIONAL_$1, new Integer(getDimension()))); } } /** * Returns a hash value for this grid range. * This value need not remain consistent between * different implementations of the same class. */ public int hashCode() { int code=45123678; if (index!=null) { for (int i=index.length; --i>=0;) { code = code*31 + index[i]; } } return code; } /** * Compares the specified object with * this grid range for equality. */ public boolean equals(final Object object) { if (object instanceof GridRange) { final GridRange that = (GridRange) object; return Arrays.equals(this.index, that.index); } return false; } /** * Returns a string représentation of this grid range. * The returned string is implementation dependent. It * is usually provided for debugging purposes. */ public String toString() { final int dimension = index.length/2; final StringBuffer buffer=new StringBuffer(Utilities.getShortClassName(this)); buffer.append('['); for (int i=0; i