sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1761015 - in /sis/branches/JDK8/storage/sis-netcdf/src: main/java/org/apache/sis/internal/netcdf/impl/ test/java/org/apache/sis/internal/netcdf/impl/ test/java/org/apache/sis/test/suite/
Date Fri, 16 Sep 2016 09:46:10 GMT
Author: desruisseaux
Date: Fri Sep 16 09:46:10 2016
New Revision: 1761015

URL: http://svn.apache.org/viewvc?rev=1761015&view=rev
Log:
Store attribute values as primitive wrappers instead than arrays where there is only one value.

Removed:
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
Modified:
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
    sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java?rev=1761015&r1=1761014&r2=1761015&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] Fri Sep 16 09:46:10 2016
@@ -354,6 +354,19 @@ public final class ChannelDecoder extend
         if (length < 0 || type == null) {
             throw malformedHeader();
         }
+        if (length == 1) {
+            switch (type) {
+                case BYTE:   {final byte  v =         input.readByte();          align(1);
return v;}
+                case UBYTE:  {final short v = (short) input.readUnsignedByte();  align(1);
return v;}
+                case SHORT:  {final short v =         input.readShort();         align(2);
return v;}
+                case USHORT: {final int   v =         input.readUnsignedShort(); align(2);
return v;}
+                case INT:    return input.readInt();
+                case INT64:  return input.readLong();
+                case UINT:   return input.readUnsignedInt();
+                case FLOAT:  return input.readFloat();
+                case DOUBLE: return input.readDouble();
+            }
+        }
         switch (type) {
             case CHAR: {
                 final String text = input.readString(length, encoding);
@@ -371,9 +384,7 @@ public final class ChannelDecoder extend
             case USHORT: {
                 final short[] array = new short[length];
                 input.readFully(array, 0, length);
-                if ((length & 1) != 0) {
-                    input.readShort();      // For byte alignment.
-                }
+                align(length << 1);
                 return array;
             }
             case INT:
@@ -599,7 +610,7 @@ public final class ChannelDecoder extend
         if (value instanceof String) {
             return parseNumber((String) value);
         }
-        final Number[] v = Attribute.numberValues(value);
+        final Number[] v = VariableInfo.numberValues(value);
         return (v.length != 0) ? v[0] : null;
     }
 

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java?rev=1761015&r1=1761014&r2=1761015&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
[UTF-8] Fri Sep 16 09:46:10 2016
@@ -18,6 +18,7 @@ package org.apache.sis.internal.netcdf.i
 
 import java.util.Map;
 import java.io.IOException;
+import java.lang.reflect.Array;
 import ucar.nc2.constants.CF;
 import ucar.nc2.constants.CDM;
 import ucar.nc2.constants._Coordinate;
@@ -29,6 +30,7 @@ import org.apache.sis.internal.storage.R
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.DataStoreContentException;
 import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.Numbers;
 
 
@@ -43,6 +45,11 @@ import org.apache.sis.util.Numbers;
  */
 final class VariableInfo extends Variable {
     /**
+     * The array to be returned by {@link #numberValues(Object)} when the given value is
null.
+     */
+    private static final Number[] EMPTY = new Number[0];
+
+    /**
      * The names of attributes where to look for the description to be returned by {@link
#getDescription()}.
      * We use the same attributes than the one documented in the {@link ucar.nc2.Variable#getDescription()}
javadoc.
      */
@@ -76,6 +83,20 @@ final class VariableInfo extends Variabl
 
     /**
      * The attributes associates to the variable, or an empty map if none.
+     * Values can be:
+     *
+     * <ul>
+     *   <li>a {@link String}</li>
+     *   <li>A {@link Number}</li>
+     *   <li>an array of primitive type</li>
+     * </ul>
+     *
+     * If the value is a {@code String}, then leading and trailing spaces and control characters
+     * should be trimmed by {@link String#trim()}.
+     *
+     * @see #stringValues(Object)
+     * @see #numberValues(Object)
+     * @see #booleanValue(Object)
      */
     private final Map<String,Object> attributes;
 
@@ -108,7 +129,7 @@ final class VariableInfo extends Variabl
     {
         final Object isUnsigned = attributes.get(CDM.UNSIGNED);
         if (isUnsigned != null) {
-            dataType = dataType.unsigned(Attribute.booleanValue(isUnsigned));
+            dataType = dataType.unsigned(booleanValue(isUnsigned));
         }
         this.name          = name;
         this.dimensions    = dimensions;
@@ -230,7 +251,58 @@ final class VariableInfo extends Variabl
     @Override
     public Object[] getAttributeValues(final String attributeName, final boolean numeric)
{
         final Object value = attributes.get(attributeName);
-        return numeric ? Attribute.numberValues(value) : Attribute.stringValues(value);
+        return numeric ? numberValues(value) : stringValues(value);
+    }
+
+    /**
+     * Returns the attribute values as an array of {@link String}s, or an empty array if
none.
+     * The given argument is typically a value of the {@link #attributes} map.
+     *
+     * @see #getAttributeValues(String, boolean)
+     */
+    static String[] stringValues(final Object value) {
+        if (value == null) {
+            return CharSequences.EMPTY_ARRAY;
+        }
+        if (value.getClass().isArray()) {
+            final String[] values = new String[Array.getLength(value)];
+            for (int i=0; i<values.length; i++) {
+                values[i] = Array.get(value, i).toString();
+            }
+            return values;
+        }
+        return new String[] {value.toString()};
+    }
+
+    /**
+     * Returns the attribute values as an array of {@link Number}, or an empty array if none.
+     * The given argument is typically a value of the {@link #attributes} map.
+     *
+     * @see #getAttributeValues(String, boolean)
+     */
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
+    static Number[] numberValues(final Object value) {
+        if (value != null) {
+            if (value.getClass().isArray()) {
+                final Number[] values = new Number[Array.getLength(value)];
+                for (int i=0; i<values.length; i++) {
+                    values[i] = (Number) Array.get(value, i);
+                }
+                return values;
+            }
+            if (value instanceof Number) {
+                return new Number[] {(Number) value};
+            }
+        }
+        return EMPTY;
+    }
+
+    /**
+     * Returns the attribute value as a boolean, or {@code false} if the attribute is not
a boolean.
+     * The given argument is typically a value of the {@link #attributes} map.
+     */
+    private static boolean booleanValue(final Object value) {
+        return (value instanceof String) && Boolean.valueOf((String) value);
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java?rev=1761015&r1=1761014&r2=1761015&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/VariableInfoTest.java
[UTF-8] Fri Sep 16 09:46:10 2016
@@ -21,6 +21,9 @@ import org.apache.sis.internal.netcdf.De
 import org.apache.sis.internal.netcdf.VariableTest;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.test.DependsOn;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
 
 
 /**
@@ -30,7 +33,7 @@ import org.apache.sis.test.DependsOn;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.8
  * @module
  */
 @DependsOn({ChannelDecoderTest.class, VariableTest.class})
@@ -57,4 +60,14 @@ public final strictfp class VariableInfo
     protected boolean isSupplementalFormatSupported(final String format) {
         return false;
     }
+
+    /**
+     * Tests the {@link VariableInfo#numberValues(Object)} and {@link VariableInfo#stringValues(Object)}
methods.
+     */
+    @Test
+    public void testNumberValues() {
+        final float[] a = new float[] {10, 20, 1};
+        assertArrayEquals("numberValues", new Number[] { 10f,    20f,    1f  }, VariableInfo.numberValues(a));
+        assertArrayEquals("stringValues", new String[] {"10.0", "20.0", "1.0"}, VariableInfo.stringValues(a));
+    }
 }

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java?rev=1761015&r1=1761014&r2=1761015&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] Fri Sep 16 09:46:10 2016
@@ -36,7 +36,6 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.netcdf.DecoderTest.class,
     org.apache.sis.internal.netcdf.VariableTest.class,
     org.apache.sis.internal.netcdf.GridGeometryTest.class,
-    org.apache.sis.internal.netcdf.impl.AttributeTest.class,
     org.apache.sis.internal.netcdf.impl.ChannelDecoderTest.class,
     org.apache.sis.internal.netcdf.impl.VariableInfoTest.class,
     org.apache.sis.internal.netcdf.impl.GridGeometryInfoTest.class,



Mime
View raw message