sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1755522 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/math/ test/java/org/apache/sis/math/
Date Mon, 08 Aug 2016 17:43:14 GMT
Author: desruisseaux
Date: Mon Aug  8 17:43:14 2016
New Revision: 1755522

URL: http://svn.apache.org/viewvc?rev=1755522&view=rev
Log:
Allow the Vector implementations to return a narrower primitive type if it can hold the value.
Added a toString(int) method.

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/main/java/org/apache/sis/math/package-info.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=1755522&r1=1755521&r2=1755522&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] Mon Aug  8 17:43:14 2016
@@ -33,30 +33,98 @@ import org.apache.sis.internal.util.Nume
  * @module
  */
 abstract class ArrayVector<E extends Number> extends Vector implements CheckedContainer<E>, Serializable {
-    /** For cross-version compatibility. */
+    /**
+     * For cross-version compatibility.
+     */
     private static final long serialVersionUID = 3496467575389288163L;
 
-    /** For sub-classes constructor. */
-    ArrayVector() {}
+    /**
+     * For sub-classes constructor.
+     */
+    ArrayVector() {
+    }
 
-    /** Default implementation for the convenience of direct sub-types. */
-    @Override public boolean isUnsigned() {
+    /**
+     * Default implementation for the convenience of direct sub-types.
+     */
+    @Override
+    public boolean isUnsigned() {
         return false;
     }
 
-    /** Default implementation for the convenience of wrapper of integer types. */
-    @Override public boolean    isNaN(int index) {return false;}
-    @Override public long   longValue(int index) {throw canNotConvert(long .class);}
-    @Override public int     intValue(int index) {throw canNotConvert(int  .class);}
-    @Override public short shortValue(int index) {throw canNotConvert(short.class);}
-    @Override public byte   byteValue(int index) {throw canNotConvert(byte .class);}
+    /**
+     * Default implementation for the convenience of wrapper of integer types.
+     */
+    @Override
+    public boolean isNaN(int index) {
+        return false;
+    }
+
+    /**
+     * 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 ClassCastException canNotConvert(final Class<?> target) {
-        return new ClassCastException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
+    private ArithmeticException canNotConvert(final Class<?> target) {
+        return new ArithmeticException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
                 Numbers.wrapperToPrimitive(getElementType()), target));
     }
 
@@ -102,6 +170,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Double.isNaN(array[index]);
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Double.toString(array[index]);
+        }
+
         /** Returns the value at the given index. */
         @Override public double doubleValue(final int index) {
             return array[index];
@@ -159,6 +232,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Float.isNaN(array[index]);
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Float.toString(array[index]);
+        }
+
         /** Returns the value at the given index. */
         @Override public double doubleValue(int index) {return array[index];}
         @Override public float   floatValue(int index) {return array[index];}
@@ -192,6 +270,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Long.class;
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Long.toString(array[index]);
+        }
+
         @Override public final int     size()          {return array.length;}
         @Override public double doubleValue(int index) {return array[index];}
         @Override public float   floatValue(int index) {return array[index];}
@@ -225,6 +308,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Integer.class;
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Integer.toString(array[index]);
+        }
+
         @Override public final int     size()          {return array.length;}
         @Override public double doubleValue(int index) {return array[index];}
         @Override public float   floatValue(int index) {return array[index];}
@@ -259,6 +347,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Short.class;
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Short.toString(array[index]);
+        }
+
         @Override public final int     size()          {return array.length;}
         @Override public double doubleValue(int index) {return array[index];}
         @Override public float   floatValue(int index) {return array[index];}
@@ -294,6 +387,11 @@ abstract class ArrayVector<E extends Num
             return java.lang.Byte.class;
         }
 
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Byte.toString(array[index]);
+        }
+
         @Override public final int     size()          {return array.length;}
         @Override public double doubleValue(int index) {return array[index];}
         @Override public float   floatValue(int index) {return array[index];}
@@ -341,6 +439,11 @@ abstract class ArrayVector<E extends Num
             if (value >= 0) return value;
             throw new ArithmeticException();
         }
