sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1467941 - in /sis/branches/JDK7/sis-referencing/src: main/java/org/apache/sis/geometry/ test/java/org/apache/sis/geometry/ test/java/org/apache/sis/test/suite/
Date Mon, 15 Apr 2013 11:34:19 GMT
Author: desruisseaux
Date: Mon Apr 15 11:34:17 2013
New Revision: 1467941

URL: http://svn.apache.org/r1467941
Log:
Implemented GeneralEnvelope.subEnvelope(int, int).

Added:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
  (with props)
    sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
  (with props)
Modified:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
    sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/AbstractEnvelopeTest.java
    sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/GeneralEnvelopeTest.java
    sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java?rev=1467941&r1=1467940&r2=1467941&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -80,6 +80,17 @@ class ArrayEnvelope extends AbstractEnve
     CoordinateReferenceSystem crs;
 
     /**
+     * Creates a new envelope using the given array of ordinate values. This constructor
stores
+     * the given reference directly; it does <strong>not</strong> clone the given
array. This is
+     * the desired behavior for proper working of {@link SubEnvelope}.
+     *
+     * @param ordinates The array of ordinate values to store directly (not cloned).
+     */
+    ArrayEnvelope(final double[] ordinates) {
+        this.ordinates = ordinates;
+    }
+
+    /**
      * Constructs an envelope defined by two corners given as direct positions.
      * If at least one corner is associated to a CRS, then the new envelope will also
      * be associated to that CRS.
@@ -315,7 +326,7 @@ scanNumber: while ((i += Character.charC
     }
 
     /**
-     * Returns the index of the first valid lower ordinate value in the {@link #ordinates}
array.
+     * Returns the index of the first valid ordinate value of the lower corner in the {@link
#ordinates} array.
      * This is always 0, unless this envelope is a {@link SubEnvelope}.
      *
      * <p>See {@link #endIndex()} for the list of methods that need to be also overridden
@@ -326,7 +337,7 @@ scanNumber: while ((i += Character.charC
     }
 
     /**
-     * Returns the index after the last valid lower ordinate value in the {@link #ordinates}
array.
+     * Returns the index after the last valid ordinate value of the lower corner in the {@link
#ordinates} array.
      * This is always {@code ordinates.length >>> 1}, unless this envelope is a
{@link SubEnvelope}.
      *
      * <p>Unless otherwise indicated by a "{@code // Must be overridden in SubEnvelope}"
comment, every methods

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java?rev=1467941&r1=1467940&r2=1467941&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralEnvelope.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -112,6 +112,17 @@ public class GeneralEnvelope extends Arr
     private static volatile Field ordinatesField;
 
     /**
+     * Creates a new envelope using the given array of ordinate values. This constructor
stores
+     * the given reference directly; it does <strong>not</strong> clone the given
array. This is
+     * the desired behavior for proper working of {@link SubEnvelope}.
+     *
+     * @param ordinates The array of ordinate values to store directly (not cloned).
+     */
+    GeneralEnvelope(final double[] ordinates) {
+        super(ordinates);
+    }
+
+    /**
      * Constructs an envelope defined by two corners given as direct positions.
      * If at least one corner is associated to a CRS, then the new envelope will also
      * be associated to that CRS.
@@ -290,28 +301,30 @@ public class GeneralEnvelope extends Arr
      * (<var>x</var><sub>min</sub>, <var>y</var><sub>min</sub>,
<var>z</var><sub>min</sub>,
      *  <var>x</var><sub>max</sub>, <var>y</var><sub>max</sub>,
<var>z</var><sub>max</sub>)
      *
-     * @param ordinates The new ordinate values.
+     * @param corners Ordinates of the new lower corner followed by the new upper corner.
      */
-    public void setEnvelope(final double... ordinates) {
-        if ((ordinates.length & 1) != 0) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.OddArrayLength_1, ordinates.length));
-        }
-        final int dimension = ordinates.length >>> 1;
-        final int expected  = getDimension();
-        if (dimension != expected) {
-            throw new MismatchedDimensionException(Errors.format(
-                    Errors.Keys.MismatchedDimension_3, "ordinates", expected, dimension));
-        }
-        doSetEnvelope(ordinates);
+    public void setEnvelope(final double... corners) {
+        verifyArrayLength(ordinates.length >>> 1, corners);
+        System.arraycopy(corners, 0, ordinates, 0, ordinates.length);
     }
 
     /**
-     * Sets the ordinate values after their validity has been verified.
-     * Must be overridden by {@link SubEnvelope}.
+     * Verifies that the given array of ordinate values has the expected length
+     * for the given number of dimensions.
+     *
+     * @param dimension The dimension of the envelope.
+     * @param corners The user-provided array of ordinate values.
      */
