sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Rename ArraysExt.sequence(…) as ArraysExt.range(…) and modify its semantic for consistency with IntStream.range(…).
Date Sun, 10 Mar 2019 22:08:09 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 87f6141432010c11ceded362d3b77cfb225ed26a
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sun Mar 10 14:13:24 2019 +0100

    Rename ArraysExt.sequence(…) as ArraysExt.range(…) and modify its semantic for consistency
with IntStream.range(…).
---
 .../apache/sis/internal/raster/RasterFactory.java  |  2 +-
 .../org/apache/sis/image/DefaultIteratorTest.java  |  2 +-
 .../java/org/apache/sis/image/TiledImageMock.java  |  2 +-
 .../operation/builder/LinearTransformBuilder.java  |  2 +-
 .../operation/builder/ProjectedTransformTry.java   |  4 +-
 .../sis/referencing/operation/matrix/Solver.java   |  2 +-
 .../operation/transform/PassThroughTransform.java  |  2 +-
 .../operation/transform/TransformSeparator.java    |  8 +--
 .../main/java/org/apache/sis/util/ArraysExt.java   | 58 ++++++++++------------
 .../java/org/apache/sis/util/ArraysExtTest.java    | 22 +++-----
 .../java/org/apache/sis/internal/netcdf/Axis.java  |  2 +-
 .../sis/internal/storage/AbstractGridResource.java |  2 +-
 12 files changed, 49 insertions(+), 59 deletions(-)

