sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1760953 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/util/ core/sis-utility/src/test/java/org/apache/sis/internal/util/ storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ storage/sis-ne...
Date Thu, 15 Sep 2016 15:58:03 GMT
Author: desruisseaux
Date: Thu Sep 15 15:58:02 2016
New Revision: 1760953

URL: http://svn.apache.org/viewvc?rev=1760953&view=rev
Log:
A plain HashMap is sufficient for storing the NetCDF global attributes and the variable attributes.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CollectionsExtTest.java
    sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
    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/AttributeTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java?rev=1760953&r1=1760952&r2=1760953&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] Thu Sep 15 15:58:02 2016
@@ -27,9 +27,6 @@ import org.opengis.parameter.InvalidPara
 
 import static org.apache.sis.util.collection.Containers.hashMapCapacity;
 
-// Branch-dependent imports
-import java.util.function.Function;
-
 
 /**
  * Static methods working on {@link Collection} objects.
@@ -701,60 +698,46 @@ public final class CollectionsExt extend
      * <strong>before</strong> to ask for the lower-cases version of that name.</p>
      *
      * @param  <E>           the type of elements.
-     * @param  elements      the elements to store in the map, or {@code null} if none.
-     * @param  nameFunction  the function for computing a name from an element.
+     * @param  entries       the entries to store in the map, or {@code null} if none.
      * @param  namesLocale   the locale to use for creating the "all lower cases" names.
      * @return a (<cite>name</cite>, <cite>element</cite>) mapping
with lower cases entries where possible.
      * @throws InvalidParameterCardinalityException if the same name is used for more than
one element.
      */
