sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1755584 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/math/ test/java/org/apache/sis/math/
Date Tue, 09 Aug 2016 09:10:27 GMT
Author: desruisseaux
Date: Tue Aug  9 09:10:27 2016
New Revision: 1755584

URL: http://svn.apache.org/viewvc?rev=1755584&view=rev
Log:
Reorganize Vector API:
- rename toString(int) as stringValue(int) for consistency with other xxxValue(int) methods.
- rename view(int...) as pick(int...).
- add a toString() method for correct representation of unsigned numbers.
- provide default implementation of most xxxValue(int) methods.
- change some method declaration order.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java?rev=1755584&r1=1755583&r2=1755584&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
[UTF-8] Tue Aug  9 09:10:27 2016
@@ -61,81 +61,13 @@ abstract class ArrayVector<E extends Num
     }
 
     /**
-     * The default implementation delegates to {@link #doubleValue(int)} and verifies if
the result
-     * can be rounded to a {@code long}. Subclasses that are wrapper of integer types should
override.
-     */
-    @Override
-    public long longValue(final int index) {
-        final double value = doubleValue(index);
-        final long result = Math.round(value);
-        if (Math.abs(result - value) <= 0.5) {
-            return result;
-        }
-        throw canNotConvert(long.class);
-    }
-
-    /**
-     * The default implementation delegates to {@link #longValue(int)} and verifies if the
result
-     * can be casted to a {@code int}. Subclasses that are wrapper of type not wider should
override.
-     */
-    @Override
-    public int intValue(final int index) {
-        final long value = longValue(index);
-        if (value >= java.lang.Integer.MIN_VALUE &&
-            value <= java.lang.Integer.MAX_VALUE)
-        {
-            return (int) value;
-        }
-        throw canNotConvert(int.class);
-    }
-
-    /**
-     * The default implementation delegates to {@link #longValue(int)} and verifies if the
result
-     * can be casted to a {@code short}. Subclasses that are wrapper of type not wider should
override.
-     */
-    @Override
-    public short shortValue(final int index) {
-        final long value = longValue(index);
-        if (value >= java.lang.Short.MIN_VALUE &&
-            value <= java.lang.Short.MAX_VALUE)
-        {
-            return (short) value;
-        }
-        throw canNotConvert(short.class);
-    }
-
-    /**
-     * The default implementation delegates to {@link #longValue(int)} and verifies if the
result
-     * can be casted to a {@code byte}. Subclasses that are wrapper of type not wider should
override.
-     */
-    @Override
-    public byte byteValue(final int index) {
-        final long value = longValue(index);
-        if (value >= java.lang.Byte.MIN_VALUE &&
-            value <= java.lang.Byte.MAX_VALUE)
-        {
-            return (byte) value;
-        }
-        throw canNotConvert(byte.class);
-    }
-
-    /**
-     * Returns the exception to be thrown when the component type in the backing array can
-     * not be converted to the requested type through an identity or widening conversion.
-     */
-    private ArithmeticException canNotConvert(final Class<?> target) {
-        return new ArithmeticException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
-                Numbers.wrapperToPrimitive(getElementType()), target));
-    }
-
-    /**
      * Verifies that a value of the given type can be casted to the expected type.
      * The expected type must be one of the {@link Numbers} constants.
      */
     final void verifyType(final Class<? extends Number> type, final byte expected)
{
         final byte t = Numbers.getEnumConstant(type);
         if (t < Numbers.BYTE || t > expected) {
-            throw new ArrayStoreException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
+            throw new ClassCastException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
                 type, Numbers.wrapperToPrimitive(getElementType())));
         }
     }
@@ -171,7 +103,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Double.toString(array[index]);
         }
 
@@ -233,7 +165,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Float.toString(array[index]);
         }
 
@@ -271,7 +203,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Long.toString(array[index]);
         }
 
@@ -309,7 +241,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Integer.toString(array[index]);
         }
 
@@ -348,7 +280,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Short.toString(array[index]);
         }
 
@@ -388,7 +320,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Byte.toString(array[index]);
         }
 