diff --git a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
index edfdd23..a276788 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/internal/raster/RasterFactory.java
@@ -120,7 +120,7 @@ public final class RasterFactory extends Static {
             }
         } else {
             if (bankIndices == null) {
-                bankIndices = ArraysExt.sequence(0, bandOffsets.length);
+                bankIndices = ArraysExt.range(0, bandOffsets.length);
             }
             if (pixelStride == 1) {
                 /*
diff --git a/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java b/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
index 8c4832f..4f8944f 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/image/DefaultIteratorTest.java
@@ -155,7 +155,7 @@ public strictfp class DefaultIteratorTest extends TestCase {
         }
         expected = new float[StrictMath.max(subMaxX - subMinX, 0) * StrictMath.max(subMaxY
- subMinY, 0) * numBands];
         final WritableRaster raster = Raster.createWritableRaster(new PixelInterleavedSampleModel(dataType,
-                width, height, numBands, width * numBands, ArraysExt.sequence(0, numBands)),
new Point(xmin, ymin));
+                width, height, numBands, width * numBands, ArraysExt.range(0, numBands)),
new Point(xmin, ymin));
         /*
          * At this point, all data structures have been created an initialized to zero sample
values.
          * Now fill the data structures with arbitrary values.
diff --git a/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java b/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
index 3697ada..93fe1a5 100644
--- a/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
+++ b/core/sis-raster/src/test/java/org/apache/sis/image/TiledImageMock.java
@@ -115,7 +115,7 @@ final class TiledImageMock implements WritableRenderedImage {
         this.numYTiles   = (height + tileHeight - 1) / tileHeight;
         this.tiles       = new WritableRaster[numXTiles * numYTiles];
         this.sampleModel = new PixelInterleavedSampleModel(dataType, tileWidth, tileHeight,
-                                numBands, tileWidth * numBands, ArraysExt.sequence(0, numBands));
+                                numBands, tileWidth * numBands, ArraysExt.range(0, numBands));
     }
 
     /*
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
index 6aa38a4..8f8586a 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
@@ -1143,7 +1143,7 @@ search:         for (int j=domain(); --j >= 0;) {
         }
         final int tgtDim = getTargetDimensions();
         if (dimensions == null || dimensions.length == 0) {
-            dimensions = ArraysExt.sequence(0, tgtDim);
+            dimensions = ArraysExt.range(0, tgtDim);
         }
         if (linearizers == null) {
             linearizers = new ArrayList<>();
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/ProjectedTransformTry.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/ProjectedTransformTry.java
index 5065b8f..1f740e5 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/ProjectedTransformTry.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/ProjectedTransformTry.java
@@ -230,7 +230,7 @@ final class ProjectedTransformTry implements Comparable<ProjectedTransformTry>
{
      * @return a copy of the given {@code correlation} array with new values overwriting
the old values.
      */
     final double[] replace(double[] correlations, final double[] newValues) {
-        if (newValues.length == correlations.length && ArraysExt.isSequence(0, dimensions))
{
+        if (newValues.length == correlations.length && ArraysExt.isRange(0, dimensions))
{
             return newValues;
         }
         correlations = correlations.clone();
@@ -255,7 +255,7 @@ final class ProjectedTransformTry implements Comparable<ProjectedTransformTry>
{
          * have a different number of rows since the number of target dimensions may differ.
          */
         assert newValues.getNumCol() == transform.getNumCol();
-        if (newValues.getNumRow() == transform.getNumRow() && ArraysExt.isSequence(0,
dimensions)) {
+        if (newValues.getNumRow() == transform.getNumRow() && ArraysExt.isRange(0,
dimensions)) {
             return newValues;
         }
         transform = transform.clone();
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
index 95a5400..3b19b9c 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Solver.java
@@ -323,7 +323,7 @@ searchNaN:  for (int flatIndex = (size - 1) * size; --flatIndex >=
0;) {
     {
         final int errorLU = size * size;
         assert errorLU == GeneralMatrix.indexOfErrors(size, size, LU);
-        final int[] pivot = ArraysExt.sequence(0, size);
+        final int[] pivot = ArraysExt.range(0, size);
         final double[]  column = new double[size * 2];  // [0 … size-1] : column values;
[size … 2*size-1] : error terms.
         final DoubleDouble acc = new DoubleDouble();    // Temporary variable for sum ("accumulator")
and subtraction.
         final DoubleDouble rat = new DoubleDouble();    // Temporary variable for products
and ratios.
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
index b989543..d5f812d 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
@@ -257,7 +257,7 @@ public class PassThroughTransform extends AbstractMathTransform implements
Seria
      * @see org.apache.sis.referencing.operation.DefaultPassThroughOperation#getModifiedCoordinates()
      */
     public final int[] getModifiedCoordinates() {
-        return ArraysExt.sequence(firstAffectedCoordinate, subTransform.getSourceDimensions());
+        return ArraysExt.range(firstAffectedCoordinate, firstAffectedCoordinate + subTransform.getSourceDimensions());
     }
 
     /**
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
index 7197f81..83b0caf 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransformSeparator.java
@@ -224,7 +224,7 @@ public class TransformSeparator {
                     isOutOfRange ? "upper" : "lower", min, max-1, isOutOfRange ? upper :
lower));
         }
         if (offset == 0) {
-            sequence = ArraysExt.sequence(lower, upper - lower);
+            sequence = ArraysExt.range(lower, upper);
         } else {
             sequence = Arrays.copyOf(sequence, (offset -= lower) + upper);
             for (int i=lower; i<upper; i++) {
@@ -413,10 +413,10 @@ public class TransformSeparator {
                 tr = filterTargetDimensions(tr, targetDimensions);
             }
             if (sourceDimensions == null) {
-                sourceDimensions = ArraysExt.sequence(0, transform.getSourceDimensions());
+                sourceDimensions = ArraysExt.range(0, transform.getSourceDimensions());
             }
             if (targetDimensions == null) {
-                targetDimensions = ArraysExt.sequence(0, transform.getTargetDimensions());
+                targetDimensions = ArraysExt.range(0, transform.getTargetDimensions());
             }
         } else {
             /*
@@ -503,7 +503,7 @@ public class TransformSeparator {
         final int lower  = dimensions[0];
         final int upper  = dimensions[dimensions.length - 1] + 1;
         if (lower == 0 && upper == numSrc && dimensions.length == numSrc)
{
-            targetDimensions = ArraysExt.sequence(0, numTgt);
+            targetDimensions = ArraysExt.range(0, numTgt);
             return step;
         }
         if (step.isIdentity()) {
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java b/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
index 26079ae..0bfb01f 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/ArraysExt.java
@@ -20,7 +20,6 @@ import java.util.Arrays;
 import java.util.Objects;
 import java.util.Comparator;
 import java.lang.reflect.Array;
-import org.apache.sis.util.resources.Errors;
 
 
 /**
@@ -1259,45 +1258,43 @@ public final class ArraysExt extends Static {
     }
 
     /**
-     * Returns a finite arithmetic progression of the given length and common difference
of 1.
-     * For example {@code sequence(-1, 4)} returns {@code {-1, 0, 1, 2}}.
+     * Returns the ordered values in the range from {@code start} inclusive to {@code end} exclusive.
+     * This method performs the same work than {@link java.util.stream.IntStream#range(int,
int)} but
+     * returning values in an array instead than in a stream. This method is okay for small
sequences;
+     * for large sequences the stream approach should be preferred.
      *
      * <div class="note"><b>Purpose:</b>
      * this method is convenient for enumerating dimensions in a coordinate reference system
or bands in an image.
      * Some methods in the Java library or in Apache SIS want dimensions or bands to be specified
by their indices.
-     * An example in the Java library is the {@code bankIndices} argument in
+     * An example from the Java library is the {@code bankIndices} argument in
      * <code>{@linkplain java.awt.image.Raster#createBandedRaster(int, int, int, int,
int[], int[], java.awt.Point)
      * Raster.createBandedRaster}(…, bankIndices, …)</code>.
-     * An example in Apache SIS is the {@code range} argument in
-     * <code>{@linkplain org.apache.sis.storage.GridCoverageResource#read GridCoverageResource.read}(…,
range)</code>.</div>
+     * An example from Apache SIS is the {@code range} argument in
+     * <code>{@linkplain org.apache.sis.storage.GridCoverageResource#read GridCoverageResource.read}(…,
range)</code>.
+     * This {@code range(start, end)} method can be used in the common case where all bands
are wanted in order.</div>
      *
-     * For any array returned by this method, <code>{@linkplain #isSequence(int, int[])
isSequence}(start, array)</code>
+     * For any array returned by this method, <code>{@link #isRange(int, int[]) isRange}(start,
array)</code>
      * is guaranteed to return {@code true}.
      *
-     * @param  start   first value in the array to return.
-     * @param  length  number of values to return.
-     * @return a sequence of increasing integers starting at {@code start} and having {@code
length} values.
-     * @throws ArithmeticException if {@code start + length} overflows 32 bits integer.
+     * @param  start  first value (inclusive) in the array to return.
+     * @param  end    upper bound (exclusive) of values in the array to return.
+     * @return a finite arithmetic progression of common difference of 1 with all values
in the specified range.
+     * @throws ArithmeticException if the sequence length is greater than {@link Integer#MAX_VALUE}.
      *
      * @see java.util.stream.IntStream#range(int, int)
+     * @see <a href="https://en.wikipedia.org/wiki/Arithmetic_progression">Arithmetic
progression on Wikipedia</a>
      *
      * @since 1.0
      */
-    public static int[] sequence(final int start, final int length) {
-        if (length > 0) {
-            if (start + (length - 1) >= start) {
-                final int[] array = new int[length];
-                for (int i=0; i<length; i++) {
-                    array[i] = start + i;
-                }
-                return array;
-            } else {
-                throw new ArithmeticException(Errors.format(Errors.Keys.IntegerOverflow_1,
Integer.SIZE));
+    public static int[] range(final int start, final int end) {
+        if (end > start) {
+            final int[] array = new int[Math.subtractExact(end, start)];
+            for (int i=0; i<array.length; i++) {
+                array[i] = start + i;
             }
-        } else if (length == 0) {
-            return EMPTY_INT;
+            return array;
         } else {
-            throw new NegativeArraySizeException(Errors.format(Errors.Keys.NegativeArgument_2,
"length", length));
+            return EMPTY_INT;
         }
     }
 
@@ -1308,15 +1305,14 @@ public final class ArraysExt extends Static {
      *
      * <ul>
      *   <li>If {@code array} is {@code null}, then return {@code false}.</li>
-     *   <li>Otherwise if {@code array} is empty, then return {@code true} for consistency
-     *       with <code>{@linkplain #sequence(int, int) sequence}(start, 0)</code>.</li>
+     *   <li>Otherwise if {@code array} is empty, then return {@code true} for consistency
with {@link #range}.</li>
      *   <li>Otherwise for any index 0 ≦ <var>i</var> {@literal <}
{@code array.length}, if {@code array[i]}
      *       is equal to {@code start + i} (computed as if no overflow occurs), then return
{@code true}.</li>
      *   <li>Otherwise return {@code false}.</li>
      * </ul>
      *
      * <div class="note"><b>Example:</b>
-     * {@code isSequence(1, array)} returns {@code true} if the given array is {@code {1,
2, 3, 4}}
+     * {@code isRange(1, array)} returns {@code true} if the given array is {@code {1, 2,
3, 4}}
      * but {@code false} if the array is {@code {1, 2, 4}} (missing 3).</div>
      *
      * This method is useful when {@code array} is an argument specified to another method,
and determining that the
@@ -1325,18 +1321,18 @@ public final class ArraysExt extends Static {
      * @param  start  first value expected in the given {@code array}.
      * @param  array  the array to test, or {@code null}.
      * @return {@code true} if the given array is non-null and equal to
-     *         <code>{@linkplain #sequence(int, int) sequence}(start, array.length)</code>.
+     *         <code>{@linkplain #range(int, int) range}(start, start + array.length)</code>.
      *
-     * @see #sequence(int, int)
+     * @see #range(int, int)
      *
      * @since 1.0
      */
-    public static boolean isSequence(final int start, final int[] array) {
+    public static boolean isRange(final int start, final int[] array) {
         if (array == null) {
             return false;
         }
         if (array.length != 0) {
-            if (start + (array.length - 1) < start) {
+            if (start + (array.length - 1) < 0) {
                 return false;                               // Overflow.
             }
             for (int i=0; i<array.length; i++) {
diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
index df015a2..65366c4 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/util/ArraysExtTest.java
@@ -58,22 +58,16 @@ public final strictfp class ArraysExtTest extends TestCase {
     }
 
     /**
-     * Tests {@link ArraysExt#sequence(int, int)} and {@link ArraysExt#isSequence(int, int[])}.
+     * Tests {@link ArraysExt#range(int, int)} and {@link ArraysExt#isRange(int, int[])}.
      */
     @Test
-    public void testSequence() {
-        int[] sequence = ArraysExt.sequence(-1, 4);
-        assertArrayEquals("sequence", new int[] {-1, 0, 1, 2}, sequence);
-        assertTrue ("isSequence", ArraysExt.isSequence(-1, sequence));
-        assertFalse("isSequence", ArraysExt.isSequence(-2, sequence));
-        assertTrue ("isSequence", ArraysExt.isSequence(1, new int[] {1, 2, 3, 4}));
-        assertFalse("isSequence", ArraysExt.isSequence(1, new int[] {1, 2,    4}));
-        try {
-            ArraysExt.sequence(Integer.MAX_VALUE - 10, 12);
-            fail("Expected ArithmeticException.");
-        } catch (ArithmeticException e) {
-            assertNotNull(e.getMessage());
-        }
+    public void testRange() {
+        int[] sequence = ArraysExt.range(-1, 3);
+        assertArrayEquals("range", new int[] {-1, 0, 1, 2}, sequence);
+        assertTrue ("isRange", ArraysExt.isRange(-1, sequence));
+        assertFalse("isRange", ArraysExt.isRange(-2, sequence));
+        assertTrue ("isRange", ArraysExt.isRange(1, new int[] {1, 2, 3, 4}));
+        assertFalse("isRange", ArraysExt.isRange(1, new int[] {1, 2,    4}));
     }
 
     /**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
index 9f24ba7..ca282a3 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
@@ -304,7 +304,7 @@ public final class Axis extends NamedElement {
         final int up;
         if (length >= 0) {
             if (length <= 1) return ArraysExt.EMPTY_INT;
-            if (length <= 4) return ArraysExt.sequence(0, length - 1);
+            if (length <= 4) return ArraysExt.range(0, length - 1);
             up = length - 2;
         } else {
             up = Integer.MAX_VALUE - 1;                 // For unsigned integers, < 0
means overflow.
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
index 5813803..9d5cd02 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/AbstractGridResource.java
@@ -102,7 +102,7 @@ public abstract class AbstractGridResource extends AbstractResource implements
G
     protected final int[] validateRangeArgument(final int numSampleDimensions, int[] range)
{
         ArgumentChecks.ensureStrictlyPositive("numSampleDimensions", numSampleDimensions);
         if (range == null || range.length == 0) {
-            return ArraysExt.sequence(0, numSampleDimensions);
+            return ArraysExt.range(0, numSampleDimensions);
         }
         range = range.clone();
         for (int i=0; i<range.length; i++) {


Mime
View raw message