+
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Long.toUnsignedString(super.longValue(index));
+        }
     }
 
     /**
@@ -365,6 +468,11 @@ abstract class ArrayVector<E extends Num
             if (value >= 0) return value;
             throw new ArithmeticException();
         }
+
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Integer.toUnsignedString(super.intValue(index));
+        }
     }
 
     /**
@@ -388,6 +496,11 @@ abstract class ArrayVector<E extends Num
             if (value >= 0) return value;
             throw new ArithmeticException();
         }
+
+        /** Returns the string representation at the given index. */
+        @Override public String toString(final int index) {
+            return java.lang.Integer.toString(intValue(index));
+        }
     }
 
     /**
@@ -412,5 +525,10 @@ abstract class ArrayVector<E extends Num
             if (value >= 0) return value;
             throw new ArithmeticException();
         }
+
+        /** Returns the string representation at the given index. */
+        @Override public String toString(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=1755522&r1=1755521&r2=1755522&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] Mon Aug  8 17:43:14 2016
@@ -17,7 +17,7 @@
 package org.apache.sis.math;
 
 import java.io.Serializable;
-import org.apache.sis.util.Numbers;
+import org.apache.sis.util.Classes;
 
 
 /**
@@ -57,11 +57,11 @@ final class ConcatenatedVector extends V
     }
 
     /**
-     * Returns widest type of the two vectors.
+     * Returns the parent type of the two vectors.
      */
     @Override
     public Class<? extends Number> getElementType() {
-        return Numbers.widestClass(first.getElementType(), second.getElementType());
+        return Classes.findCommonClass(first.getElementType(), second.getElementType()).asSubclass(Number.class);
     }
 
     /**
@@ -186,6 +186,21 @@ final class ConcatenatedVector extends V
     }
 
     /**
+     * Returns the string representation at the given index.
+     */
+    @Override
+    public String toString(int index) {
+        final Vector v;
+        if (index < limit) {
+            v = first;
+        } else {
+            v = second;
+            index -= limit;
+        }
+        return v.toString(index);
+    }
+
+    /**
      * Returns the value at the given index.
      *
      * @throws ArrayIndexOutOfBoundsException if the given index is out of bounds.
@@ -221,14 +236,14 @@ final class ConcatenatedVector extends V
      * Delegates to the backing vectors if possible.
      */
     @Override
-    Vector createSubList(final int first, final int step, final int length) {
+    Vector createSubSampling(final int first, final int step, final int length) {
         if (first >= limit) {
-            return second.subList(first - limit, step, length);
+            return second.subSampling(first - limit, step, length);
         }
         if (first + step*length <= limit) {
-            return this.first.subList(first, step, length);
+            return this.first.subSampling(first, step, length);
         }
-        return super.createSubList(first, step, length);
+        return super.createSubSampling(first, step, length);
     }
 
     /**

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=1755522&r1=1755521&r2=1755522&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] Mon Aug  8 17:43:14 2016
@@ -165,6 +165,14 @@ final class SequenceVector extends Vecto
     }
 
     /**
+     * Returns the string representation of the value at the given index.
+     */
+    @Override
+    public String toString(final int index) throws IndexOutOfBoundsException {
+        return String.valueOf(doubleValue(index));
+    }
+
+    /**
      * Computes the value at the given index.
      */
     @Override
@@ -184,7 +192,7 @@ final class SequenceVector extends Vecto
      * Creates a new sequence.
      */
     @Override
-    Vector createSubList(final int first, final int step, final int length) {
+    Vector createSubSampling(final int first, final int step, final int length) {
         return new SequenceVector(doubleValue(first), increment*step, length);
     }
 }

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=1755522&r1=1755521&r2=1755522&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] Mon Aug  8 17:43:14 2016
@@ -36,17 +36,35 @@ import static org.apache.sis.util.Argume
  *   <li><var>y[i]</var> is the measurement of a phenomenon at time <var>x[i]</var>.</li>
  * </ul>
  *