@@ -441,7 +373,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Long.toUnsignedString(super.longValue(index));
         }
     }
@@ -470,7 +402,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Integer.toUnsignedString(super.intValue(index));
         }
     }
@@ -498,7 +430,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Integer.toString(intValue(index));
         }
     }
@@ -527,7 +459,7 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String toString(final int index) {
+        @Override public String stringValue(final int index) {
             return java.lang.Integer.toString(intValue(index));
         }
     }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java?rev=1755584&r1=1755583&r2=1755584&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/ConcatenatedVector.java
[UTF-8] Tue Aug  9 09:10:27 2016
@@ -189,7 +189,7 @@ final class ConcatenatedVector extends V
      * Returns the string representation at the given index.
      */
     @Override
-    public String toString(int index) {
+    public String stringValue(int index) {
         final Vector v;
         if (index < limit) {
             v = first;
@@ -197,7 +197,7 @@ final class ConcatenatedVector extends V
             v = second;
             index -= limit;
         }
-        return v.toString(index);
+        return v.stringValue(index);
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java?rev=1755584&r1=1755583&r2=1755584&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java
[UTF-8] Tue Aug  9 09:10:27 2016
@@ -133,42 +133,10 @@ final class SequenceVector extends Vecto
     }
 
     /**
-     * Computes the value at the given index.
-     */
-    @Override
-    public long longValue(final int index) throws IndexOutOfBoundsException {
-        return Math.round(doubleValue(index));
-    }
-
-    /**
-     * Computes the value at the given index.
-     */
-    @Override
-    public int intValue(final int index) throws IndexOutOfBoundsException {
-        return (int) longValue(index);
-    }
-
-    /**
-     * Computes the value at the given index.
-     */
-    @Override
-    public short shortValue(final int index) throws IndexOutOfBoundsException {
-        return (short) longValue(index);
-    }
-
-    /**
-     * Computes the value at the given index.
-     */
-    @Override
-    public byte byteValue(final int index) throws IndexOutOfBoundsException {
-        return (byte) longValue(index);
-    }
-
-    /**
      * Returns the string representation of the value at the given index.
      */
     @Override
-    public String toString(final int index) throws IndexOutOfBoundsException {
+    public String stringValue(final int index) throws IndexOutOfBoundsException {
         return String.valueOf(doubleValue(index));
     }
 

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java?rev=1755584&r1=1755583&r2=1755584&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java [UTF-8]
Tue Aug  9 09:10:27 2016
@@ -45,7 +45,7 @@ import static org.apache.sis.util.Argume
  * {@preformat java
  *     float[] array = new float[100];
  *     Vector v = Vector.create(array, false).subList(20, 40)
- *     // At this point, v.doubleValue(0) is equivalent to array[20].
+ *     // At this point, v.doubleValue(0) is equivalent to (double) array[20].
  * }
  *
  * <div class="section">Usage</div>
@@ -184,6 +184,8 @@ public abstract class Vector extends Abs
      * verify this information for avoiding {@link ArithmeticException}.</p>
      *
      * @return the type of elements in this vector.
+     *
+     * @see org.apache.sis.util.collection.CheckedContainer#getElementType()
      */
     public abstract Class<? extends Number> getElementType();
 
@@ -229,7 +231,8 @@ public abstract class Vector extends Abs
 
     /**
      * Returns the value at the given index as a {@code float}.
-     * This method may result in a lost of precision if the underlying storage is a {@code
double[]} array.
+     * This method may result in a lost of precision if this vector
+     * stores or computes its values with the {@code double} type.
      *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
@@ -241,45 +244,94 @@ public abstract class Vector extends Abs
      * Returns the value at the given index as a {@code long}.
      * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
+     * <p>The default implementation delegates to {@link #doubleValue(int)} and verifies
+     * if the result can be rounded to a {@code long} with an error not greater than 0.5.
+     * Subclasses that store or compute their values with an integer type should override
this method.</p>
+     *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
long} type.
      */
-    public abstract long longValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
+    public long longValue(final int index) {
+        final double value = doubleValue(index);
+        final long result = Math.round(value);
+        if (Math.abs(result - value) <= 0.5) {
+            return result;
+        }
+        throw canNotConvert(index, long.class);
+    }
 
     /**
      * Returns the value at the given index as an {@code int}.
      * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
+     * <p>The default implementation delegates to {@link #longValue(int)} and verifies
if the result
+     * fits in the {@code int} type. Subclasses that store or compute their values with the
{@code int},
+     * {@code short} or {@code byte} type should override this method.</p>
+     *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
int} type.
      */
-    public abstract int intValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
+    public int intValue(final int index) {
+        final long value = longValue(index);
+        if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
+            return (int) value;
+        }
+        throw canNotConvert(index, int.class);
+    }
 
     /**
      * Returns the value at the given index as a {@code short}.
      * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
+     * <p>The default implementation delegates to {@link #longValue(int)} and verifies
if the result
+     * fits in the {@code short} type. Subclasses that store or compute their values with
the {@code short}
+     * or {@code byte} type should override this method.</p>
+     *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
short} type.
      */
-    public abstract short shortValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
+    public short shortValue(final int index) {
+        final long value = longValue(index);
+        if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
+            return (short) value;
+        }
+        throw canNotConvert(index, short.class);
+    }
 
     /**
      * Returns the value at the given index as a {@code byte}.
      * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
+     * <p>The default implementation delegates to {@link #longValue(int)} and verifies
if the result
+     * fits in the {@code byte} type. Subclasses that store or compute their values with
the {@code byte}
+     * type should override this method.</p>
+     *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
byte} type.
      */