-    public static <E> Map<String,E> toCaseInsensitiveNameMap(final Collection<?
extends E> elements,
-            final Function<E,String> nameFunction, final Locale namesLocale)
+    public static <E> Map<String,E> toCaseInsensitiveNameMap(
+            final Collection<Map.Entry<String,E>> entries, final Locale namesLocale)
     {
-        if (elements == null) {
+        if (entries == null) {
             return Collections.emptyMap();
         }
-        final Map<String,E> map = new HashMap<>(hashMapCapacity(elements.size()));
-        Set<String> excludes = null;
-        for (final E e : elements) {
-            final String name = nameFunction.apply(e);
-            E old = map.put(name, e);
-            if (old != null) {
+        final Map<String,E> map = new HashMap<>(hashMapCapacity(entries.size()));
+        final Set<String> generated = new HashSet<>();
+        for (final Map.Entry<String, ? extends E> entry : entries) {
+            final String name = entry.getKey();
+            final E value = entry.getValue();
+            E old = map.put(name, value);
+            if (old != null && !generated.remove(name)) {
                 /*
                  * If two elements use exactly the same name, this is considered an error.
Otherwise the previous
                  * mapping was using a lower case name version of its original name, so we
can discard that lower
                  * case version (the original name is still present in the map).
                  */
-                final String oldName = nameFunction.apply(old);
-                if (Objects.equals(name, oldName)) {
-                    throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.ValueAlreadyDefined_1,
name), name);
-                }
+                throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.ValueAlreadyDefined_1,
name), name);
             }
             /*
              * Add lower-cases versions of the above element names, only if that name is
not already used.
              * If a name was already used, then the original mapping will have precedence.
              */
             final String lower = name.toLowerCase(namesLocale);
-            if (!name.equals(lower) && (excludes == null || !excludes.contains(lower)))
{
-                old = map.put(lower, e);
-                if (old != null) {
-                    final String oldName = nameFunction.apply(old);
-                    if (lower.equals(oldName)) {
-                        /*
-                         * An entry already exists with a lower case name. Keep that previous
entry unchanged.
-                         */
-                        map.put(oldName, old);
-                    } else {
-                        /*
-                         * Two entries having non-lower case names got the same name after
conversion to
-                         * lower cases. Retains none of them, since doing so would introduce
an ambiguity.
-                         * Remember that we can not use that lower cases name for any other
entries.
-                         */
-                        map.remove(lower);
-                        if (excludes == null) {
-                            excludes = new HashSet<>();
-                        }
-                        excludes.add(lower);
-                    }
+            if (!name.equals(lower)) {
+                if (generated.add(lower)) {
+                    map.putIfAbsent(lower, value);
+                } else {
+                    /*
+                     * Two entries having non-lower case names got the same name after conversion
to
+                     * lower cases. Retains none of them, since doing so would introduce
an ambiguity.
+                     * Remember that we can not use that lower cases name for any other entries.
+                     */
+                    map.remove(lower);
                 }
             }
         }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CollectionsExtTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CollectionsExtTest.java?rev=1760953&r1=1760952&r2=1760953&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CollectionsExtTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CollectionsExtTest.java
[UTF-8] Thu Sep 15 15:58:02 2016
@@ -18,7 +18,9 @@ package org.apache.sis.internal.util;
 
 import java.util.List;
 import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Map;
+import java.util.AbstractMap;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Set;
@@ -29,22 +31,18 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.Locale;
 import org.apache.sis.util.collection.CodeListSet;
-import org.apache.sis.util.ObjectConverters;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.apache.sis.test.Assert.*;
 
-// Branch-dependent imports
-import java.util.function.Function;
-
 
 /**
  * Tests the {@link CollectionsExt} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.6
+ * @version 0.8
  * @module
  */
 public final strictfp class CollectionsExtTest extends TestCase {
@@ -122,23 +120,28 @@ public final strictfp class CollectionsE
     }
 
     /**
-     * Tests {@link CollectionsExt#toCaseInsensitiveNameMap(Collection, Function, Locale)}.
+     * Tests {@link CollectionsExt#toCaseInsensitiveNameMap(Collection, Locale)}.
      */
     @Test
     public void testToCaseInsensitiveNameMap() {
-        final Function<String,String> nameFunction = ObjectConverters.identity(String.class);
+        final List<Map.Entry<String,String>> elements = new ArrayList<>();
+        elements.add(new AbstractMap.SimpleEntry<>("AA", "AA"));
+        elements.add(new AbstractMap.SimpleEntry<>("Aa", "Aa"));
+        elements.add(new AbstractMap.SimpleEntry<>("BB", "BB"));
+        elements.add(new AbstractMap.SimpleEntry<>("bb", "bb"));
+        elements.add(new AbstractMap.SimpleEntry<>("CC", "CC"));
+
         final Map<String,String> expected = new HashMap<>();
         assertNull(expected.put("AA", "AA"));
-        assertNull(expected.put("Aa", "Aa")); // No mapping for "aa", because of ambiguity
between "AA" and "Aa".
+        assertNull(expected.put("Aa", "Aa"));   // No mapping for "aa", because of ambiguity
between "AA" and "Aa".
         assertNull(expected.put("BB", "BB"));
         assertNull(expected.put("bb", "bb"));
         assertNull(expected.put("CC", "CC"));
-        assertNull(expected.put("cc", "CC")); // Automatically added.
+        assertNull(expected.put("cc", "CC"));   // Automatically added.
 
-        final List<String> elements = Arrays.asList("AA", "Aa", "BB", "bb", "CC");
         for (int i=0; i<10; i++) {
             Collections.shuffle(elements);
-            assertMapEquals(expected, CollectionsExt.toCaseInsensitiveNameMap(elements, nameFunction,
Locale.ROOT));
+            assertMapEquals(expected, CollectionsExt.toCaseInsensitiveNameMap(elements, Locale.ROOT));
         }
     }
 

Modified: sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java?rev=1760953&r1=1760952&r2=1760953&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/Attribute.java
[UTF-8] Thu Sep 15 15:58:02 2016
@@ -17,15 +17,21 @@
 package org.apache.sis.internal.netcdf.impl;
 
 import java.lang.reflect.Array;
-import org.apache.sis.util.Debug;
-import org.apache.sis.util.Utilities;
-
-// Branch-dependent imports
-import java.util.function.Function;
+import org.apache.sis.util.CharSequences;
 
 
 /**
- * Attribute found in a NetCDF file.
+ * Static methods about attributes found in a NetCDF file.
+ * 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()}.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
@@ -35,49 +41,33 @@ import java.util.function.Function;
  */
 final class Attribute {
     /**
-     * The function for obtaining the name of an attribute.
-     */
-    static final Function<Attribute,String> NAME_FUNCTION = (Attribute value1) ->
value1.name;
-
-    /**
-     * The attribute name.
+     * The array to be returned by {@link #numberValues(Object)} when the given value is
null.
      */
-    final String name;
+    private static final Number[] EMPTY = new Number[0];
 
     /**
-     * The value, either as a {@link String} or as an array of primitive type.
-     * Never {@code null} and never an empty string or empty array.
-     *
-     * <p>If the value is a {@code String}, then leading and trailing spaces and control
characters
-     * have been trimmed by {@link String#trim()}.</p>
+     * Do not allow instantiation of this class.
      */
-    final Object value;
-
-    /**
-     * Creates a new attribute of the given name and value.
-     *
-     * @param  name   the attribute name (can not be null).
-     * @param  value  the value (trimmed if a {@code String}).
-     */
-    Attribute(final String name, final Object value) {
-        this.name  = name;
-        this.value = value;
+    private Attribute() {
     }
 
     /**
-     * Returns the attribute values as an array of {@link String}.
+     * Returns the attribute values as an array of {@link String}s, or an empty array if
none.
      *
      * @see VariableInfo#getAttributeValues(String, boolean)
      */
-    final String[] stringValues() {
-        if (value instanceof String) {
-            return new String[] {(String) value};
+    static String[] stringValues(final Object value) {
+        if (value == null) {
+            return CharSequences.EMPTY_ARRAY;
         }
-        final String[] values = new String[Array.getLength(value)];
-        for (int i=0; i<values.length; i++) {
-            values[i] = Array.get(value, i).toString();
+        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 values;
+        return new String[] {value.toString()};
     }
 
     /**
@@ -85,12 +75,21 @@ final class Attribute {
      *
      * @see VariableInfo#getAttributeValues(String, boolean)
      */
-    final Number[] numberValues() {
-        final Number[] values = new Number[(value instanceof String) ? 0 : Array.getLength(value)];
-        for (int i=0; i<values.length; i++) {
-            values[i] = (Number) Array.get(value, i);
+    @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 values;
+        return EMPTY;
     }
 
     /**
@@ -98,16 +97,7 @@ final class Attribute {
      *
      * @see VariableInfo#isUnsigned()
      */
-    final boolean booleanValue() {
+    static boolean booleanValue(final Object value) {
         return (value instanceof String) && Boolean.valueOf((String) value);
     }
-
-    /**
-     * A string representation of this dimension for debugging purpose only.
-     */
-    @Debug
-    @Override
-    public String toString() {
-        return name + " = " + Utilities.deepToString(value);
-    }
 }

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=1760953&r1=1760952&r2=1760953&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] Thu Sep 15 15:58:02 2016
@@ -16,24 +16,24 @@
  */
 package org.apache.sis.internal.netcdf.impl;
 
-import org.apache.sis.internal.netcdf.DataType;
 import java.util.Set;
 import java.util.Map;
+import java.util.AbstractMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedHashMap;
 import java.util.IdentityHashMap;
 import java.util.List;
 import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.Locale;
 import java.util.regex.Pattern;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.ReadableByteChannel;
-import java.lang.reflect.Array;
 import javax.measure.converter.UnitConverter;
 import javax.measure.converter.ConversionException;
-import org.opengis.parameter.InvalidParameterCardinalityException;
+import org.apache.sis.internal.netcdf.DataType;
 import org.apache.sis.internal.netcdf.Decoder;
 import org.apache.sis.internal.netcdf.Variable;
 import org.apache.sis.internal.netcdf.GridGeometry;
@@ -46,12 +46,10 @@ import org.apache.sis.util.iso.DefaultNa
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.util.logging.WarningListeners;
-import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Debug;
 import org.apache.sis.measure.Units;
 
 // Branch-dependent imports
-import java.util.function.Function;
 import java.time.DateTimeException;
 
 
@@ -176,7 +174,7 @@ public final class ChannelDecoder extend
      *
      * @see #findAttribute(String)
      */
-    private final Map<String,Attribute> attributeMap;
+    private final Map<String,Object> attributeMap;
 
     /**
      * The grid geometries, created when first needed.
@@ -231,9 +229,9 @@ public final class ChannelDecoder extend
          * Read the dimension, attribute and variable declarations. We expect exactly 3 lists,
          * where any of them can be flagged as absent by a long (64 bits) 0.
          */
-        Dimension[]    dimensions = null;
-        VariableInfo[] variables  = null;
-        Attribute[]    attributes = null;
+        Dimension[]        dimensions = null;
+        VariableInfo[]     variables  = null;
+        Map<String,Object> attributes = null;
         for (int i=0; i<3; i++) {
             final long tn = input.readLong(); // Combination of tag and nelems
             if (tn != 0) {
@@ -248,7 +246,7 @@ public final class ChannelDecoder extend
                 }
             }
         }
-        attributeMap = toMap(attributes, Attribute.NAME_FUNCTION);
+        attributeMap = attributes;
         this.variables = variables;
     }
 
@@ -449,17 +447,16 @@ public final class ChannelDecoder extend
      *
      * @param  nelems  the number of attributes to read.
      */
-    private Attribute[] readAttributes(final int nelems) throws IOException, DataStoreException
{
-        final Attribute[] attributes = new Attribute[nelems];
-        int count = 0;
-        for (int i=0; i<nelems; i++) {
+    private Map<String,Object> readAttributes(int nelems) throws IOException, DataStoreException
{
+        final List<Map.Entry<String,Object>> attributes = new ArrayList<>(nelems);
+        while (--nelems >= 0) {
             final String name = readName();
             final Object value = readValues(DataType.valueOf(input.readInt()), input.readInt());
             if (value != null) {
-                attributes[count++] = new Attribute(name, value);
+                attributes.add(new AbstractMap.SimpleEntry<>(name, value));
             }
         }
-        return ArraysExt.resize(attributes, count);
+        return CollectionsExt.toCaseInsensitiveNameMap(attributes, NAME_LOCALE);
     }
 
     /**
@@ -506,7 +503,7 @@ public final class ChannelDecoder extend
              * Following block is almost a copy-and-paste of similar block in the contructor,
              * but with less cases in the "switch" statements.
              */
-            Attribute[] attributes = null;
+            Map<String,Object> attributes = null;
             final long tn = input.readLong();
             if (tn != 0) {
                 final int tag = (int) (tn >>> Integer.SIZE);
@@ -518,36 +515,12 @@ public final class ChannelDecoder extend
                     default:        throw malformedHeader();
                 }
             }
-            variables[j] = new VariableInfo(input, name, varDims, dimensions,
-                    toMap(attributes, Attribute.NAME_FUNCTION), DataType.valueOf(input.readInt()),
input.readInt(), readOffset());
+            variables[j] = new VariableInfo(input, name, varDims, dimensions, attributes,
+                    DataType.valueOf(input.readInt()), input.readInt(), readOffset());
         }
         return variables;
     }
 
-    /**
-     * Creates a (<cite>name</cite>, <cite>element</cite>) mapping
for the given array of elements.
-     * If the name of an element is not all lower cases, then this method also adds an entry
for the
-     * lower cases version of that name in order to allow case-insensitive searches.
-     *
-     * <p>Code searching in the returned map shall ask for the original (non lower-case)
name
-     * <strong>before</strong> to ask for the lower-cases version of that name.</p>
-     *
-     * @param  <E>           the type of elements.
-     * @param  elements      the elements to store in the map, or {@code null} if none.
-     * @param  nameFunction  the function for computing a name from an element.
-     * @return a (<cite>name</cite>, <cite>element</cite>) mapping
with lower cases entries where possible.
-     * @throws DataStoreException if the same name is used for more than one element.
-     *
-     * @see #findAttribute(String)
-     */
-    private <E> Map<String,E> toMap(final E[] elements, final Function<E,String>
nameFunction) throws DataStoreException {
-        try {
-            return CollectionsExt.toCaseInsensitiveNameMap(Arrays.asList(elements), nameFunction,
NAME_LOCALE);
-        } catch (InvalidParameterCardinalityException e) {
-            throw new DataStoreContentException(errors().getString(Errors.Keys.ValueAlreadyDefined_1,
e.getParameterName()));
-        }
-    }
-
 
 
     // --------------------------------------------------------------------------------------------
@@ -587,17 +560,17 @@ public final class ChannelDecoder extend
      * the constants defined in the {@link AttributeNames} class.
      *
      * @param  name  the name of the attribute to search, or {@code null}.
-     * @return the attribute, or {@code null} if none.
+     * @return the attribute value, or {@code null} if none.
      */
-    private Attribute findAttribute(final String name) {
-        Attribute attribute = attributeMap.get(name);
-        if (attribute == null && name != null) {
+    private Object findAttribute(final String name) {
+        Object value = attributeMap.get(name);
+        if (value == null && name != null) {
             final String lower = name.toLowerCase(NAME_LOCALE);
             if (lower != name) { // Identity comparison is ok since this check is only an
optimization for a common case.
-                attribute = attributeMap.get(lower);
+                value = attributeMap.get(lower);
             }
         }
-        return attribute;
+        return value;
     }
 
     /**
@@ -609,11 +582,8 @@ public final class ChannelDecoder extend
      */
     @Override
     public String stringValue(final String name) throws IOException {
-        final Attribute attribute = findAttribute(name);
-        if (attribute != null) {
-            return attribute.value.toString();
-        }
-        return null;
+        final Object value = findAttribute(name);
+        return (value != null) ? value.toString() : null;
     }
 
     /**
@@ -625,14 +595,12 @@ public final class ChannelDecoder extend
      */
     @Override
     public Number numericValue(final String name) throws IOException {
-        final Attribute attribute = findAttribute(name);
-        if (attribute != null && attribute.value != null) {
-            if (attribute.value instanceof String) {
-                return parseNumber((String) attribute.value);
-            }
-            return (Number) Array.get(attribute.value, 0);
+        final Object value = findAttribute(name);
+        if (value instanceof String) {
+            return parseNumber((String) value);
         }
-        return null;
+        final Number[] v = Attribute.numberValues(value);
+        return (v.length != 0) ? v[0] : null;
     }
 
     /**
@@ -644,13 +612,11 @@ public final class ChannelDecoder extend
      */
     @Override
     public Date dateValue(final String name) throws IOException {
-        final Attribute attribute = findAttribute(name);
-        if (attribute != null) {
-            if (attribute.value instanceof CharSequence) try {
-                return StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse((CharSequence)
attribute.value));
-            } catch (DateTimeException | ArithmeticException e) {
-                listeners.warning(null, e);
-            }
+        final Object value = findAttribute(name);
+        if (value instanceof CharSequence) try {
+            return StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse((CharSequence)
value));
+        } catch (DateTimeException | ArithmeticException e) {
+            listeners.warning(null, e);
         }
         return 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=1760953&r1=1760952&r2=1760953&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] Thu Sep 15 15:58:02 2016
@@ -77,7 +77,7 @@ final class VariableInfo extends Variabl
     /**
      * The attributes associates to the variable, or an empty map if none.
      */
-    private final Map<String,Attribute> attributes;
+    private final Map<String,Object> attributes;
 
     /**
      * The NetCDF type of data, or {@code null} if unknown.
@@ -103,13 +103,12 @@ final class VariableInfo extends Variabl
      * @param  offset         the offset where the variable data begins in the NetCDF file.
      */
     VariableInfo(final ChannelDataInput input, final String name,
-            final Dimension[] dimensions, final Dimension[] allDimensions,
-            final Map<String,Attribute> attributes, DataType dataType, final int size,
final long offset)
-            throws DataStoreException
+            final Dimension[] dimensions, final Dimension[] allDimensions, final Map<String,Object>
attributes,
+            DataType dataType, final int size, final long offset) throws DataStoreException
     {
-        final Attribute attribute = attributes.get(CDM.UNSIGNED);
-        if (attribute != null) {
-            dataType = dataType.unsigned(attribute.booleanValue());
+        final Object isUnsigned = attributes.get(CDM.UNSIGNED);
+        if (isUnsigned != null) {
+            dataType = dataType.unsigned(Attribute.booleanValue(isUnsigned));
         }
         this.name          = name;
         this.dimensions    = dimensions;
@@ -143,9 +142,9 @@ final class VariableInfo extends Variabl
     @Override
     public String getDescription() {
         for (final String attributeName : DESCRIPTION_ATTRIBUTES) {
-            final Attribute attribute = attributes.get(attributeName);
-            if (attribute != null && attribute.value instanceof String) {
-                return (String) attribute.value;
+            final Object value = attributes.get(attributeName);
+            if (value instanceof String) {
+                return (String) value;
             }
         }
         return null;
@@ -156,11 +155,8 @@ final class VariableInfo extends Variabl
      */
     @Override
     public String getUnitsString() {
-        final Attribute attribute = attributes.get(CDM.UNITS);
-        if (attribute != null && attribute.value instanceof String) {
-            return (String) attribute.value;
-        }
-        return null;
+        final Object value = attributes.get(CDM.UNITS);
+        return (value instanceof String) ? (String) value : null;
     }
 
     /**
@@ -179,13 +175,10 @@ final class VariableInfo extends Variabl
      */
     @Override
     public boolean isCoordinateSystemAxis() {
-        String name = this.name;
-        final Attribute attribute = attributes.get(_CoordinateVariableAlias);
-        if (attribute != null && attribute.value instanceof String) {
-            name = (String) attribute.value;
-        }
+        final Object value = attributes.get(_CoordinateVariableAlias);
+        final String alias = (value instanceof String) ? (String) value : name;
         for (final Dimension dimension : allDimensions) {
-            if (name.equals(dimension.name)) {
+            if (alias.equals(dimension.name)) {
                 // This variable is a dimension of another variable.
                 return true;
             }
@@ -197,11 +190,8 @@ final class VariableInfo extends Variabl
      * Returns the value of the {@code "_CoordinateAxisType"} attribute, or {@code null}
if none.
      */
     final String getAxisType() {
-        final Attribute attribute = attributes.get(_Coordinate.AxisType);
-        if (attribute != null && attribute.value instanceof String) {
-            return (String) attribute.value;
-        }
-        return null;
+        final Object value = attributes.get(_Coordinate.AxisType);
+        return (value instanceof String) ? (String) value : null;
     }
 
     /**
@@ -239,11 +229,8 @@ final class VariableInfo extends Variabl
      */
     @Override
     public Object[] getAttributeValues(final String attributeName, final boolean numeric)
{
-        Attribute attribute = attributes.get(attributeName);
-        if (attribute != null) {
-            return numeric ? attribute.numberValues() : attribute.stringValues();
-        }
-        return new Object[0];
+        final Object value = attributes.get(attributeName);
+        return numeric ? Attribute.numberValues(value) : Attribute.stringValues(value);
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java?rev=1760953&r1=1760952&r2=1760953&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/impl/AttributeTest.java
[UTF-8] Thu Sep 15 15:58:02 2016
@@ -36,8 +36,8 @@ public final strictfp class AttributeTes
      */
     @Test
     public void testNumberValues() {
-        final Attribute a = new Attribute("aName", new float[] {10, 20, 1});
-        assertArrayEquals("numberValues", new Number[] { 10f,    20f,    1f  }, a.numberValues());
-        assertArrayEquals("stringValues", new String[] {"10.0", "20.0", "1.0"}, a.stringValues());
+        final float[] a = new float[] {10, 20, 1};
+        assertArrayEquals("numberValues", new Number[] { 10f,    20f,    1f  }, Attribute.numberValues(a));
+        assertArrayEquals("stringValues", new String[] {"10.0", "20.0", "1.0"}, Attribute.stringValues(a));
     }
 }



Mime
View raw message