sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1799204 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/math/SequenceVector.java main/java/org/apache/sis/math/Vector.java main/java/org/apache/sis/util/Numbers.java test/java/org/apache/sis/math/VectorTest.java
Date Mon, 19 Jun 2017 12:47:43 GMT
Author: desruisseaux
Date: Mon Jun 19 12:47:43 2017
New Revision: 1799204

URL: http://svn.apache.org/viewvc?rev=1799204&view=rev
Log:
When compressing a vector as a SequenceVector, preserve the element type.

Modified:
    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/util/Numbers.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/SequenceVector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java?rev=1799204&r1=1799203&r2=1799204&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 Jun 19 12:47:43 2017
@@ -19,6 +19,7 @@ package org.apache.sis.math;
 import java.util.Arrays;
 import java.io.Serializable;
 import org.apache.sis.measure.NumberRange;
+import org.apache.sis.util.Numbers;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
@@ -36,7 +37,12 @@ abstract class SequenceVector extends Ve
     /**
      * For cross-version compatibility.
      */
-    private static final long serialVersionUID = 7980737287789566091L;
+    private static final long serialVersionUID = 2544089499300079707L;
+
+    /**
+     * The type of values in the vector.
+     */
+    final Class<? extends Number> type;
 
     /**
      * The length of this vector.
@@ -46,7 +52,8 @@ abstract class SequenceVector extends Ve
     /**
      * Creates a sequence of numbers of the given length.
      */
