sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1653541 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/parameter/Parameters.java test/java/org/apache/sis/parameter/ParametersTest.java
Date Wed, 21 Jan 2015 14:13:36 GMT
Author: desruisseaux
Date: Wed Jan 21 14:13:36 2015
New Revision: 1653541

URL: http://svn.apache.org/r1653541
Log:
Make Parameters.copy(...) robust to multi-occurrences of a parameter value.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java?rev=1653541&r1=1653540&r2=1653541&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
[UTF-8] Wed Jan 21 14:13:36 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.parameter;
 
+import java.util.Map;
+import java.util.HashMap;
 import javax.measure.unit.Unit;
 import org.opengis.util.MemberName;
 import org.opengis.parameter.*; // We use almost all types from this package.
@@ -174,7 +176,8 @@ public final class Parameters extends St
      * Sub-groups are copied recursively.
      *
      * <p>A typical usage of this method is for transferring values from an arbitrary
implementation
-     * to some specific implementation.</p>
+     * to some specific implementation, or to a parameter group using a different but compatible
+     * {@linkplain DefaultParameterValueGroup#getDescriptor() descriptor}.</p>
      *
      * @param  values The parameters values to copy.
      * @param  destination Where to copy the values.
@@ -186,6 +189,8 @@ public final class Parameters extends St
     public static void copy(final ParameterValueGroup values, final ParameterValueGroup destination)
             throws InvalidParameterNameException, InvalidParameterValueException
     {
+        final Integer ONE = 1;
+        final Map<String,Integer> occurrences = new HashMap<>();
         for (final GeneralParameterValue value : values.values()) {
             final String name = value.getDescriptor().getName().getCode();
             if (value instanceof ParameterValueGroup) {
@@ -198,35 +203,74 @@ public final class Parameters extends St
                     final ParameterValueGroup groups = (ParameterValueGroup) descriptor.createValue();
                     copy((ParameterValueGroup) value, groups);
                     values.groups(name).add(groups);
-                    continue;
                 } else {
                     throw new InvalidParameterNameException(Errors.format(
                             Errors.Keys.UnexpectedParameter_1, name), name);
                 }
-            }
-            /*
-             * Single parameter - copy the value, with special care for value with units.
-             */
-            final ParameterValue<?> source = (ParameterValue<?>) value;
-            final ParameterValue<?> target;
-            try {
-                target = destination.parameter(name);
-            } catch (ParameterNotFoundException cause) {
-                throw new InvalidParameterNameException(Errors.format(
-                            Errors.Keys.UnexpectedParameter_1, name), cause, name);
-            }
-            final Object  v    = source.getValue();
-            final Unit<?> unit = source.getUnit();
-            if (unit == null) {
-                target.setValue(v);
-            } else if (v instanceof Number) {
-                target.setValue(((Number) v).doubleValue(), unit);
-            } else if (v instanceof double[]) {
-                target.setValue((double[]) v, unit);
             } else {
-                throw new InvalidParameterValueException(Errors.format(
-                        Errors.Keys.IllegalArgumentValue_2, name, v), name, v);
+                /*
+                 * Single parameter - copy the value, with special care for value with units
+                 * and for multi-occurrences. Not that the later is not allowed by ISO 19111
+                 * but supported by SIS implementation.
+                 */
+                final ParameterValue<?> source = (ParameterValue<?>) value;
+                final ParameterValue<?> target;
+                Integer occurrence = occurrences.get(name);
+                if (occurrence == null) {
+                    occurrence = ONE;
+                    try {
+                        target = destination.parameter(name);
+                    } catch (ParameterNotFoundException cause) {
+                        throw new InvalidParameterNameException(Errors.format(
+                                    Errors.Keys.UnexpectedParameter_1, name), cause, name);
+                    }
+                } else {
+                    target = (ParameterValue<?>) getOrCreate(destination, name, occurrence);
+                    occurrence++;
+                }
+                occurrences.put(name, occurrence);
+                final Object  v    = source.getValue();
+                final Unit<?> unit = source.getUnit();
+                if (unit == null) {
+                    target.setValue(v);
+                } else if (v instanceof Number) {
+                    target.setValue(((Number) v).doubleValue(), unit);
+                } else if (v instanceof double[]) {
+                    target.setValue((double[]) v, unit);
+                } else {
+                    throw new InvalidParameterValueException(Errors.format(
+                            Errors.Keys.IllegalArgumentValue_2, name, v), name, v);
+                }
+            }
+        }
+    }
+
+    /**
+     * Returns the <var>n</var>th occurrence of the parameter of the given name.
+     *
+     * @param  values The group from which to get or create a value
+     * @param  name   The name of the parameter to fetch. An exact match will be required.
+     * @param  n      Number of occurrences to skip before to return or create the parameter.
+     * @return The <var>n</var>th occurrence (zero-based) of the parameter of
the given name.
+     * @throws IndexOutOfBoundsException if {@code n} is greater than the current number
of
+     *         parameters of the given name.
+     */
+    private static GeneralParameterValue getOrCreate(final ParameterValueGroup values, final
String name, int n) {
+        for (final GeneralParameterValue value : values.values()) {
+            if (name.equals(value.getDescriptor().getName().getCode())) {
+                if (--n < 0) {
+                    return value;
+                }
             }
         }
+        if (n == 0) {
+            final GeneralParameterValue value = values.getDescriptor().descriptor(name).createValue();
+            values.values().add(value);
+            return value;
+        } else {
+            // We do not botter formatting a good error message for now, because
+            // this method is currently invoked only with increasing index values.
+            throw new IndexOutOfBoundsException(name);
+        }
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java?rev=1653541&r1=1653540&r2=1653541&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java
[UTF-8] Wed Jan 21 14:13:36 2015
@@ -24,6 +24,7 @@ import org.opengis.parameter.ParameterDi
 import org.opengis.parameter.ParameterValue;
 import org.opengis.parameter.ParameterValueGroup;
 import org.opengis.metadata.Identifier;
+import org.opengis.parameter.GeneralParameterValue;
 import org.opengis.util.GenericName;
 import org.opengis.util.InternationalString;
 import javax.measure.unit.Unit;
@@ -151,6 +152,8 @@ public final strictfp class ParametersTe
         assertEquals("Mandatory 1", 10, destination.parameter("Mandatory 1").intValue());
         assertEquals("Mandatory 2", 20, destination.parameter("Mandatory 2").intValue());
         assertEquals("Optional 3",  30, destination.parameter("Optional 3") .intValue());
-// TODO assertEquals("Optional 4",  40, destination.parameter("Optional 4") .intValue());
+        assertEquals("Optional 4",  40, destination.parameter("Optional 4") .intValue());
+        assertEquals("Optional 4 (second occurrence)", 50,
+                ((ParameterValue<?>) destination.values().get(4)).intValue());
     }
 }



Mime
View raw message