- * Instances of {@code Vector} are created by calls to the {@link #create(Object, boolean)} static method.
+ * <div class="section">Instantiation</div>
+ * Instances of {@code Vector} are usually created by calls to the {@link #create(Object, boolean)} static method.
  * The supplied array is not cloned – changes to the primitive array are reflected in the vector, and vis-versa.
- *
- * Vectors can be created over a subrange of an array, provides a view of the elements in reverse
- * order, <i>etc</i>. The example below creates a view over a subrange:
+ * Vectors can be a view over a subsection of the given array, or can provide a view of the elements in reverse order,
+ * <i>etc</i>. The example below creates a view over a subsection:
  *
  * {@preformat java
- *     Vector v = Vector.create(array).subList(lower, upper)
+ *     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].
  * }
  *
- * <div class="note"><b>Note:</b>
+ * <div class="section">Usage</div>
+ * The methods that are most often used after {@code Vector} creation are {@link #size()} and {@link #doubleValue(int)}
+ * or {@link #intValue(int)}. Those methods make abstraction of the underlying data type. For example if the vector is
+ * backed by an array of type {@code int[]}, then calls to {@code doubleValue(index)} will:
+ *
+ * <ul>
+ *   <li>Convert the {@code int[index]} value to a {@code double} value.</li>
+ *   <li>If {@link #isUnsigned()} is {@code true}, apply the necessary bitmask before conversion.</li>
+ * </ul>
+ *
+ * Widening conversions (for example from {@code short} to {@code long}) are always allowed.
+ * Narrowing conversions are allowed if the result can be represented at least approximatively by the target type.
+ * For example conversions from {@code double} to {@code float} are always allowed (values that are too large for
+ * the {@code float} type are represented by positive of negative infinity), but conversions from {@code long} to
+ * {@code short} are allowed only if the value is between {@link Short#MIN_VALUE} and {@link Short#MAX_VALUE} inclusive.
+ *
+ * <div class="note"><b>Comparison with other API:</b>
  * the above functionalities look like similar functionalities provided by {@link java.nio.ByteBuffer}
  * in standard Java, but they actually serve different purposes. The {@code ByteBuffer} getter methods
  * (for example {@code getShort(int)}, {@code getLong(int)}, <i>etc.</i>) allow to decode a sequence of
@@ -58,9 +76,6 @@ import static org.apache.sis.util.Argume
  * For example a method performing a numerical calculation may want to see the data as {@code double} values
  * without concern about whether the data were really stored as {@code double} or as {@code float} values.</div>
  *
- * The methods that are most often used after {@code Vector} creation are {@link #size()} and
- * {@link #doubleValue(int)} or {@link #intValue(int)}.
- *
  * @author  Martin Desruisseaux (MPO, Geomatys)
  * @since   0.8
  * @version 0.8
@@ -166,7 +181,7 @@ public abstract class Vector extends Abs
      *
      * <p>Users of the {@link #doubleValue(int)} method do not need to care about this information since
      * {@code Vector} will perform automatically the type conversion. Users of other methods may want to
-     * verify this information for avoiding {@link ClassCastException}.</p>
+     * verify this information for avoiding {@link ArithmeticException}.</p>
      *
      * @return the type of elements in this vector.
      */
@@ -174,8 +189,8 @@ public abstract class Vector extends Abs
 
     /**
      * Returns {@code true} if integer values shall be interpreted as unsigned values.
-     * This method may return {@code true} only for data stored in {@code byte[]},
-     * {@code short[]}, {@code int[]} or {@code long[]} arrays.
+     * This method may return {@code true} for data stored in {@code byte[]}, {@code short[]}, {@code int[]}
+     * or {@code long[]} arrays, but never for data stored in {@code float[]} and {@code double[]} arrays.
      *
      * <p>Unless otherwise noticed in Javadoc, users do not need to care about this information since
      * {@code Vector} methods will perform automatically the operations needed for unsigned integers.</p>
@@ -195,123 +210,110 @@ public abstract class Vector extends Abs
     /**
      * Returns {@code true} if the value at the given index is {@code NaN}.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
      * @return {@code true} if the value at the given index is {@code NaN}.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      */
-    public abstract boolean isNaN(final int index) throws IndexOutOfBoundsException;
+    public abstract boolean isNaN(int index) throws IndexOutOfBoundsException;
 
     /**
      * Returns the value at the given index as a {@code double}.
      * This is the safest method since all primitive types supported by {@code Vector}
      * are convertible to the {@code double} type.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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.
      */
-    public abstract double doubleValue(final int index) throws IndexOutOfBoundsException;
+    public abstract double doubleValue(int index) throws IndexOutOfBoundsException;
 
     /**
      * Returns the value at the given index as a {@code float}.
-     * If this vector uses internally a wider type like {@code double}, then this method may
-     * cast the value or throw an exception at implementation choice. The general guidelines
-     * are:
+     * This method may result in a lost of precision if the underlying storage is a {@code double[]} array.
      *
-     * <ul>
-     *   <li>If the value is read from a primitive array of a wider type (typically through a
-     *       vector created by {@link #create(Object)}, throw an exception because we assume
-     *       that the data provider thinks that the extra precision is needed.</li>
-     *   <li>If the value is the result of a computation (typically through a vector created
-     *       by {@link #createSequence}), cast the value because the calculation accuracy is
-     *       often unknown to the vector - and not necessarily its job.</li>
-     * </ul>
-     *
-     * For safety users should either call {@link #doubleValue(int)} in all cases, or call this
-     * methods only if the type returned by {@link #getElementType()} has been verified.
-     *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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 ClassCastException if the component type can not be converted to a
-     *         {@code float} by an identity or widening conversion.
      */
-    public abstract float floatValue(final int index) throws IndexOutOfBoundsException, ClassCastException;
+    public abstract float floatValue(int index) throws IndexOutOfBoundsException;
 
     /**
      * Returns the value at the given index as a {@code long}.
-     * If this vector uses internally a wider type, then this method may cast the value
-     * or throw an exception according the same guidelines than {@link #floatValue(int)}.
+     * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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 ClassCastException if the component type can not be converted to a
-     *         {@code long} by an identity or widening conversion.
+     * @throws ArithmeticException if the value is too large for the capacity of the {@code long} type.
      */
-    public abstract long longValue(final int index) throws IndexOutOfBoundsException, ClassCastException;
+    public abstract long longValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
 
     /**
      * Returns the value at the given index as an {@code int}.
-     * If this vector uses internally a wider type, then this method may cast the value
-     * or throw an exception according the same guidelines than {@link #floatValue(int)}.
+     * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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 ClassCastException if the component type can not be converted to an
-     *         {@code int} by an identity or widening conversion.
+     * @throws ArithmeticException if the value is too large for the capacity of the {@code int} type.
      */
-    public abstract int intValue(final int index) throws IndexOutOfBoundsException, ClassCastException;
+    public abstract int intValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
 
     /**
      * Returns the value at the given index as a {@code short}.
-     * If this vector uses internally a wider type, then this method may cast the value
-     * or throw an exception according the same guidelines than {@link #floatValue(int)}.
+     * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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 ClassCastException if the component type can not be converted to a
-     *         {@code short} by an identity or widening conversion.
+     * @throws ArithmeticException if the value is too large for the capacity of the {@code short} type.
      */
-    public abstract short shortValue(final int index) throws IndexOutOfBoundsException, ClassCastException;
+    public abstract short shortValue(int index) throws IndexOutOfBoundsException, ArithmeticException;
 
     /**
      * Returns the value at the given index as a {@code byte}.
-     * If this vector uses internally a wider type, then this method may cast the value
-     * or throw an exception according the same guidelines than {@link #floatValue(int)}.
+     * If this vector uses floating point values, the value is rounded to the nearest integer.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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 ClassCastException if the component type can not be converted to a
-     *         {@code byte} by an identity or widening conversion.
+     * @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;
+
+    /**
+     * Returns a string representation of the value at the given index.
+     * Invoking this method is generally equivalent to invoking
+     * <code>String.valueOf({@linkplain #get(int) get}(index))</code>
+     * except if the values are {@linkplain #isUnsigned() unsigned integers}.
+     *
+     * @param  index  the index in the [0 … {@linkplain #size() size}-1] range.
+     * @return a string representation of the value at the given index.
+     * @throws IndexOutOfBoundsException if the given index is out of bounds.
      */
-    public abstract byte byteValue(final int index) throws IndexOutOfBoundsException, ClassCastException;
+    public abstract String toString(int index) throws IndexOutOfBoundsException;
 
     /**
      * Returns the number at the given index, or {@code null} if none.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
+     * @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.
      */
     @Override
-    public abstract Number get(final int index) throws IndexOutOfBoundsException;
+    public abstract Number get(int index) throws IndexOutOfBoundsException;
 
     /**
      * Sets the number at the given index.
      *
-     * @param  index the index in the [0 … {@linkplain #size size}-1] range.
-     * @param  value the value to set at the given index.
+     * @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.
      */
     @Override
-    public abstract Number set(final int index, final Number value)
-            throws IndexOutOfBoundsException, ArrayStoreException;
+    public abstract Number set(int index, Number value) throws IndexOutOfBoundsException, ArrayStoreException;
 
     /**
      * If this vector is a view over an other vector, returns the backing vector.
@@ -330,17 +332,17 @@ public abstract class Vector extends Abs
      *
      * <p>Only subclasses that are views of this vector will override this method.</p>
      *
-     * @param  index the indexes given by the user.
+     * @param  indices  the indexes given by the user.
      * @return the indexes to use. Must be a new array in order to protect it from user changes.
      * @throws IndexOutOfBoundsException if at least one index is out of bounds.
      */
-    int[] toBacking(int[] index) throws IndexOutOfBoundsException {
-        index = index.clone();
+    int[] toBacking(int[] indices) throws IndexOutOfBoundsException {
+        indices = indices.clone();
         final int length = size();
-        for (int i : index) {
+        for (int i : indices) {
             ensureValidIndex(length, i);
         }
-        return index;
+        return indices;
     }
 
     /**
@@ -348,55 +350,58 @@ public abstract class Vector extends Abs
      * 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>The indexes don't need to be in any particular order. The same index can be repeated
+     * <p>The indexes do not need to be in any particular order. The same index can be repeated
      * more than once. Thus it is possible to create a vector larger than the original vector.</p>
      *
-     * @param  index index of the values to be returned.
+     * @param  indices  indexes of the values to be returned.
      * @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... index) throws IndexOutOfBoundsException {
-        index = toBacking(index);
+    public Vector view(int... indices) throws IndexOutOfBoundsException {
+        indices = toBacking(indices);
         final int first, step;
-        switch (index.length) {
+        switch (indices.length) {
             case 0: {
                 first = 0;
                 step  = 1;
                 break;
             }
             case 1: {
-                first = index[0];
+                first = indices[0];
                 step  = 1;
                 break;
             }
             default: {
                 int limit;
-                first = index[0];
-                limit = index[1];
+                first = indices[0];
+                limit = indices[1];
                 step  = limit - first;
-                for (int i=2; i<index.length; i++) {
-                    final int current = index[i];
+                for (int i=2; i<indices.length; i++) {
+                    final int current = indices[i];
                     if (current - limit != step) {
-                        return backingVector().new View(index);
+                        return backingVector().new View(indices);
                     }
                     limit = current;
                 }
                 break;
             }
         }
-        return subList(first, step, index.length);
+        return subSampling(first, step, indices.length);
     }
 
     /**
-     * Returns a view which contains the values of this vector in reverse order. This method delegates its work
-     * to <code>{@linkplain #subList(int,int,int) subList}(size-1, -1, {@linkplain #size() size})</code>.
+     * 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) ? subList(length-1, -1, length) : this;
+        return (length != 0) ? subSampling(length-1, -1, length) : this;
     }
 
     /**
@@ -404,18 +409,19 @@ public abstract class Vector extends Abs
      * The returned view will contain the values from index {@code lower} inclusive to
      * {@code upper} exclusive.
      *
-     * <p>This method delegates its work to <code>{@linkplain #subList(int,int,int) subList}(lower,
-     * 1, upper-lower)</code>. It is declared final in order to force every subclasses to override
-     * the later method instead than this one.</p>
+     * <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.
+     * @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.
+     * @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 subList(lower, 1, upper - lower);
+        return subSampling(lower, 1, upper - lower);
     }
 
     /**
@@ -428,36 +434,36 @@ public abstract class Vector extends Abs
      * <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.
+     * @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.
+     *         is outside the [0 … {@linkplain #size() size}-1] range.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
-    public Vector subList(final int first, final int step, final int length) throws IndexOutOfBoundsException {
+    public Vector subSampling(final int first, final int step, final int length) throws IndexOutOfBoundsException {
         if (step == 1 && first == 0 && length == size()) {
             return this;
         }
-        return createSubList(first, step, length);
+        return createSubSampling(first, step, length);
     }
 
     /**
-     * Implementation of {@link #subList(int,int,int)} to be overridden by subclasses.
+     * Implementation of {@link #subSampling(int,int,int)} to be overridden by subclasses.
      */
-    Vector createSubList(final int first, final int step, final int length) {
-        return new SubList(first, step, length);
+    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]
+     * 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.
+     * @param  toAppend  the vector to concatenate at the end of this vector.
      * @return the concatenation of this vector with the given vector.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
@@ -482,22 +488,18 @@ public abstract class Vector extends Abs
      * A view over an other vector at pre-selected indexes.
      */
     private final class View extends Vector implements Serializable {
-        /**
-         * For cross-version compatibility.
-         */
+        /** For cross-version compatibility. */
         private static final long serialVersionUID = 6574040261355090760L;
 
-        /**
-         * The pre-selected indexes.
-         */
-        private final int[] index;
+        /** The pre-selected indexes. */
+        private final int[] indices;
 
         /**
          * 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.
          */
-        public View(int[] index) {
-            this.index = index;
+        View(int[] indices) {
+            this.indices = indices;
         }
 
         /** Returns the backing vector. */
@@ -509,7 +511,7 @@ public abstract class Vector extends Abs
         @Override int[] toBacking(final int[] i) throws IndexOutOfBoundsException {
             final int[] ni = new int[i.length];
             for (int j=0; j<ni.length; j++) {
-                ni[j] = index[i[j]];
+                ni[j] = indices[i[j]];
             }
             return ni;
         }
@@ -519,69 +521,28 @@ public abstract class Vector extends Abs
             return Vector.this.getElementType();
         }
 
-        /** Returns whether the type should be interpreted as an unsigned integer type. */
-        @Override public boolean isUnsigned() {
-            return Vector.this.isUnsigned();
-        }
-
-        /** Returns the length of this view. */
-        @Override public int size() {
-            return index.length;
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public boolean isNaN(final int i) {
-            return Vector.this.isNaN(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public double doubleValue(final int i) {
-            return Vector.this.doubleValue(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public float floatValue(final int i) {
-            return Vector.this.floatValue(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public long longValue(final int i) {
-            return Vector.this.longValue(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public int intValue(final int i) {
-            return Vector.this.intValue(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public short shortValue(final int i) {
-            return Vector.this.shortValue(index[i]);
-        }
-
         /** Delegates to the enclosing vector. */
-        @Override public byte byteValue(final int i) {
-            return Vector.this.byteValue(index[i]);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public Number get(final int i) {
-            return Vector.this.get(index[i]);
-        }
+        @Override public int      size()                {return indices.length;}
+        @Override public boolean  isUnsigned()          {return Vector.this.isUnsigned();}
+        @Override public boolean  isNaN      (int i)    {return Vector.this.isNaN      (indices[i]);}
+        @Override public double   doubleValue(int i)    {return Vector.this.doubleValue(indices[i]);}
+        @Override public float    floatValue (int i)    {return Vector.this.floatValue (indices[i]);}
+        @Override public long     longValue  (int i)    {return Vector.this.longValue  (indices[i]);}
+        @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 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);}
 
         /** Delegates to the enclosing vector. */
-        @Override public Number set(final int i, final Number value) {
-            return Vector.this.set(index[i], value);
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override Vector createSubList(int first, final int step, final int length) {
+        @Override Vector createSubSampling(int first, final int step, final int length) {
             ensureValid(first, step, length);
             final int[] ni = new int[length];
             if (step == 1) {
-                System.arraycopy(index, first, ni, 0, length);
+                System.arraycopy(indices, first, ni, 0, length);
             } else for (int j=0; j<length; j++) {
-                ni[j] = index[first];
+                ni[j] = indices[first];
                 first += step;
             }
             return Vector.this.view(ni);
@@ -590,9 +551,9 @@ public abstract class Vector extends Abs
         /** Concatenates the indexes if possible. */
         @Override Vector createConcatenate(final Vector toAppend) {
             if (toAppend instanceof View && toAppend.backingVector() == Vector.this) {
-                final int[] other = ((View) toAppend).index;
-                final int[] c = Arrays.copyOf(index, index.length + other.length);
-                System.arraycopy(other, 0, c, index.length, other.length);
+                final int[] other = ((View) 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 super.createConcatenate(toAppend);
@@ -622,31 +583,21 @@ public abstract class Vector extends Abs
     /**
      * A view over an other vector in a range of index.
      */
-    private final class SubList extends Vector implements Serializable {
-        /**
-         * For cross-version compatibility.
-         */
+    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.
-         */
+        /** Index of the first element in the enclosing vector. */
         private final int first;
 
-        /**
-         * The index increment. May be negative but not zero.
-         */
+        /** The index increment. May be negative but not zero. */
         private final int step;
 
-        /**
-         * The length of this vector.
-         */
+        /** The length of this vector. */
         private final int length;
 
-        /**
-         * Creates a new view over the given range.
-         */
-        protected SubList(final int first, final int step, 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;
@@ -678,74 +629,37 @@ public abstract class Vector extends Abs
             return Vector.this.getElementType();
         }
 
-        /** Returns whether the type should be interpreted as an unsigned integer type. */
-        @Override public boolean isUnsigned() {
-            return Vector.this.isUnsigned();
-        }
-
         /** Returns the length of this subvector. */
         @Override public int size() {
             return length;
         }
 
         /** Delegates to the enclosing vector. */
-        @Override public boolean isNaN(final int index) {
-            return Vector.this.isNaN(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public double doubleValue(final int index) {
-            return Vector.this.doubleValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public float floatValue(final int index) {
-            return Vector.this.floatValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public long longValue(final int index) {
-            return Vector.this.longValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public int intValue(final int index) {
-            return Vector.this.intValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public short shortValue(final int index) {
-            return Vector.this.shortValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public byte byteValue(final int index) {
-            return Vector.this.byteValue(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public Number get(final int index) {
-            return Vector.this.get(toBacking(index));
-        }
-
-        /** Delegates to the enclosing vector. */
-        @Override public Number set(final int index, final Number value) {
-            return Vector.this.set(toBacking(index), value);
-        }
+        @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);}
 
         /** Delegates to the enclosing vector. */
-        @Override Vector createSubList(int first, int step, final int length) {
+        @Override Vector createSubSampling(int first, int step, final int length) {
             first = toBacking(first);
             step *= this.step;
-            return Vector.this.subList(first, step, length);
+            return Vector.this.subSampling(first, step, length);
         }
 
         /** Delegates to the enclosing vector if possible. */
         @Override Vector createConcatenate(final Vector toAppend) {
-            if (toAppend instanceof SubList && toAppend.backingVector() == Vector.this) {
-                final SubList other = (SubList) 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.createSubList(first, step, length + other.length);
+                    return Vector.this.createSubSampling(first, step, length + other.length);
                 }
             }
             return super.createConcatenate(toAppend);

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java?rev=1755522&r1=1755521&r2=1755522&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java [UTF-8] Mon Aug  8 17:43:14 2016
@@ -19,21 +19,25 @@
  * A set of mathematical objects and algebraic utilities.
  * This package provides:
  *
- * <ul>
- *   <li>Static functions in {@link org.apache.sis.math.MathFunctions} and {@link org.apache.sis.math.DecimalFunctions}.</li>
+ * <ul class="verbose">
+ *   <li>Mathematical functions as static methods in {@link org.apache.sis.math.MathFunctions} and
+ *       {@link org.apache.sis.math.DecimalFunctions}.</li>
+ *
  *   <li>{@link org.apache.sis.math.Statistics} accumulator, optionally with statistics on derivatives
  *       and {@linkplain org.apache.sis.math.StatisticsFormat tabular formatting}.</li>
+ *
  *   <li>Simple equations for {@link org.apache.sis.math.Line} and {@link org.apache.sis.math.Plane}
- *       with capability to determine the coefficients from a set of coordinates.</li>
- *   <li>{@link org.apache.sis.math.Vector} as views over arrays of primitive types.
- *       Those views make abstraction of the data type (e.g. {@code float} or {@code double})
- *       and whether the integer types are signed or unsigned.</li>
- * </ul>
+ *       with capability to determine the coefficients from a set of coordinates.
  *
- * <div class="note"><b>Note:</b>
- * {@code Line} and {@code Plane} classes are not geometric objects since they are not bounded in space.
- * For example the {@link java.awt.geom.Line2D} geometry have starting and ending points, while the
- * {@code Line} class in this package extends to infinite.</div>
+ *       <div class="note"><b>Note:</b>
+ *       {@code Line} and {@code Plane} classes are not geometric objects since they are not bounded in space.
+ *       For example the {@link java.awt.geom.Line2D} geometry have starting and ending points, while the
+ *       {@code Line} class in this package extends to infinite.</div></li>
+ *
+ *   <li>{@link org.apache.sis.math.Vector} of real numbers, typically as views over arrays of primitive types.
+ *       Those views make abstraction of the data type (e.g. {@code float}, {@code double}, {@code int},
+ *       unsigned {@code int}, <i>etc.</i>).</li>
+ * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3

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=1755522&r1=1755521&r2=1755522&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] Mon Aug  8 17:43:14 2016
@@ -95,13 +95,16 @@ public final strictfp class VectorTest e
         }
         try {
             vector.byteValue(0);
-            fail("Expected a ClassCastException");
-        } catch (ClassCastException e) {
+            fail("Expected a ArithmeticException");
+        } catch (ArithmeticException e) {
             // This is the expected exception.
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("short"));
+            assertTrue(message, message.contains("byte"));
         }
 
         // Tests subvector in the range [100:2:298].
-        vector = vector.subList(100, 2, 100);
+        vector = vector.subSampling(100, 2, 100);
         assertEquals(100, vector.size());
         for (int i=0; i<100; i++) {
             assertEquals(array[i*2 + 100], vector.shortValue(i));
@@ -195,7 +198,7 @@ public final strictfp class VectorTest e
         Vector v2 = Vector.create(extra, false);
         Vector v3 = v1.concatenate(v2);
         assertEquals("Length of V3 should be the sum of V1 and V2 length.", 60, v3.size());
-        assertEquals("Component type should be the widest of V1 and V2.", Float.class, v3.getElementType());
+        assertEquals("Component type should be the common parent of V1 and V2.", Number.class, v3.getElementType());
         assertEquals("Sample from V1.", Float  .valueOf(200), v3.get(20));
         assertEquals("Sample from V2.", Integer.valueOf(500), v3.get(50));
         for (int i=0; i<60; i++) {



Mime
View raw message