You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2012/12/16 11:04:30 UTC

svn commit: r1422494 - /sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java

Author: desruisseaux
Date: Sun Dec 16 10:04:29 2012
New Revision: 1422494

URL: http://svn.apache.org/viewvc?rev=1422494&view=rev
Log:
Moved methods for slightly more "natural" flow (no code change).

Modified:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java?rev=1422494&r1=1422493&r2=1422494&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java (original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java Sun Dec 16 10:04:29 2012
@@ -507,7 +507,7 @@ public abstract class AbstractEnvelope i
      *
      * <ul>
      *   <li>If {@code isNull() == true}, then {@code isEmpty() == true}</li>
-     *   <li>If {@code #isEmpty() == false}, then {@code isNull() == false}</li>
+     *   <li>If {@code isEmpty() == false}, then {@code isNull() == false}</li>
      *   <li>The converse of the above-cited rules are not always true.</li>
      * </ul>
      *
@@ -765,74 +765,66 @@ public abstract class AbstractEnvelope i
     }
 
     /**
-     * Formats this envelope in the <cite>Well Known Text</cite> (WKT) format.
-     * The output is of the form "{@code BOX}<var>n</var>{@code D(}{@linkplain #getLowerCorner()
-     * lower corner}{@code ,}{@linkplain #getUpperCorner() upper corner}{@code )}"
-     * where <var>n</var> is the {@linkplain #getDimension() number of dimensions}.
-     * Example:
+     * Compares to the specified envelope for equality up to the specified tolerance value.
+     * The tolerance value {@code eps} can be either relative to the {@linkplain #getSpan(int)
+     * envelope span} along each dimension or can be an absolute value (as for example some
+     * ground resolution of a {@linkplain org.opengis.coverage.grid.GridCoverage.GridCoverage
+     * grid coverage}).
      *
-     * {@preformat wkt
-     *   BOX3D(-90 -180 0, 90 180 1)
-     * }
+     * <ul>
+     *   <li>If {@code epsIsRelative} is set to {@code true}, the actual tolerance value for a
+     *       given dimension <var>i</var> is {@code eps} × {@code span} where {@code span}
+     *       is the maximum of {@linkplain #getSpan(int) this envelope span} and the specified
+     *       envelope span along dimension <var>i</var>.</li>
+     *   <li>If {@code epsIsRelative} is set to {@code false}, the actual tolerance value for a
+     *       given dimension <var>i</var> is {@code eps}.</li>
+     * </ul>
      *
-     * The string returned by this method can be {@linkplain GeneralEnvelope#GeneralEnvelope(String) parsed}
-     * by the {@code GeneralEnvelope} constructor.
+     * {@note Relative tolerance value (as opposed to absolute tolerance value) help to workaround
+     * the fact that tolerance value are CRS dependent. For example the tolerance value need to be
+     * smaller for geographic CRS than for UTM projections, because the former typically has a
+     * [-180…180]° range while the later can have a range of thousands of meters.}
      *
-     * @return This envelope as a {@code BOX2D} or {@code BOX3D} (most typical dimensions) in WKT format.
-     */
-    @Override
-    public String toString() {
-        return toString(this);
-    }
-
-    /**
-     * Implementation of the public {@link #toString()} and {@link Envelopes#toWKT(Envelope)} methods
-     * for formatting a {@code BOX} element from an envelope in <cite>Well Known Text</cite> (WKT) format.
+     * {@section Coordinate Reference System}
+     * To be considered equal, the two envelopes must have the same {@linkplain #getDimension() dimension}
+     * and their CRS must be {@linkplain org.apache.sis.util.Utilities#equalsIgnoreMetadata equals,
+     * ignoring metadata}. If at least one envelope has a null CRS, then the CRS are ignored and the
+     * ordinate values are compared as if the CRS were equal.
      *
-     * @param  envelope The envelope to format.
-     * @return The envelope as a {@code BOX2D} or {@code BOX3D} (most typical dimensions) in WKT format.
+     * @param  other The envelope to compare with.
+     * @param  eps   The tolerance value to use for numerical comparisons.
+     * @param  epsIsRelative {@code true} if the tolerance value should be relative to
+     *         axis length, or {@code false} if it is an absolute value.
+     * @return {@code true} if the given object is equal to this envelope up to the given tolerance value.
      *
-     * @see GeneralEnvelope#GeneralEnvelope(String)
-     * @see org.apache.sis.measure.CoordinateFormat
-     * @see org.apache.sis.io.wkt
+     * @see #contains(Envelope, boolean)
+     * @see #intersects(Envelope, boolean)
      */
-    static String toString(final Envelope envelope) {
-        final int dimension = envelope.getDimension();
-        final DirectPosition lower = envelope.getLowerCorner();
-        final DirectPosition upper = envelope.getUpperCorner();
-        final StringBuilder buffer = new StringBuilder(64).append("BOX").append(dimension).append("D(");
-        for (int i=0; i<dimension; i++) {
-            if (i != 0) {
-                buffer.append(' ');
-            }
-            trimFractionalPart(buffer.append(lower.getOrdinate(i)));
+    public boolean equals(final Envelope other, final double eps, final boolean epsIsRelative) {
+        ensureNonNull("other", other);
+        final int dimension = getDimension();
+        if (other.getDimension() != dimension || !equalsIgnoreMetadata(
+                getCoordinateReferenceSystem(), other.getCoordinateReferenceSystem(), false))
+        {
+            return false;
         }
-        buffer.append(',');
+        final DirectPosition lower = other.getLowerCorner();
+        final DirectPosition upper = other.getUpperCorner();
         for (int i=0; i<dimension; i++) {
-            trimFractionalPart(buffer.append(' ').append(upper.getOrdinate(i)));
-        }
-        return buffer.append(')').toString();
-    }
-
-    /**
-     * Returns a hash value for this envelope.
-     */
-    @Override
-    public int hashCode() {
-        final int dimension = getDimension();
-        int code = 1;
-        boolean p = true;
-        do {
-            for (int i=0; i<dimension; i++) {
-                final long bits = doubleToLongBits(p ? getLower(i) : getUpper(i));
-                code = 31 * code + (((int) bits) ^ (int) (bits >>> 32));
+            double ε = eps;
+            if (epsIsRelative) {
+                final double span = Math.max(getSpan(i), other.getSpan(i));
+                if (span > 0 && span < Double.POSITIVE_INFINITY) {
+                    ε *= span;
+                }
+            }
+            if (!epsilonEqual(getLower(i), lower.getOrdinate(i), ε) ||
+                !epsilonEqual(getUpper(i), upper.getOrdinate(i), ε))
+            {
+                return false;
             }
-        } while ((p = !p) == false);
-        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
-        if (crs != null) {
-            code += crs.hashCode();
         }
-        return code;
+        return true;
     }
 
     /**
@@ -876,66 +868,74 @@ public abstract class AbstractEnvelope i
     }
 
     /**
-     * Compares to the specified envelope for equality up to the specified tolerance value.
-     * The tolerance value {@code eps} can be either relative to the {@linkplain #getSpan(int)
-     * envelope span} along each dimension or can be an absolute value (as for example some
-     * ground resolution of a {@linkplain org.opengis.coverage.grid.GridCoverage.GridCoverage
-     * grid coverage}).
+     * Returns a hash value for this envelope.
+     */
+    @Override
+    public int hashCode() {
+        final int dimension = getDimension();
+        int code = 1;
+        boolean p = true;
+        do {
+            for (int i=0; i<dimension; i++) {
+                final long bits = doubleToLongBits(p ? getLower(i) : getUpper(i));
+                code = 31 * code + (((int) bits) ^ (int) (bits >>> 32));
+            }
+        } while ((p = !p) == false);
+        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
+        if (crs != null) {
+            code += crs.hashCode();
+        }
+        return code;
+    }
+
+    /**
+     * Formats this envelope in the <cite>Well Known Text</cite> (WKT) format.
+     * The output is of the form "{@code BOX}<var>n</var>{@code D(}{@linkplain #getLowerCorner()
+     * lower corner}{@code ,}{@linkplain #getUpperCorner() upper corner}{@code )}"
+     * where <var>n</var> is the {@linkplain #getDimension() number of dimensions}.
+     * Example:
      *
-     * <ul>
-     *   <li>If {@code epsIsRelative} is set to {@code true}, the actual tolerance value for a
-     *       given dimension <var>i</var> is {@code eps} × {@code span} where {@code span}
-     *       is the maximum of {@linkplain #getSpan(int) this envelope span} and the specified
-     *       envelope span along dimension <var>i</var>.</li>
-     *   <li>If {@code epsIsRelative} is set to {@code false}, the actual tolerance value for a
-     *       given dimension <var>i</var> is {@code eps}.</li>
-     * </ul>
+     * {@preformat wkt
+     *   BOX3D(-90 -180 0, 90 180 1)
+     * }
      *
-     * {@note Relative tolerance value (as opposed to absolute tolerance value) help to workaround
-     * the fact that tolerance value are CRS dependent. For example the tolerance value need to be
-     * smaller for geographic CRS than for UTM projections, because the former typically has a
-     * [-180…180]° range while the later can have a range of thousands of meters.}
+     * The string returned by this method can be {@linkplain GeneralEnvelope#GeneralEnvelope(String) parsed}
+     * by the {@code GeneralEnvelope} constructor.
      *
-     * {@section Coordinate Reference System}
-     * To be considered equal, the two envelopes must have the same {@linkplain #getDimension() dimension}
-     * and their CRS must be {@linkplain org.apache.sis.util.Utilities#equalsIgnoreMetadata equals,
-     * ignoring metadata}. If at least one envelope has a null CRS, then the CRS are ignored and the
-     * ordinate values are compared as if the CRS were equal.
+     * @return This envelope as a {@code BOX2D} or {@code BOX3D} (most typical dimensions) in WKT format.
+     */
+    @Override
+    public String toString() {
+        return toString(this);
+    }
+
+    /**
+     * Implementation of the public {@link #toString()} and {@link Envelopes#toWKT(Envelope)} methods
+     * for formatting a {@code BOX} element from an envelope in <cite>Well Known Text</cite> (WKT) format.
      *
-     * @param  other The envelope to compare with.
-     * @param  eps   The tolerance value to use for numerical comparisons.
-     * @param  epsIsRelative {@code true} if the tolerance value should be relative to
-     *         axis length, or {@code false} if it is an absolute value.
-     * @return {@code true} if the given object is equal to this envelope up to the given tolerance value.
+     * @param  envelope The envelope to format.
+     * @return The envelope as a {@code BOX2D} or {@code BOX3D} (most typical dimensions) in WKT format.
      *
-     * @see #contains(Envelope, boolean)
-     * @see #intersects(Envelope, boolean)
+     * @see GeneralEnvelope#GeneralEnvelope(String)
+     * @see org.apache.sis.measure.CoordinateFormat
+     * @see org.apache.sis.io.wkt
      */
-    public boolean equals(final Envelope other, final double eps, final boolean epsIsRelative) {
-        ensureNonNull("other", other);
-        final int dimension = getDimension();
-        if (other.getDimension() != dimension || !equalsIgnoreMetadata(
-                getCoordinateReferenceSystem(), other.getCoordinateReferenceSystem(), false))
-        {
-            return false;
-        }
-        final DirectPosition lower = other.getLowerCorner();
-        final DirectPosition upper = other.getUpperCorner();
+    static String toString(final Envelope envelope) {
+        final int dimension = envelope.getDimension();
+        final DirectPosition lower = envelope.getLowerCorner();
+        final DirectPosition upper = envelope.getUpperCorner();
+        final StringBuilder buffer = new StringBuilder(64).append("BOX").append(dimension).append("D(");
         for (int i=0; i<dimension; i++) {
-            double ε = eps;
-            if (epsIsRelative) {
-                final double span = Math.max(getSpan(i), other.getSpan(i));
-                if (span > 0 && span < Double.POSITIVE_INFINITY) {
-                    ε *= span;
-                }
-            }
-            if (!epsilonEqual(getLower(i), lower.getOrdinate(i), ε) ||
-                !epsilonEqual(getUpper(i), upper.getOrdinate(i), ε))
-            {
-                return false;
+            if (i != 0) {
+                buffer.append(' ');
             }
+            trimFractionalPart(buffer.append(lower.getOrdinate(i)));
         }
-        return true;
+        buffer.append(',');
+        for (int i=0; i<dimension; i++) {
+            trimFractionalPart(buffer.append(' ').append(upper.getOrdinate(i)));
+        }
+        return buffer.append(')').toString();
     }
 
     /**