-    void doSetEnvelope(final double[] values) {
-        System.arraycopy(values, 0, ordinates, 0, ordinates.length);
+    static void verifyArrayLength(final int dimension, final double[] corners) {
+        if ((corners.length & 1) != 0) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.OddArrayLength_1, corners.length));
+        }
+        final int d = corners.length >>> 1;
+        if (d != dimension) {
+            throw new MismatchedDimensionException(Errors.format(
+                    Errors.Keys.MismatchedDimension_3, "ordinates", dimension, d));
+        }
     }
 
     /**
@@ -886,6 +899,41 @@ public class GeneralEnvelope extends Arr
     }
 
     /**
+     * Returns a view over this envelope that encompass only some dimensions. The returned
object is "live":
+     * changes applied on the original envelope is reflected in the sub-envelope view, and
conversely.
+     *
+     * <p>This method is useful for querying and updating only some dimensions.
+     * For example in order to expand only the horizontal component of a four dimensional
+     * (<var>x</var>,<var>y</var>,<var>z</var>,<var>t</var>)
envelope, one can use:</p>
+     *
+     * {@preformat
+     *     envelope.subEnvelope(0, 2).add(myPosition2D);
+     * }
+     *
+     * If the sub-envelope needs to be independent from the original envelope, use the following
idiom:
+     *
+     * {@preformat
+     *     GeneralEnvelope copy = envelope.subEnvelope(0, 2).clone();
+     * }
+     *
+     * The sub-envelope is initialized with a {@code null} {@linkplain #getCoordinateReferenceSystem()
CRS}.
+     * This method does not compute a sub-CRS because it may not be needed, or the sub-CRS
may be already
+     * known by the caller.
+     *
+     * @param  beginIndex The index of the first valid ordinate value of the corners.
+     * @param  endIndex   The index after the last valid ordinate value of the corners.
+     * @return The sub-envelope of dimension {@code endIndex - beginIndex}.
+     * @throws IndexOutOfBoundsException If an index is out of bounds.
+     */
+    // Must be overridden in SubEnvelope
+    public GeneralEnvelope subEnvelope(final int beginIndex, final int endIndex) throws IndexOutOfBoundsException
{
+        ensureValidIndexRange(ordinates.length >>> 1, beginIndex, endIndex);
+        return new SubEnvelope(ordinates, beginIndex, endIndex);
+        // Do check if we could return "this" as an optimization, in order to keep the
+        // method contract simpler (i.e. the returned envelope CRS is always null).
+    }
+
+    /**
      * Returns a deep copy of this envelope.
      *
      * @return A clone of this envelope.

Added: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java?rev=1467941&view=auto
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
(added)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.geometry;
+
+/*
+ * Do not add dependency to java.awt.Rectangle2D in this class, because not every platforms
+ * support Java2D (e.g. Android),  or applications that do not need it may want to avoid
to
+ * force installation of the Java2D module (e.g. JavaFX/SWT).
+ */
+import java.util.Arrays;
+
+import static org.apache.sis.util.ArgumentChecks.ensureValidIndex;
+import static org.apache.sis.util.ArgumentChecks.ensureValidIndexRange;
+
+
+/**
+ * A view over a sub-set of the dimensions of a {@link GeneralEnvelope}.
+ * This class doesn't keep any reference to the original envelope;
+ * only the internal array is shared.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+final class SubEnvelope extends GeneralEnvelope {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = 7241242611077979466L;
+
+    /**
+     * The index of the first valid ordinate value of the lower corner in the {@link #ordinates}
array.
+     *
+     * @see ArrayEnvelope#beginIndex()
+     */
+    private final int beginIndex;
+
+    /**
+     * The index after the last valid ordinate value of the lower corner  in the {@link #ordinates}
array.
+     *
+     * @see ArrayEnvelope#endIndex()
+     */
+    private final int endIndex;
+
+    /**
+     * Creates a new envelope over a portion of the given array. This constructor stores
the given
+     * reference directly; it does <strong>not</strong> clone the given array.
This is the desired
+     * behavior for allowing the {@code SubEnvelope} view to be "live".
+     *
+     * @param ordinates  The array of ordinate values to store directly (not cloned).
+     * @param beginIndex The index of the first valid ordinate value of the lower corner
in the ordinates array.
+     * @param endIndex   The index after the last valid ordinate value of the lower corner
in the ordinates array.
+     */
+    SubEnvelope(final double[] ordinates, final int beginIndex, final int endIndex) {
+        super(ordinates);
+        this.beginIndex = beginIndex;
+        this.endIndex = endIndex;
+    }
+
+    /**
+     * Returns the index of the first valid ordinate value of the lower corner in the ordinates
array.
+     * This information is used by super-class methods.
+     */
+    @Override
+    final int beginIndex() {
+        return beginIndex;
+    }
+
+    /**
+     * Returns the index after the last valid ordinate value of the lower corner in the ordinates
array.
+     * This information is used by super-class methods.
+     */
+    @Override
+    final int endIndex() {
+        return endIndex;
+    }
+
+    /**
+     * Returns the number of dimensions in the portion of the array used by this {@code SubEnvelope}.
+     */
+    @Override
+    public int getDimension() {
+        return endIndex - beginIndex;
+    }
+
+    /**
+     * Must be overridden, since the super-class method does not handle the index range
+     * for performance reasons.
+     */
+    @Override
+    public double getLower(final int dimension) throws IndexOutOfBoundsException {
+        ensureValidIndex(endIndex, dimension);
+        return ordinates[dimension + beginIndex];
+    }
+
+    /**
+     * Must be overridden, since the super-class method does not handle the index range
+     * for performance reasons.
+     */
+    @Override
+    public double getUpper(final int dimension) throws IndexOutOfBoundsException {
+        ensureValidIndex(endIndex, dimension);
+        return ordinates[dimension + beginIndex + (ordinates.length >>> 1)];
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public void setRange(int dimension, final double lower, final double upper)
+            throws IndexOutOfBoundsException
+    {
+        ensureValidIndex(endIndex, dimension);
+        dimension += beginIndex;
+        ordinates[dimension + (ordinates.length >>> 1)] = upper;
+        ordinates[dimension] = lower;
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public void setEnvelope(final double... corners) {
+        final int dimension = getDimension();
+        verifyArrayLength(dimension, corners);
+        final int d = ordinates.length >>> 1;
+        System.arraycopy(corners, 0,         ordinates, beginIndex,     dimension);
+        System.arraycopy(corners, dimension, ordinates, beginIndex + d, dimension);
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public boolean isAllNaN() {
+        final int d = ordinates.length >>> 1;
+        for (int i=beginIndex; i<endIndex; i++) {
+            if (!Double.isNaN(ordinates[i]) || !Double.isNaN(ordinates[i+d])) {
+                return false;
+            }
+        }
+        assert isEmpty() : this;
+        return true;
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public void setToNaN() {
+        final int d = ordinates.length >>> 1;
+        Arrays.fill(ordinates, beginIndex,   endIndex,   Double.NaN);
+        Arrays.fill(ordinates, beginIndex+d, endIndex+d, Double.NaN);
+        assert isAllNaN() : this;
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public int hashCode() {
+        return hashCodeByAPI();
+    }
+
+    /**
+     * Must be overridden, since the super-class method processes the full array as a whole.
+     */
+    @Override
+    public boolean equals(final Object object) {
+        return equalsByAPI(object);
+    }
+
+    /**
+     * Must be overridden, since the super-class method does not handle the index range
+     * for performance reasons.
+     */
+    @Override
+    public GeneralEnvelope subEnvelope(final int b, final int e) throws IndexOutOfBoundsException
{
+        ensureValidIndexRange(endIndex - beginIndex, b, e);
+        return new SubEnvelope(ordinates, b + beginIndex, e + beginIndex);
+    }
+
+    /**
+     * If the user wants a clone, copy only the relevant part of the ordinates array.
+     */
+    @Override
+    public GeneralEnvelope clone() {
+        final int d = ordinates.length >>> 1;
+        final int dimension = endIndex - beginIndex;
+        final GeneralEnvelope copy = new GeneralEnvelope(endIndex - beginIndex);
+        System.arraycopy(ordinates, beginIndex,     copy.ordinates, 0,         dimension);
+        System.arraycopy(ordinates, beginIndex + d, copy.ordinates, dimension, dimension);
+        copy.crs = crs;
+        return copy;
+    }
+}

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/SubEnvelope.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/AbstractEnvelopeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/AbstractEnvelopeTest.java?rev=1467941&r1=1467940&r2=1467941&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/AbstractEnvelopeTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/AbstractEnvelopeTest.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -50,7 +50,7 @@ public final strictfp class AbstractEnve
      * Enumeration of implementations to be tested.
      * The {@code LAST} constant is for stopping the loops.
      */
-    private static final int GENERAL=0, IMMUTABLE=1, RECTANGLE=2, LAST=3;
+    private static final int GENERAL=0, IMMUTABLE=1, RECTANGLE=2, SUBENVELOPE=3, LAST=4;
 
     /**
      * The coordinate reference system used for the tests.
@@ -85,6 +85,17 @@ public final strictfp class AbstractEnve
                 envelope = new Envelope2D(WGS84, xmin, ymin, xmax - xmin, ymax - ymin);
                 break;
             }
+            case SUBENVELOPE: {
+                final GeneralEnvelope ge = new GeneralEnvelope(5);
+                ge.setCoordinateReferenceSystem(WGS84);
+                ge.setRange(1, xmin, xmax);
+                ge.setRange(2, ymin, ymax);
+                ge.setRange(0, 2, 3); // Following values will be verified in verifyInvariants(…)
+                ge.setRange(3, 4, 6);
+                ge.setRange(4, 8, 9);
+                envelope = ge.subEnvelope(1, 3);
+                break;
+            }
             default: throw new IllegalArgumentException(String.valueOf(type));
         }
         if (PENDING_NEXT_GEOAPI_RELEASE) {
@@ -94,6 +105,26 @@ public final strictfp class AbstractEnve
     }
 
     /**
+     * Verifies some invariants for the given envelope of the given type.
+     */
+    private static void verifyInvariants(final int type, final Envelope envelope) {
+        assertSame(WGS84, envelope.getCoordinateReferenceSystem());
+        switch (type) {
+            case SUBENVELOPE: {
+                // Asserts that other dimensions in the original envelope has not been modified.
+                final double[] ordinates = ((SubEnvelope) envelope).ordinates;
+                assertEquals(2, ordinates[0], STRICT);
+                assertEquals(3, ordinates[5], STRICT);
+                assertEquals(4, ordinates[3], STRICT);
+                assertEquals(6, ordinates[8], STRICT);
+                assertEquals(8, ordinates[4], STRICT);
+                assertEquals(9, ordinates[9], STRICT);
+                break;
+            }
+        }
+    }
+
+    /**
      * Tests the simple case (no anti-meridian crossing).
      *
      * {@preformat text
@@ -121,24 +152,29 @@ public final strictfp class AbstractEnve
             assertEquals(label, 12, envelope.getMaximum(0), STRICT);
             assertEquals(label,  4, envelope.getMedian (0), STRICT);
             assertEquals(label, 16, envelope.getSpan   (0), STRICT);
-            if (envelope instanceof AbstractEnvelope) {
-                final AbstractEnvelope ext = (AbstractEnvelope) envelope;
-                assertTrue (label, ext.contains  (inside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect, false));
-                assertTrue (label, ext.intersects(intersect, false));
-                assertDisjoint(ext, disjoint);
-                assertContains(ext, contained);
-            }
-            if (envelope instanceof Rectangle2D) {
-                final Rectangle2D ext = (Rectangle2D) envelope;
-                assertTrue (label, ext.contains  (inside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect));
-                assertTrue (label, ext.intersects(intersect));
-                assertDisjoint(ext, disjoint);
-                assertContains(ext, contained);
+            switch (type) {
+                default: {
+                    final AbstractEnvelope ext = (AbstractEnvelope) envelope;
+                    assertTrue (label, ext.contains  (inside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect, false));
+                    assertTrue (label, ext.intersects(intersect, false));
+                    assertDisjoint(ext, disjoint);
+                    assertContains(ext, contained);
+                    break;
+                }
+                case RECTANGLE: {
+                    final Rectangle2D ext = (Rectangle2D) envelope;
+                    assertTrue (label, ext.contains  (inside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect));
+                    assertTrue (label, ext.intersects(intersect));
+                    assertDisjoint(ext, disjoint);
+                    assertContains(ext, contained);
+                    break;
+                }
             }
+            verifyInvariants(type, envelope);
         }
     }
 
@@ -176,26 +212,31 @@ public final strictfp class AbstractEnve
             assertEquals(label, +180, envelope.getMaximum (0), STRICT);
             assertEquals(label, -176, envelope.getMedian  (0), STRICT);
             assertEquals(label,  344, envelope.getSpan    (0), STRICT); // 360° - testSimpleEnvelope()
-            if (envelope instanceof AbstractEnvelope) {
-                final AbstractEnvelope ext = (AbstractEnvelope) envelope;
-                assertTrue (label, ext.contains  (inside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect, false));
-                assertTrue (label, ext.intersects(intersect, false));
-                assertDisjoint(ext, disjoint);
-                assertContains(ext, contained);
-                assertContains(ext, spanning);
-            }
-            if (envelope instanceof Rectangle2D) {
-                final Rectangle2D ext = (Rectangle2D) envelope;
-                assertTrue (label, ext.contains  (inside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect));
-                assertTrue (label, ext.intersects(intersect));
-                assertDisjoint(ext, disjoint);
-                assertContains(ext, contained);
-                assertContains(ext, spanning);
+            switch (type) {
+                default: {
+                    final AbstractEnvelope ext = (AbstractEnvelope) envelope;
+                    assertTrue (label, ext.contains  (inside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect, false));
+                    assertTrue (label, ext.intersects(intersect, false));
+                    assertDisjoint(ext, disjoint);
+                    assertContains(ext, contained);
+                    assertContains(ext, spanning);
+                    break;
+                }
+                case RECTANGLE: {
+                    final Rectangle2D ext = (Rectangle2D) envelope;
+                    assertTrue (label, ext.contains  (inside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect));
+                    assertTrue (label, ext.intersects(intersect));
+                    assertDisjoint(ext, disjoint);
+                    assertContains(ext, contained);
+                    assertContains(ext, spanning);
+                    break;
+                }
             }
+            verifyInvariants(type, envelope);
         }
     }
 
@@ -244,6 +285,7 @@ public final strictfp class AbstractEnve
                 assertTrue (label, ext.intersects(spanning,  false));
                 assertDisjoint(ext, disjoint);
                 assertContains(ext, contained);
+                break;
             }
             if (envelope instanceof Rectangle2D) {
                 final Rectangle2D ext = (Rectangle2D) envelope;
@@ -255,7 +297,9 @@ public final strictfp class AbstractEnve
                 assertTrue (label, ext.intersects(spanning));
                 assertDisjoint(ext, disjoint);
                 assertContains(ext, contained);
+                break;
             }
+            verifyInvariants(type, envelope);
         }
     }
 
@@ -287,26 +331,31 @@ public final strictfp class AbstractEnve
             assertEquals(label, +180, envelope.getMaximum (0), STRICT);
             assertEquals(label, -176, envelope.getMedian  (0), STRICT); // Note the alternance
with the previous test methods.
             assertEquals(label,  NaN, envelope.getSpan    (0), STRICT); // testCrossingAntiMeridianTwice()
+ 360°.
-            if (envelope instanceof AbstractEnvelope) {
-                final AbstractEnvelope ext = (AbstractEnvelope) envelope;
-                assertFalse(label, ext.contains  (wasInside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (spanning,  false));
-                assertTrue (label, ext.intersects(spanning,  false));
-                assertDisjoint(ext, wasIntersect);
-                assertDisjoint(ext, disjoint);
-                assertDisjoint(ext, wasContained);
-            }
-            if (envelope instanceof Rectangle2D) {
-                final Rectangle2D ext = (Rectangle2D) envelope;
-                assertFalse(label, ext.contains  (wasInside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (spanning));
-                assertTrue (label, ext.intersects(spanning));
-                assertDisjoint(ext, wasIntersect);
-                assertDisjoint(ext, disjoint);
-                assertDisjoint(ext, wasContained);
+            switch (type) {
+                default: {
+                    final AbstractEnvelope ext = (AbstractEnvelope) envelope;
+                    assertFalse(label, ext.contains  (wasInside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (spanning,  false));
+                    assertTrue (label, ext.intersects(spanning,  false));
+                    assertDisjoint(ext, wasIntersect);
+                    assertDisjoint(ext, disjoint);
+                    assertDisjoint(ext, wasContained);
+                    break;
+                }
+                case RECTANGLE: {
+                    final Rectangle2D ext = (Rectangle2D) envelope;
+                    assertFalse(label, ext.contains  (wasInside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (spanning));
+                    assertTrue (label, ext.intersects(spanning));
+                    assertDisjoint(ext, wasIntersect);
+                    assertDisjoint(ext, disjoint);
+                    assertDisjoint(ext, wasContained);
+                    break;
+                }
             }
+            verifyInvariants(type, envelope);
         }
     }
 
@@ -331,28 +380,33 @@ public final strictfp class AbstractEnve
             assertEquals(label,  0.0, envelope.getMaximum(0), STRICT);
             assertEquals(label,    0, envelope.getMedian (0), STRICT);
             assertEquals(label,    0, envelope.getSpan   (0), STRICT);
-            if (envelope instanceof AbstractEnvelope) {
-                final AbstractEnvelope ext = (AbstractEnvelope) envelope;
-                assertFalse(label, ext.contains  (wasInside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect, false));
-                assertTrue (label, ext.intersects(intersect, false));
-                assertFalse(label, ext.contains  (spanning,  false));
-                assertFalse(label, ext.intersects(spanning,  false));
-                assertFalse(label, ext.intersects(spanning,  false));
-                assertDisjoint(ext, wasContained);
-            }
-            if (envelope instanceof Rectangle2D) {
-                final Rectangle2D ext = (Rectangle2D) envelope;
-                assertFalse(label, ext.contains  (wasInside));
-                assertFalse(label, ext.contains  (outside));
-                assertFalse(label, ext.contains  (intersect));
-                assertTrue (label, ext.intersects(intersect));
-                assertFalse(label, ext.contains  (spanning));
-                assertFalse(label, ext.intersects(spanning));
-                assertFalse(label, ext.intersects(spanning));
-                assertDisjoint(ext, wasContained);
+            switch (type) {
+                default: {
+                    final AbstractEnvelope ext = (AbstractEnvelope) envelope;
+                    assertFalse(label, ext.contains  (wasInside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect, false));
+                    assertTrue (label, ext.intersects(intersect, false));
+                    assertFalse(label, ext.contains  (spanning,  false));
+                    assertFalse(label, ext.intersects(spanning,  false));
+                    assertFalse(label, ext.intersects(spanning,  false));
+                    assertDisjoint(ext, wasContained);
+                    break;
+                }
+                case RECTANGLE: {
+                    final Rectangle2D ext = (Rectangle2D) envelope;
+                    assertFalse(label, ext.contains  (wasInside));
+                    assertFalse(label, ext.contains  (outside));
+                    assertFalse(label, ext.contains  (intersect));
+                    assertTrue (label, ext.intersects(intersect));
+                    assertFalse(label, ext.contains  (spanning));
+                    assertFalse(label, ext.intersects(spanning));
+                    assertFalse(label, ext.intersects(spanning));
+                    assertDisjoint(ext, wasContained);
+                    break;
+                }
             }
+            verifyInvariants(type, envelope);
         }
     }
 
@@ -382,22 +436,27 @@ public final strictfp class AbstractEnve
             assertEquals(label, +180, envelope.getMaximum (0), STRICT);
             assertEquals(label,  180, envelope.getMedian  (0), STRICT);
             assertEquals(label,  360, envelope.getSpan    (0), STRICT);
-            if (envelope instanceof AbstractEnvelope) {
-                final AbstractEnvelope ext = (AbstractEnvelope) envelope;
-                assertTrue(label, ext.contains(inside));
-                assertTrue(label, ext.contains(wasOutside));
-                assertTrue(label, ext.intersects(intersect, false));
-                assertContains(ext, contained);
-                assertContains(ext, spanning);
-            }
-            if (envelope instanceof Rectangle2D) {
-                final Rectangle2D ext = (Rectangle2D) envelope;
-                assertTrue(label, ext.contains(inside));
-                assertTrue(label, ext.contains(wasOutside));
-                assertTrue(label, ext.intersects(intersect));
-                assertContains(ext, contained);
-                assertContains(ext, spanning);
+            switch (type) {
+                default: {
+                    final AbstractEnvelope ext = (AbstractEnvelope) envelope;
+                    assertTrue(label, ext.contains(inside));
+                    assertTrue(label, ext.contains(wasOutside));
+                    assertTrue(label, ext.intersects(intersect, false));
+                    assertContains(ext, contained);
+                    assertContains(ext, spanning);
+                    break;
+                }
+                case RECTANGLE: {
+                    final Rectangle2D ext = (Rectangle2D) envelope;
+                    assertTrue(label, ext.contains(inside));
+                    assertTrue(label, ext.contains(wasOutside));
+                    assertTrue(label, ext.intersects(intersect));
+                    assertContains(ext, contained);
+                    assertContains(ext, spanning);
+                    break;
+                }
             }
+            verifyInvariants(type, envelope);
         }
     }
 }

Modified: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/GeneralEnvelopeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/GeneralEnvelopeTest.java?rev=1467941&r1=1467940&r2=1467941&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/GeneralEnvelopeTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/GeneralEnvelopeTest.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -43,11 +43,11 @@ import static org.apache.sis.geometry.Ab
  * @module
  */
 @DependsOn(AbstractEnvelopeTest.class)
-public final strictfp class GeneralEnvelopeTest extends TestCase {
+public strictfp class GeneralEnvelopeTest extends TestCase {
     /**
      * The comparison threshold for strict comparisons.
      */
-    private static final double STRICT = 0;
+    static final double STRICT = 0;
 
     /**
      * Tolerance threshold for floating point comparisons.
@@ -56,8 +56,9 @@ public final strictfp class GeneralEnvel
 
     /**
      * Creates a new geographic envelope for the given ordinate values.
+     * This method is overridden by {@link SubEnvelopeTest}.
      */
-    private static GeneralEnvelope create(final double xmin, final double ymin, final double
xmax, final double ymax) {
+    GeneralEnvelope create(final double xmin, final double ymin, final double xmax, final
double ymax) {
         final GeneralEnvelope envelope = new GeneralEnvelope(2);
         envelope.setCoordinateReferenceSystem(WGS84);
         envelope.setEnvelope(xmin, ymin, xmax, ymax);
@@ -68,6 +69,14 @@ public final strictfp class GeneralEnvel
     }
 
     /**
+     * Verifies invariants for the given envelope after each test.
+     * This method is overridden by {@link SubEnvelopeTest}.
+     */
+    void verifyInvariants(final GeneralEnvelope envelope) {
+        assertSame(WGS84, envelope.getCoordinateReferenceSystem());
+    }
+
+    /**
      * Asserts that the given two-dimensional envelope is equals to the given rectangle.
      * The {@code xLower} and {@code xUpper} arguments are the <var>x</var> ordinate
values
      * for the lower and upper corners respectively. The actual {@code xmin} and {@code ymin}
@@ -263,6 +272,10 @@ public final strictfp class GeneralEnvel
         //  ─────┘     └─────
         e2.setRange(0, 10, 90);
         assertIntersectEquals(e1, e2, NaN, ymin, NaN, ymax);
+
+        // Post-test verification, mostly for SubEnvelope.
+        verifyInvariants(e1);
+        verifyInvariants(e2);
     }
 
     /**
@@ -324,6 +337,10 @@ public final strictfp class GeneralEnvel
         //  ─────┘     └─────
         e2.setRange(0, 10, 90);
         assertUnionEquals(e1, e2, +0.0, ymin, -0.0, ymax, true, true);
+
+        // Post-test verification, mostly for SubEnvelope.
+        verifyInvariants(e1);
+        verifyInvariants(e2);
     }
 
     /**
@@ -353,6 +370,8 @@ public final strictfp class GeneralEnvel
 
         p.x = 30; // Add on the left side.
         assertAddEquals(e, p, 80, ymin, 30, ymax);
+
+        verifyInvariants(e);
     }
 
     /**
@@ -377,6 +396,7 @@ public final strictfp class GeneralEnvel
         assertTrue(e.normalize());
         assertEquals("Expect positive zero", Double.doubleToLongBits(+0.0), Double.doubleToLongBits(e.getLower(0)));
         assertEquals("Expect negative zero", Double.doubleToLongBits(-0.0), Double.doubleToLongBits(e.getUpper(0)));
+        verifyInvariants(e);
     }
 
     /**
@@ -389,6 +409,7 @@ public final strictfp class GeneralEnvel
         GeneralEnvelope e = create(-195, -90, +170, +90); // -195° is equivalent to 165°
         assertTrue(e.normalize());
         assertEnvelopeEquals(e, -180, -90, +180, +90);
+        verifyInvariants(e);
     }
 
     /**
@@ -411,6 +432,7 @@ public final strictfp class GeneralEnvel
         e = create(0.0, -10, -0.0, 10);
         assertTrue(e.simplify());
         assertEnvelopeEquals(e, -180, -10, 180, 10);
+        verifyInvariants(e);
     }
 
     /**
@@ -425,6 +447,7 @@ public final strictfp class GeneralEnvel
         assertEquals(-2.0, e.getLower(1), 0.0);
         assertEquals( 3.0, e.getUpper(0), 0.0);
         assertEquals(-1.0, e.getUpper(1), 0.0);
+        verifyInvariants(e);
     }
 
     /**
@@ -574,10 +597,8 @@ public final strictfp class GeneralEnvel
      * Tests {@code GeneralEnvelope} serialization.
      */
     @Test
-    public void testSerialization() {
-        final GeneralEnvelope e1 = new GeneralEnvelope(
-                new double[] {-20, -10},
-                new double[] { 20,  10});
+    public final void testSerialization() {
+        final GeneralEnvelope e1 = create(-20, -10, 20, 10);
         final GeneralEnvelope e2 = assertSerializedEquals(e1);
         assertNotSame(e1, e2);
         validate(e2);

Added: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java?rev=1467941&view=auto
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
(added)
+++ sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.geometry;
+
+import org.junit.Test;
+import org.apache.sis.test.DependsOn;
+
+import static java.lang.Double.NaN;
+import static org.junit.Assert.*;
+import static org.opengis.test.Validators.validate;
+import static org.apache.sis.geometry.AbstractEnvelopeTest.WGS84;
+import static org.apache.sis.geometry.GeneralEnvelopeTest.STRICT;
+
+
+/**
+ * Tests the {@link SubEnvelopeTest} class. This method leverage the tests written for
+ * {@link GeneralEnvelope}, but using a sub-envelope instead than a full envelope.
+ *
+ * @author  Martin Desruisseaux (IRD, Geomatys)
+ * @author  Johann Sorel (Geomatys)
+ * @since   0.3 (derived from geotk-2.4)
+ * @version 0.3
+ * @module
+ */
+@DependsOn(GeneralEnvelopeTest.class)
+public final strictfp class SubEnvelopeTest extends GeneralEnvelopeTest {
+    /**
+     * Creates a new sub-envelope envelope for the given ordinate values.
+     */
+    @Override
+    GeneralEnvelope create(final double xmin, final double ymin, final double xmax, final
double ymax) {
+        final GeneralEnvelope envelope = new GeneralEnvelope(5);
+        envelope.setCoordinateReferenceSystem(WGS84);
+        envelope.setEnvelope(1, 4, xmin, ymin, 5,
+                             2, 7, xmax, ymax, 9);
+        if (PENDING_NEXT_GEOAPI_RELEASE) {
+            validate(envelope);
+        }
+        return envelope.subEnvelope(2, 4);
+    }
+
+    /**
+     * Verifies invariants for the given envelope after each test.
+     */
+    @Override
+    void verifyInvariants(final GeneralEnvelope envelope) {
+        super.verifyInvariants(envelope);
+        final double[] ordinates = envelope.ordinates;
+        assertEquals(1, ordinates[0], STRICT);
+        assertEquals(4, ordinates[1], STRICT);
+        assertEquals(5, ordinates[4], STRICT);
+        assertEquals(2, ordinates[5], STRICT);
+        assertEquals(7, ordinates[6], STRICT);
+        assertEquals(9, ordinates[9], STRICT);
+    }
+
+    /**
+     * Skips this test, since {@code SubEnvelope} provides nothing new compared to the super-class.
+     */
+    @Override
+    public void testWktParsing() {
+    }
+
+    /**
+     * Tests {@link SubEnvelope#setToNaN()}.
+     */
+    @Test
+    public void testSetToNaN() {
+        final GeneralEnvelope e = create(-40, -20, +60, +30);
+        e.setToNaN();
+        assertArrayEquals(new double[] {1, 4, NaN, NaN, 5, 2, 7, NaN, NaN, 9}, e.ordinates,
STRICT);
+        validate(e);
+    }
+
+    /**
+     * Tests two distinct sub-envelopes for equality.
+     */
+    @Test
+    @Override
+    public void testEquals() {
+        final GeneralEnvelope e1 = create(-40, -20, +60, +30);
+        final GeneralEnvelope e2 = create(-40, -20, +60, +30);
+        assertEquals(e1, e2);
+        e2.ordinates[0] = -1;
+        e2.ordinates[1] = -4;
+        e2.ordinates[9] = -9;
+        assertEquals(e1, e2);
+        e2.ordinates[2] = -41;
+        assertFalse(e1.equals(e2));
+    }
+
+    /**
+     * Tests the {@link SubEnvelope#clone()} method.
+     */
+    @Test
+    @Override
+    public void testClone() {
+        final GeneralEnvelope e1 = create(-40, -20, +60, +30);
+        final GeneralEnvelope e2 = e1.clone();
+        assertArrayEquals(new double[] {1, 4, -40, -20, 5, 2, 7, +60, +30, 9}, e1.ordinates,
STRICT);
+        assertArrayEquals(new double[] {      -40, -20,          +60, +30   }, e2.ordinates,
STRICT);
+        validate(e2);
+    }
+}

Propchange: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/geometry/SubEnvelopeTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java?rev=1467941&r1=1467940&r2=1467941&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] Mon Apr 15 11:34:17 2013
@@ -35,6 +35,7 @@ import org.junit.runners.Suite;
     org.apache.sis.geometry.DirectPosition2DTest.class,
     org.apache.sis.geometry.AbstractEnvelopeTest.class,
     org.apache.sis.geometry.GeneralEnvelopeTest.class,
+    org.apache.sis.geometry.SubEnvelopeTest.class,
     org.apache.sis.geometry.ImmutableEnvelopeTest.class,
     org.apache.sis.geometry.Envelope2DTest.class
 })



Mime
View raw message