-    public abstract byte byteValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
+    public byte byteValue(final int index) {
+        final long value = longValue(index);
+        if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
+            return (byte) value;
+        }
+        throw canNotConvert(index, byte.class);
+    }
+
+    /**
+     * Returns the exception to be thrown when the component type in the backing array can
+     * not be converted to the requested type through an identity or widening conversion.
+     */
+    private ArithmeticException canNotConvert(final int index, final Class<?> target)
{
+        return new ArithmeticException(Errors.format(Errors.Keys.CanNotConvertValue_2, stringValue(index),
target));
+    }
 
     /**
      * Returns a string representation of the value at the given index.
@@ -291,10 +343,12 @@ public abstract class Vector extends Abs
      * @return a string representation of the value at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      */
-    public abstract String toString(int index) throws IndexOutOfBoundsException;
+    public abstract String stringValue(int index) throws IndexOutOfBoundsException;
 
     /**
      * Returns the number at the given index, or {@code null} if none.
+     * If non-null, the object returned by this method will be an instance
+     * of the class returned by {@link #getElementType()} or a sub-class.
      *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return the value at the given index.
@@ -305,18 +359,158 @@ public abstract class Vector extends Abs
 
     /**
      * Sets the number at the given index.
+     * The given number should be an instance of the same type than the number returned by
{@link #get(int)}.
      *
      * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @param  value  the value to set at the given index.
      * @return the value previously stored at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
-     * @throws ArrayStoreException if the given value can not be stored in this vector.
+     * @throws ClassCastException if the given value can not be stored in this vector.
+     */
+    @Override
+    public abstract Number set(int index, Number value) throws IndexOutOfBoundsException,
ClassCastException;
+
+    /**
+     * Returns a view which contain the values of this vector in the given index range.
+     * The returned view will contain the values from index {@code lower} inclusive to
+     * {@code upper} exclusive.
+     *
+     * <div class="note"><b>Implementation note:</b> this method delegates
its work
+     * <code>{@linkplain #subSampling(int,int,int) subSampling}(lower, 1, upper - lower)</code>.
+     * This method is declared final in order to force subclasses to override {@code subSampling(…)}
instead.</div>
+     *
+     * @param  lower  index of the first value to be included in the returned view.
+     * @param  upper  index after the last value to be included in the returned view.
+     * @return a view of this vector containing values in the given index range.
+     * @throws IndexOutOfBoundsException If an index is outside the [0 … {@linkplain #size()
size}-1] range.
      */
     @Override
