sis-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1755594&view=rev
Log:
Add support for vector backed by String[] array. This is not recommended, but happen because
of the way GDAL encode some of their extensions in GeoTIFF tags.

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/Vector.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/math/VectorTest.java
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Type.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=1755594&r1=1755593&r2=1755594&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 10:45:14 2016
@@ -137,7 +137,7 @@ abstract class ArrayVector<E extends Num
     /**
      * A vector backed by an array of type {@code float[]}.
      */
-    static final class Float extends ArrayVector<java.lang.Float> {
+    static class Float extends ArrayVector<java.lang.Float> {
         /** For cross-version compatibility. */
         private static final long serialVersionUID = 5395284704294981455L;
 
@@ -150,32 +150,32 @@ abstract class ArrayVector<E extends Num
         }
 
         /** Returns the type of elements in the backing array. */
-        @Override public Class<java.lang.Float> getElementType() {
+        @Override public final Class<java.lang.Float> getElementType() {
             return java.lang.Float.class;
         }
 
         /** Returns the length of the backing array. */
-        @Override public int size() {
+        @Override public final int size() {
             return array.length;
         }
 
         /** Returns {@code true} if the value at the given index is {@code NaN}. */
-        @Override public boolean isNaN(final int index) {
+        @Override public final boolean isNaN(final int index) {
             return java.lang.Float.isNaN(array[index]);
         }
 
         /** Returns the string representation at the given index. */
-        @Override public String stringValue(final int index) {
+        @Override public final String stringValue(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];}
-        @Override public Number         get(int index) {return array[index];}
+        @Override public       double doubleValue(int index) {return array[index];}
+        @Override public final float   floatValue(int index) {return array[index];}
+        @Override public final Number         get(int index) {return array[index];}
 
         /** Sets the value at the given index. */
-        @Override public Number set(final int index, final Number value) {
+        @Override public final Number set(final int index, final Number value) {
             final float old = array[index];
             array[index] = value.floatValue();
             return old;
@@ -183,6 +183,26 @@ abstract class ArrayVector<E extends Num
     }
 
     /**
+     * A vector backed by an array of type {@code float[]} to be converted to {@code double}
in a way that minimizes
+     * the errors when represented in base 10. This implementation should be used only when
there is good reasons to
+     * believe that the {@code float} data where encoded in base 10 in the first place (for
example in an ASCII file).
+     */
+    static final class Decimal extends Float {
+        /** For cross-version compatibility. */
+        private static final long serialVersionUID = 6085386820455858377L;
+
+        /** Creates a new vector for the given array. */
+        Decimal(final float[] array) {
+            super(array);
+        }
+
+        /** Returns the value at the given index. */
+        @Override public double doubleValue(final int index) {
+            return DecimalFunctions.floatToDouble(super.floatValue(index));
+        }
+    }
+
+    /**
      * A vector backed by an array of type {@code long[]}.
      */
     static class Long extends ArrayVector<java.lang.Long> {
@@ -463,4 +483,41 @@ abstract class ArrayVector<E extends Num
             return java.lang.Integer.toString(intValue(index));
         }
     }
+
+    /**
+     * A vector backed by an array of type {@code String[]}.
+     * This is not recommended, but happen for example in GDAL extensions for GeoTIFF files.
+     */
+    static class ASCII extends ArrayVector<java.lang.Double> {
+        /** For cross-version compatibility. */
+        private static final long serialVersionUID = 2801615620517491573L;
+
+        /** The backing array. */
+        private final String[] array;
+
+        /** Creates a new vector for the given array. */
+        ASCII(final String[] array) {
+            this.array = array;
+        }
+
+        /** Returns the type of elements in the backing array. */
+        @Override public final Class<java.lang.Double> getElementType() {
+            return java.lang.Double.class;
+        }
+
+        @Override public final int     size()          {return array.length;}
+        @Override public String stringValue(int index) {return array[index];}
+        @Override public double doubleValue(int index) {return java.lang.Double .parseDouble(array[index]);}
+        @Override public float   floatValue(int index) {return java.lang.Float  .parseFloat
(array[index]);}
+        @Override public long     longValue(int index) {return java.lang.Long   .parseLong
 (array[index]);}
+        @Override public int       intValue(int index) {return java.lang.Integer.parseInt
  (array[index]);}
+        @Override public short   shortValue(int index) {return java.lang.Short  .parseShort
(array[index]);}
+        @Override public byte     byteValue(int index) {return java.lang.Byte   .parseByte
 (array[index]);}
+        @Override public final Number   get(int index) {return doubleValue(index);}
+        @Override public final Number   set(int index, final Number value) {
+            final Number old = get(index);
+            array[index] = value.toString();
+            return old;
+        }
+    }
 }

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=1755594&r1=1755593&r2=1755594&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 10:45:14 2016
@@ -87,6 +87,7 @@ public abstract class Vector extends Abs
      *
      * <ul>
      *   <li>An array of a primitive type, like {@code float[]}.</li>
+     *   <li>A {@code String[]} array (not recommended, but happen with some file formats).</li>
      *   <li>A {@code Vector}, in which case it is returned unchanged.</li>
      *   <li>The {@code null} value, in which case {@code null} is returned.</li>
      * </ul>
@@ -142,6 +143,9 @@ public abstract class Vector extends Abs
                 return new ArrayVector.Byte((byte[]) array);
             }
         }
+        if (array instanceof String[]) {
+            return new ArrayVector.ASCII((String[]) array);
+        }
         if (array == null || array instanceof Vector) {
             return (Vector) array;
         }
@@ -149,6 +153,28 @@ public abstract class Vector extends Abs
     }
 
     /**
+     * Wraps the given {@code float[]} array in a vector that preserve the string representations
in base 10.
+     * For example the 0.1 {@code float} value casted to {@code double} normally produces
0.10000000149011612
+     * because of the way IEEE 754 arithmetic represents numbers (in base 2 instead than
base 10). But the
+     * vector returned by this method will convert the 0.1 {@code float} value into the 0.1
{@code double} value.
+     * Note that despite the appearance, this is <strong>not</strong> more accurate
than the normal cast,
+     * because base 10 is not more privileged in nature than base 2.
+     *
+     * <div class="section">When to use</div>
+     * This method can be used when there is good reasons to think that the {@code float}
numbers were parsed
+     * from decimal representations, for example an ASCII file. There is usually no reason
to use this method
+     * if the values are the result of some numerical computations.
+     *
+     * @param  array  the object to wrap in a vector, or {@code null}.
+     * @return the given array wrapped in a vector, or {@code null} if the argument was {@code
null}.
+     *
+     * @see DecimalFunctions#floatToDouble(float)
+     */
+    public static Vector createForDecimal(final float[] array) {
+        return (array != null) ? new ArrayVector.Decimal(array) : null;
+    }
+
+    /**
      * Creates a sequence of numbers in a given range of values using the given increment.
      * The range of values will be {@code first} inclusive to {@code (first + increment*length)}
exclusive.
      * Note that the value given by the {@code first} argument is equivalent to a "lowest"
or "minimum" value
@@ -216,7 +242,7 @@ public abstract class Vector extends Abs
      * @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(int index) throws IndexOutOfBoundsException;
+    public abstract boolean isNaN(int index);
 
     /**
      * Returns the value at the given index as a {@code double}.
@@ -226,8 +252,9 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      */
-    public abstract double doubleValue(int index) throws IndexOutOfBoundsException;
+    public abstract double doubleValue(int index);
 
     /**
      * Returns the value at the given index as a {@code float}.
@@ -237,8 +264,9 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      */
-    public abstract float floatValue(int index) throws IndexOutOfBoundsException;
+    public abstract float floatValue(int index);
 
     /**
      * Returns the value at the given index as a {@code long}.
@@ -251,6 +279,7 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
long} type.
      */
     public long longValue(final int index) {
@@ -273,6 +302,7 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
int} type.
      */
     public int intValue(final int index) {
@@ -294,6 +324,7 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
short} type.
      */
     public short shortValue(final int index) {
@@ -315,6 +346,7 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      * @throws ArithmeticException if the value is too large for the capacity of the {@code
byte} type.
      */
     public byte byteValue(final int index) {
@@ -343,7 +375,7 @@ 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 stringValue(int index) throws IndexOutOfBoundsException;
+    public abstract String stringValue(int index);
 
     /**
      * Returns the number at the given index, or {@code null} if none.
@@ -353,9 +385,10 @@ public abstract class Vector extends Abs
      * @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 NumberFormatException if the value is stored as a {@code String} and can not
be parsed.
      */
     @Override
-    public abstract Number get(int index) throws IndexOutOfBoundsException;
+    public abstract Number get(int index);
 
     /**
      * Sets the number at the given index.
@@ -366,9 +399,10 @@ public abstract class Vector extends Abs
      * @return the value previously stored at the given index.
      * @throws IndexOutOfBoundsException if the given index is out of bounds.
      * @throws ClassCastException if the given value can not be stored in this vector.
+     * @throws NumberFormatException if the previous value was stored as a {@code String}
and can not be parsed.
      */
     @Override
-    public abstract Number set(int index, Number value) throws IndexOutOfBoundsException,
ClassCastException;
+    public abstract Number set(int index, Number value);
 
     /**
      * Returns a view which contain the values of this vector in the given index range.
@@ -382,10 +416,10 @@ public abstract class Vector extends Abs
      * @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
{
+    public final Vector subList(final int lower, final int upper) {
         return subSampling(lower, 1, upper - lower);
     }
 
@@ -409,7 +443,7 @@ public abstract class Vector extends Abs
      *         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
{
+    public Vector subSampling(final int first, final int step, final int length) {
         if (step == 1 && first == 0 && length == size()) {
             return this;
         }
@@ -453,13 +487,13 @@ public abstract class Vector extends Abs
         }
 
         /** Returns the index where to look for the value in the enclosing vector. */
-        private int toBacking(final int index) throws IndexOutOfBoundsException {
+        private int toBacking(final int index) {
             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 {
+        @Override int[] toBacking(final int[] index) {
             final int[] ni = new int[index.length];
             for (int j=0; j<ni.length; j++) {
                 ni[j] = toBacking(index[j]);
@@ -530,7 +564,7 @@ public abstract class Vector extends Abs
      * @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[] indices) throws IndexOutOfBoundsException {
+    int[] toBacking(int[] indices) {
         indices = indices.clone();
         final int length = size();
         for (int i : indices) {
@@ -551,7 +585,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 pick(int... indices) throws IndexOutOfBoundsException {
+    public Vector pick(int... indices) {
         indices = toBacking(indices);
         final int first, step;
         switch (indices.length) {
@@ -607,7 +641,7 @@ public abstract class Vector extends Abs
         }
 
         /** Returns the indexes where to look for the value in the enclosing vector. */
-        @Override int[] toBacking(final int[] i) throws IndexOutOfBoundsException {
+        @Override int[] toBacking(final int[] i) {
             final int[] ni = new int[i.length];
             for (int j=0; j<ni.length; j++) {
                 ni[j] = indices[i[j]];

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=1755594&r1=1755593&r2=1755594&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 10:45:14 2016
@@ -219,6 +219,20 @@ public final strictfp class VectorTest e
     }
 
     /**
+     * Tests a vector backed by an array of strings.
+     * This is not recommended, but happen in GDAL extensions of GeoTIFF.
+     * See {@link org.apache.sis.storage.geotiff.Type#ASCII}.
+     */
+    @Test
+    public void testStringArray() {
+        vector = Vector.create(new String[] {"100", "80", "-20"}, false);
+        assertEquals(  3, vector.size());
+        assertEquals(100, vector.intValue(0));
+        assertEquals( 80, vector.shortValue(1));
+        assertEquals(-20, vector.doubleValue(2), STRICT);
+    }
+
+    /**
      * Tests the {@link Vector#toString()} method on a vector of signed and unsigned bytes.
      */
     @Test

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Type.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Type.java?rev=1755594&r1=1755593&r2=1755594&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Type.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Type.java
[UTF-8] Tue Aug  9 10:45:14 2016
@@ -354,6 +354,10 @@ enum Type {
             ensureSingleton(lines.length);
             return Double.parseDouble(lines[0]);
         }
+
+        @Override Object readArray(final ChannelDataInput input, final int count) throws
IOException {
+            return readString(input, count, StandardCharsets.US_ASCII);
+        }
     };
 
     /**



Mime
View raw message