-    SequenceVector(final int length) {
+    SequenceVector(final Class<? extends Number> type, final int length) {
+        this.type   = type;
         this.length = length;
         if (length < 0) {
             throw new IllegalArgumentException(Errors.format(
@@ -55,6 +62,14 @@ abstract class SequenceVector extends Ve
     }
 
     /**
+     * Returns the type of elements.
+     */
+    @Override
+    public final Class<? extends Number> getElementType() {
+        return type;
+    }
+
+    /**
      * {@code SequenceVector} values are always interpreted as signed values.
      */
     @Override
@@ -137,24 +152,20 @@ abstract class SequenceVector extends Ve
         /**
          * Creates a sequence of numbers in a given range of values using the given increment.
          *
+         * @param  type       the type of elements in the sequence.
          * @param  first      the first value, inclusive.
          * @param  increment  the difference between the values at two adjacent indexes.
          * @param  length     the length of the vector.
          */
-        Doubles(final Number first, final Number increment, final int length) {
-            super(length);
+        Doubles(final Class<? extends Number> type, final Number first, final Number
increment, final int length) {
+            super(type, length);
             this.first     = first.doubleValue();
             this.increment = increment.doubleValue();
         }
 
         /** Creates a new sequence for a subrange of this vector. */
         @Override Vector createSubSampling(final int offset, final int step, final int n)
{
-            return new Doubles(doubleValue(offset), increment*step, n);
-        }
-
-        /** Returns the type of elements. */
-        @Override public Class<Double> getElementType() {
-            return Double.class;
+            return new Doubles(type, doubleValue(offset), increment*step, n);
         }
 
         /** Returns {@code true} if this vector contains only integer values. */
@@ -187,23 +198,24 @@ abstract class SequenceVector extends Ve
 
         /** Computes the value at the given index. */
         @Override public Number get(final int index) {
-            return doubleValue(index);
+            return Numbers.wrap(doubleValue(index), type);
         }
 
         /** Returns the increment between all consecutive values */
         @Override public Number increment(final double tolerance) {
-            return increment;
+            return Numbers.wrap(increment, type);
         }
 
         /** Computes the minimal and maximal values in this vector. */
-        @Override public NumberRange<Double> range() {
+        @SuppressWarnings({"unchecked","rawtypes"})
+        @Override public NumberRange<?> range() {
             double min = first;
             double max = first + increment * (length - 1);
             if (max < min) {
                 min = max;
                 max = first;
             }
-            return NumberRange.create(min, true, max, true);
+            return new NumberRange(type, Numbers.wrap(min, type), true, Numbers.wrap(max,
type), true);
         }
     }
 
@@ -231,24 +243,20 @@ abstract class SequenceVector extends Ve
         /**
          * Creates a sequence of numbers in a given range of values using the given increment.
          *
+         * @param  type       the type of elements in the sequence.
          * @param  first      the first value, inclusive.
          * @param  increment  the difference between the values at two adjacent indexes.
          * @param  length     the length of the vector.
          */
-        Longs(final Number first, final Number increment, final int length) {
-            super(length);
+        Longs(final Class<? extends Number> type, final Number first, final Number
increment, final int length) {
+            super(type, length);
             this.first     = first.longValue();
             this.increment = increment.longValue();
         }
 
         /** Creates a new sequence for a subrange of this vector. */
         @Override Vector createSubSampling(final int offset, final int step, final int n)
{
-            return new Longs(longValue(offset), increment*step, n);
-        }
-
-        /** Returns the type of elements. */
-        @Override public Class<Long> getElementType() {
-            return Long.class;
+            return new Longs(type, longValue(offset), increment*step, n);
         }
 
         /** Returns {@code true} since this vector contains only integer values. */
@@ -284,23 +292,24 @@ abstract class SequenceVector extends Ve
 
         /** Computes the value at the given index. */
         @Override public Number get(final int index) {
-            return longValue(index);
+            return Numbers.wrap(longValue(index), type);
         }
 
         /** Returns the increment between all consecutive values */
         @Override public Number increment(final double tolerance) {
-            return increment;
+            return Numbers.wrap(increment, type);
         }
 
         /** Computes the minimal and maximal values in this vector. */
-        @Override public NumberRange<Long> range() {
+        @SuppressWarnings({"unchecked","rawtypes"})
+        @Override public NumberRange<?> range() {
             long min = first;
             long max = first + increment * (length - 1);
             if (max < min) {
                 min = max;
                 max = first;
             }
-            return NumberRange.create(min, true, max, true);
+            return new NumberRange(type, Numbers.wrap(min, type), true, Numbers.wrap(max,
type), true);
         }
     }
 }

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=1799204&r1=1799203&r2=1799204&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 Jun 19 12:47:43 2017
@@ -174,12 +174,19 @@ public abstract class Vector extends Abs
         type = Numbers.widestClass(first, increment);
         type = Numbers.widestClass(type,
                Numbers.narrowestClass(first.doubleValue() + increment.doubleValue() * (length-1)));
+        return createSequence(type, first, increment, length);
+    }
+
+    /**
+     * Creates a sequence of the given type.
+     */
+    static Vector createSequence(final Class<? extends Number> type, final Number first,
final Number increment, final int length) {
         final int t = Numbers.getEnumConstant(type);
         if (t >= Numbers.BYTE && t <= Numbers.LONG) {
             // Use the long type if possible because not all long values can be represented
as double.
-            return new SequenceVector.Longs(first, increment, length);
+            return new SequenceVector.Longs(type, first, increment, length);
         } else {
-            return new SequenceVector.Doubles(first, increment, length);
+            return new SequenceVector.Doubles(type, first, increment, length);
         }
     }
 
@@ -973,7 +980,7 @@ public abstract class Vector extends Abs
         final int length = size();
         final Number inc = increment(tolerance);
         if (inc != null) {
-            return createSequence(get(0), inc, length);
+            return createSequence(getElementType(), get(0), inc, length);
         }
         /*
          * Verify if the vector contains only NaN values. This extra check is useful because
'increment()'
@@ -984,7 +991,7 @@ public abstract class Vector extends Abs
             if (!isNaN(i)) return this;
         }
         final Double NaN = Numerics.valueOf(Double.NaN);
-        return new SequenceVector.Doubles(NaN, NaN, length);
+        return new SequenceVector.Doubles(getElementType(), NaN, NaN, length);
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java?rev=1799204&r1=1799203&r2=1799204&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8]
Mon Jun 19 12:47:43 2017
@@ -38,7 +38,7 @@ import static java.lang.Double.doubleToL
  * Static methods working with {@link Number} objects, and a few primitive types by extension.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 0.3
+ * @version 0.8
  *
  * @see org.apache.sis.math.MathFunctions
  *
@@ -560,7 +560,7 @@ public final class Numbers extends Stati
     }
 
     /**
-     * Wraps the given value in a {@code Number} of the specified class.
+     * Wraps the given floating-point value in a {@code Number} of the specified class.
      * The given type shall be one of {@link Byte}, {@link Short}, {@link Integer}, {@link
Long},
      * {@link Float}, {@link Double}, {@link BigInteger} and {@link BigDecimal} classes.
      * Furthermore, the given value shall be convertible to the given class without precision
lost,
@@ -593,6 +593,44 @@ public final class Numbers extends Stati
             throw new IllegalArgumentException(Errors.format(Errors.Keys.CanNotConvertValue_2,
value, type));
         }
         return number;
+    }
+
+    /**
+     * Wraps the given integer value in a {@code Number} of the specified class.
+     * The given type shall be one of {@link Byte}, {@link Short}, {@link Integer}, {@link
Long},
+     * {@link Float}, {@link Double}, {@link BigInteger} and {@link BigDecimal} classes.
+     * Furthermore, the given value shall be convertible to the given class without precision
lost,
+     * otherwise an {@link IllegalArgumentException} will be thrown.
+     *
+     * @param  <N>    the wrapper class.
+     * @param  value  the value to wrap.
+     * @param  type   the desired wrapper class.
+     * @return the value wrapped in an object of the given class.
+     * @throws IllegalArgumentException if the given type is not one of the primitive wrappers
for numeric types,
+     *         or if the given value can not be wrapped in an instance of the given class
without precision lost.
+     *
+     * @since 0.8
+     */
+    @SuppressWarnings("unchecked")
+    public static <N extends Number> N wrap(final long value, final Class<N>
type)
+            throws IllegalArgumentException
+    {
+        final N number;
+        switch (getEnumConstant(type)) {
+            case BYTE:        number = (N) Byte      .valueOf((byte)   value); break;
+            case SHORT:       number = (N) Short     .valueOf((short)  value); break;
+            case INTEGER:     number = (N) Integer   .valueOf((int)    value); break;
+            case LONG:        return   (N) Long      .valueOf(value);  // No need to verify.
+            case FLOAT:       number = (N) Float     .valueOf((float)  value); break;
+            case DOUBLE:      number = (N) Numerics  .valueOf((double) value); break;
+            case BIG_INTEGER: return   (N) BigInteger.valueOf(value);  // No need to verify.
+            case BIG_DECIMAL: return   (N) BigDecimal.valueOf(value);  // No need to verify.
+            default: throw unknownType(type);
+        }
+        if (number.longValue() != value) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.CanNotConvertValue_2,
value, type));
+        }
+        return number;
     }
 
     /**

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=1799204&r1=1799203&r2=1799204&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 Jun 19 12:47:43 2017
@@ -44,7 +44,7 @@ public final strictfp class VectorTest e
     @Test
     public void testSequenceOfBytes() {
         vector = Vector.createSequence(100, 2, 10);
-        assertEquals(Long.class, vector.getElementType());
+        assertEquals(Integer.class, vector.getElementType());
         assertEquals(10, vector.size());
         for (int i=0; i<vector.size(); i++) {
             assertEquals(100 + 2*i, vector.byteValue(i));



Mime
View raw message