-    public abstract Number set(int index, Number value) throws IndexOutOfBoundsException,
ArrayStoreException;
+    public final Vector subList(final int lower, final int upper) throws IndexOutOfBoundsException
{
+        return subSampling(lower, 1, upper - lower);
+    }
 
     /**
-     * If this vector is a view over an other vector, returns the backing vector.
+     * Returns a view which contain the values of this vector in a given index range.
+     * The returned view will contain the values from index {@code first} inclusive to
+     * {@code (first + step*length)} exclusive with index incremented by the given {@code
step} value,
+     * which can be negative. More specifically the index <var>i</var> in the
returned vector will maps
+     * the element at index <code>(first + step*<var>i</var>)</code>
in this vector.
+     *
+     * <p>This method does not copy the values. Consequently any modification to the
+     * values of this vector will be reflected in the returned view and vis-versa.</p>
+     *
+     * @param  first   index of the first value to be included in the returned view.
+     * @param  step    the index increment in this vector between two consecutive values
+     *                 in the returned vector. Can be positive, zero or negative.
+     * @param  length  the length of the vector to be returned. Can not be greater than
+     *                 the length of this vector, except if the {@code step} is zero.
+     * @return a view of this vector containing values in the given index range.
+     * @throws IndexOutOfBoundsException if {@code first} or {@code first + step*(length-1)}
+     *         is outside the [0 … {@linkplain #size() size}-1] range.
+     */
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
+    public Vector subSampling(final int first, final int step, final int length) throws IndexOutOfBoundsException
{
+        if (step == 1 && first == 0 && length == size()) {
+            return this;
+        }
+        return createSubSampling(first, step, length);
+    }
+
+    /**
+     * Implementation of {@link #subSampling(int,int,int)} to be overridden by subclasses.
+     */
+    Vector createSubSampling(final int first, final int step, final int length) {
+        return new SubSampling(first, step, length);
+    }
+
+    /**
+     * A view over an other vector in a range of index.
+     */
+    private final class SubSampling extends Vector implements Serializable {
+        /** For cross-version compatibility. */
+        private static final long serialVersionUID = 7641036842053528486L;
+
+        /** Index of the first element in the enclosing vector. */
+        private final int first;
+
+        /** The index increment. May be negative but not zero. */
+        private final int step;
+
+        /** The length of this vector. */
+        private final int length;
+
+        /** Creates a new view over the given range. */
+        protected SubSampling(final int first, final int step, final int length) {
+            ensureValid(first, step, length);
+            this.first  = first;
+            this.step   = step;
+            this.length = length;
+        }
+
+        /** Returns the backing vector. */
+        @Override Vector backingVector() {
+            return Vector.this;
+        }
+
+        /** Returns the index where to look for the value in the enclosing vector. */
+        private int toBacking(final int index) throws IndexOutOfBoundsException {
+            ensureValidIndex(length, index);
+            return index*step + first;
+        }
+
+        /** Returns the index where to look for the value in the enclosing vector. */
+        @Override int[] toBacking(final int[] index) throws IndexOutOfBoundsException {
+            final int[] ni = new int[index.length];
+            for (int j=0; j<ni.length; j++) {
+                ni[j] = toBacking(index[j]);
+            }
+            return ni;
+        }
+
+        /** Returns the type of elements in this vector. */
+        @Override public Class<? extends Number> getElementType() {
+            return Vector.this.getElementType();
+        }
+
+        /** Returns the length of this subvector. */
+        @Override public int size() {
+            return length;
+        }
+
+        /** Delegates to the enclosing vector. */
+        @Override public boolean isUnsigned()               {return Vector.this.isUnsigned();}
+        @Override public boolean isNaN      (int index)     {return Vector.this.isNaN   
  (toBacking(index));}
+        @Override public double  doubleValue(int index)     {return Vector.this.doubleValue(toBacking(index));}
+        @Override public float   floatValue (int index)     {return Vector.this.floatValue
(toBacking(index));}
+        @Override public long    longValue  (int index)     {return Vector.this.longValue
 (toBacking(index));}
+        @Override public int     intValue   (int index)     {return Vector.this.intValue
  (toBacking(index));}
+        @Override public short   shortValue (int index)     {return Vector.this.shortValue
(toBacking(index));}
+        @Override public byte    byteValue  (int index)     {return Vector.this.byteValue
 (toBacking(index));}
+        @Override public String  stringValue(int index)     {return Vector.this.stringValue(toBacking(index));}
+        @Override public Number  get        (int index)     {return Vector.this.get     
  (toBacking(index));}
+        @Override public Number  set(int index, Number v)   {return Vector.this.set     
  (toBacking(index), v);}
+
+        /** Delegates to the enclosing vector. */
+        @Override Vector createSubSampling(int first, int step, final int length) {
+            first = toBacking(first);
+            step *= this.step;
+            return Vector.this.subSampling(first, step, length);
+        }
+
+        /** Delegates to the enclosing vector if possible. */
+        @Override Vector createConcatenate(final Vector toAppend) {
+            if (toAppend instanceof SubSampling && toAppend.backingVector() == Vector.this)
{
+                final SubSampling other = (SubSampling) toAppend;
+                if (other.step == step && other.first == first + step*length) {
+                    return Vector.this.subSampling(first, step, length + other.length);
+                }
+            }
+            return super.createConcatenate(toAppend);
+        }
+    }
+
+    /**
+     * If this vector is a view over another vector, returns the backing vector.
      * Otherwise returns {@code this}. If this method is overridden, it should be
      * together with the {@link #toBacking(int[])} method.
      */
@@ -357,7 +551,7 @@ public abstract class Vector extends Abs
      * @return a view of this vector containing values at the given indexes.
      * @throws IndexOutOfBoundsException if at least one index is out of bounds.
      */
-    public Vector view(int... indices) throws IndexOutOfBoundsException {
+    public Vector pick(int... indices) throws IndexOutOfBoundsException {
         indices = toBacking(indices);
         final int first, step;
         switch (indices.length) {
@@ -379,7 +573,7 @@ public abstract class Vector extends Abs
                 for (int i=2; i<indices.length; i++) {
                     final int current = indices[i];
                     if (current - limit != step) {
-                        return backingVector().new View(indices);
+                        return backingVector().new Pick(indices);
                     }
                     limit = current;
                 }
@@ -390,104 +584,9 @@ public abstract class Vector extends Abs
     }
 
     /**
-     * Returns a view which contains the values of this vector in reverse order.
-     *
-     * <div class="note"><b>Implementation note:</b> this method delegates
its work
-     * to <code>{@linkplain #subSampling(int,int,int) subSampling}(size-1, -1, {@linkplain
#size() size})</code>.
-     * It is declared final in order to force every subclasses to override the later method
instead than this one.
-     * </div>
-     *
-     * @return the vector values in reverse order.
-     */
-    public final Vector reverse() {
-        final int length = size();
-        return (length != 0) ? subSampling(length-1, -1, length) : this;
-    }
-
-    /**
-     * Returns a view which contain the values of this vector in the given index range.
-     * The returned view will contain the values from index {@code lower} inclusive to
-     * {@code upper} exclusive.
-     *
-     * <div class="note"><b>Implementation note:</b> this method delegates
its work
-     * <code>{@linkplain #subSampling(int,int,int) subSampling}(lower, 1, upper - lower)</code>.
-     * It is declared final in order to force every subclasses to override the later method
instead than this one.
-     * </div>
-     *
-     * @param  lower  index of the first value to be included in the returned view.
-     * @param  upper  index after the last value to be included in the returned view.
-     * @return a view of this vector containing values in the given index range.
-     * @throws IndexOutOfBoundsException If an index is outside the [0 … {@linkplain #size()
size}-1] range.
-     */
-    @Override
-    public final Vector subList(final int lower, final int upper) throws IndexOutOfBoundsException
{
-        return subSampling(lower, 1, upper - lower);
-    }
-
-    /**
-     * Returns a view which contain the values of this vector in a given index range.
-     * The returned view will contain the values from index {@code first} inclusive to
-     * {@code (first + step*length)} exclusive with index incremented by the given {@code
step} value,
-     * which can be negative. More specifically the index <var>i</var> in the
returned vector will maps
-     * the element at index <code>(first + step*<var>i</var>)</code>
in this vector.
-     *
-     * <p>This method does not copy the values. Consequently any modification to the
-     * values of this vector will be reflected in the returned view and vis-versa.</p>
-     *
-     * @param  first   index of the first value to be included in the returned view.
-     * @param  step    the index increment in this vector between two consecutive values
-     *                 in the returned vector. Can be positive, zero or negative.
-     * @param  length  the length of the vector to be returned. Can not be greater than
-     *                 the length of this vector, except if the {@code step} is zero.
-     * @return a view of this vector containing values in the given index range.
-     * @throws IndexOutOfBoundsException if {@code first} or {@code first + step*(length-1)}
-     *         is outside the [0 … {@linkplain #size() size}-1] range.
-     */
-    @SuppressWarnings("ReturnOfCollectionOrArrayField")
-    public Vector subSampling(final int first, final int step, final int length) throws IndexOutOfBoundsException
{
-        if (step == 1 && first == 0 && length == size()) {
-            return this;
-        }
-        return createSubSampling(first, step, length);
-    }
-
-    /**
-     * Implementation of {@link #subSampling(int,int,int)} to be overridden by subclasses.
-     */
-    Vector createSubSampling(final int first, final int step, final int length) {
-        return new SubSampling(first, step, length);
-    }
-
-    /**
-     * Returns the concatenation of this vector with the given one. Indexes in the [0 …
{@linkplain #size() size}-1]
-     * range will map to this vector, while indexes in the [size … size + toAppend.size]
range while map to the
-     * given vector.
-     *
-     * @param  toAppend  the vector to concatenate at the end of this vector.
-     * @return the concatenation of this vector with the given vector.
-     */
-    @SuppressWarnings("ReturnOfCollectionOrArrayField")
-    public Vector concatenate(final Vector toAppend) {
-        if (toAppend.isEmpty()) {
-            return this;
-        }
-        if (isEmpty()) {
-            return toAppend;
-        }
-        return createConcatenate(toAppend);
-    }
-
-    /**
-     * Implementation of {@link #concatenate(Vector)} to be overridden by subclasses.
-     */
-    Vector createConcatenate(final Vector toAppend) {
-        return new ConcatenatedVector(this, toAppend);
-    }
-
-    /**
      * A view over an other vector at pre-selected indexes.
      */
-    private final class View extends Vector implements Serializable {
+    private final class Pick extends Vector implements Serializable {
         /** For cross-version compatibility. */
         private static final long serialVersionUID = 6574040261355090760L;
 
@@ -498,7 +597,7 @@ public abstract class Vector extends Abs
          * Creates a new view over the values at the given indexes. This constructor
          * does not clone the array; it is caller responsibility to clone it if needed.
          */
-        View(int[] indices) {
+        Pick(int[] indices) {
             this.indices = indices;
         }
 
@@ -531,7 +630,7 @@ public abstract class Vector extends Abs
         @Override public int      intValue   (int i)    {return Vector.this.intValue   (indices[i]);}
         @Override public short    shortValue (int i)    {return Vector.this.shortValue (indices[i]);}
         @Override public byte     byteValue  (int i)    {return Vector.this.byteValue  (indices[i]);}
-        @Override public String   toString   (int i)    {return Vector.this.toString   (indices[i]);}
+        @Override public String   stringValue(int i)    {return Vector.this.stringValue(indices[i]);}
         @Override public Number   get        (int i)    {return Vector.this.get        (indices[i]);}
         @Override public Number   set(int i, Number v)  {return Vector.this.set        (indices[i],
v);}
 
@@ -545,16 +644,16 @@ public abstract class Vector extends Abs
                 ni[j] = indices[first];
                 first += step;
             }
-            return Vector.this.view(ni);
+            return Vector.this.pick(ni);
         }
 
         /** Concatenates the indexes if possible. */
         @Override Vector createConcatenate(final Vector toAppend) {
-            if (toAppend instanceof View && toAppend.backingVector() == Vector.this)
{
-                final int[] other = ((View) toAppend).indices;
+            if (toAppend instanceof Pick && toAppend.backingVector() == Vector.this)
{
+                final int[] other = ((Pick) toAppend).indices;
                 final int[] c = Arrays.copyOf(indices, indices.length + other.length);
                 System.arraycopy(other, 0, c, indices.length, other.length);
-                return Vector.this.view(c);
+                return Vector.this.pick(c);
             }
             return super.createConcatenate(toAppend);
         }
@@ -581,88 +680,62 @@ public abstract class Vector extends Abs
     }
 
     /**
-     * A view over an other vector in a range of index.
+     * Returns the concatenation of this vector with the given one. Indexes in the [0 …
{@link #size() size} - 1]
+     * range will map to this vector, while indexes in the [{@code size} … {@code size}
+ {@code toAppend.size}]
+     * range while map to the given vector.
+     *
+     * @param  toAppend  the vector to concatenate at the end of this vector.
+     * @return the concatenation of this vector with the given vector.
      */
-    private final class SubSampling extends Vector implements Serializable {
-        /** For cross-version compatibility. */
-        private static final long serialVersionUID = 7641036842053528486L;
-
-        /** Index of the first element in the enclosing vector. */
-        private final int first;
-
-        /** The index increment. May be negative but not zero. */
-        private final int step;
-
-        /** The length of this vector. */
-        private final int length;
-
-        /** Creates a new view over the given range. */
-        protected SubSampling(final int first, final int step, final int length) {
-            ensureValid(first, step, length);
-            this.first  = first;
-            this.step   = step;
-            this.length = length;
-        }
-
-        /** Returns the backing vector. */
-        @Override Vector backingVector() {
-            return Vector.this;
-        }
-
-        /** Returns the index where to look for the value in the enclosing vector. */
-        private int toBacking(final int index) throws IndexOutOfBoundsException {
-            ensureValidIndex(length, index);
-            return index*step + first;
-        }
-
-        /** Returns the index where to look for the value in the enclosing vector. */
-        @Override int[] toBacking(final int[] index) throws IndexOutOfBoundsException {
-            final int[] ni = new int[index.length];
-            for (int j=0; j<ni.length; j++) {
-                ni[j] = toBacking(index[j]);
-            }
-            return ni;
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
+    public Vector concatenate(final Vector toAppend) {
+        if (toAppend.isEmpty()) {
+            return this;
         }
-
-        /** Returns the type of elements in this vector. */
-        @Override public Class<? extends Number> getElementType() {
-            return Vector.this.getElementType();
+        if (isEmpty()) {
+            return toAppend;
         }
+        return createConcatenate(toAppend);
+    }
 
-        /** Returns the length of this subvector. */
-        @Override public int size() {
-            return length;
-        }
+    /**
+     * Implementation of {@link #concatenate(Vector)} to be overridden by subclasses.
+     */
+    Vector createConcatenate(final Vector toAppend) {
+        return new ConcatenatedVector(this, toAppend);
+    }
 
-        /** Delegates to the enclosing vector. */
-        @Override public boolean isUnsigned()               {return Vector.this.isUnsigned();}
-        @Override public boolean isNaN      (int index)     {return Vector.this.isNaN   
  (toBacking(index));}
-        @Override public double  doubleValue(int index)     {return Vector.this.doubleValue(toBacking(index));}
-        @Override public float   floatValue (int index)     {return Vector.this.floatValue
(toBacking(index));}
-        @Override public long    longValue  (int index)     {return Vector.this.longValue
 (toBacking(index));}
-        @Override public int     intValue   (int index)     {return Vector.this.intValue
  (toBacking(index));}
-        @Override public short   shortValue (int index)     {return Vector.this.shortValue
(toBacking(index));}
-        @Override public byte    byteValue  (int index)     {return Vector.this.byteValue
 (toBacking(index));}
-        @Override public String  toString   (int index)     {return Vector.this.toString
  (toBacking(index));}
-        @Override public Number  get        (int index)     {return Vector.this.get     
  (toBacking(index));}
-        @Override public Number  set(int index, Number v)   {return Vector.this.set     
  (toBacking(index), v);}
+    /**
+     * Returns a view which contains the values of this vector in reverse order.
+     *
+     * <div class="note"><b>Implementation note:</b> this method delegates
its work
+     * to <code>{@linkplain #subSampling(int,int,int) subSampling}(size-1, -1, {@linkplain
#size() size})</code>.
+     * This method is declared final in order to force subclasses to override {@code subSampling(…)}
instead.</div>
+     *
+     * @return the vector values in reverse order.
+     */
+    public final Vector reverse() {
+        final int length = size();
+        return (length > 1) ? subSampling(length-1, -1, length) : this;
+    }
 
-        /** Delegates to the enclosing vector. */
-        @Override Vector createSubSampling(int first, int step, final int length) {
-            first = toBacking(first);
-            step *= this.step;
-            return Vector.this.subSampling(first, step, length);
+    /**
+     * Returns a string representation of this vector.
+     *
+     * @return a string representation of this vector.
+     */
+    @Override
+    public String toString() {
+        final int length = size();
+        if (length == 0) {
+            return "[]";
         }
-
-        /** Delegates to the enclosing vector if possible. */
-        @Override Vector createConcatenate(final Vector toAppend) {
-            if (toAppend instanceof SubSampling && toAppend.backingVector() == Vector.this)
{
-                final SubSampling other = (SubSampling) toAppend;
-                if (other.step == step && other.first == first + step*length) {
-                    return Vector.this.createSubSampling(first, step, length + other.length);
-                }
-            }
-            return super.createConcatenate(toAppend);
+        final StringBuilder buffer = new StringBuilder();
+        String separator = "[";
+        for (int i=0; i<length; i++) {
+            buffer.append(separator).append(stringValue(i));
+            separator = ", ";
         }
+        return buffer.append(']').toString();
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java?rev=1755584&r1=1755583&r2=1755584&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java [UTF-8]
Tue Aug  9 09:10:27 2016
@@ -99,7 +99,7 @@ public final strictfp class VectorTest e
         } catch (ArithmeticException e) {
             // This is the expected exception.
             final String message = e.getMessage();
-            assertTrue(message, message.contains("short"));
+            assertTrue(message, message.contains("1000"));
             assertTrue(message, message.contains("byte"));
         }
 
@@ -117,7 +117,7 @@ public final strictfp class VectorTest e
         }
 
         // Tests subvector at specific indexes.
-        vector = vector.view(10, 20, 25);
+        vector = vector.pick(10, 20, 25);
         assertEquals(3, vector.size());
         assertEquals(array[120], vector.shortValue(0));
         assertEquals(array[140], vector.shortValue(1));
@@ -210,11 +210,25 @@ public final strictfp class VectorTest e
          * Tests concatenation of views at fixed indices. Should be
          * implemented as the concatenation of the indices arrays when possible.
          */
-        final Vector expected = v3.view(10, 25, 30, 0, 35, 39);
-        v2 = v1.view( 0, 35, 39);
-        v1 = v1.view(10, 25, 30);
+        final Vector expected = v3.pick(10, 25, 30, 0, 35, 39);
+        v2 = v1.pick( 0, 35, 39);
+        v1 = v1.pick(10, 25, 30);
         v3 = v1.concatenate(v2);
         assertEquals(expected, v3);
         assertFalse("Expected concatenation of the indices.", v3 instanceof ConcatenatedVector);
     }
+
+    /**
+     * Tests the {@link Vector#toString()} method on a vector of signed and unsigned bytes.
+     */
+    @Test
+    public void testToString() {
+        final byte[] array = new byte[] {(byte) 10, (byte) 100, (byte) 200};
+
+        vector = Vector.create(array, true);
+        assertEquals("[10, 100, 200]", vector.toString());
+
+        vector = Vector.create(array, false);
+        assertEquals("[10, 100, -56]", vector.toString());
+    }
 }




Mime
View raw message