sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1662851 - in /sis/branches/JDK8/core: sis-referencing/src/main/java/org/apache/sis/parameter/ sis-referencing/src/test/java/org/apache/sis/parameter/ sis-utility/src/main/java/org/apache/sis/util/
Date Fri, 27 Feb 2015 23:01:11 GMT
Author: desruisseaux
Date: Fri Feb 27 23:01:11 2015
New Revision: 1662851

URL: http://svn.apache.org/r1662851
Log:
Re-target the Parameters class as the base class of DefaultParameterValueGroup, but
providing an API which is more convenient to use at least for the referencing module.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValueGroup.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/package-info.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/parameter/ParametersTest.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Static.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java?rev=1662851&r1=1662850&r2=1662851&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
[UTF-8] Fri Feb 27 23:01:11 2015
@@ -210,6 +210,7 @@ public class DefaultParameterValue<T> ex
      *         and there is no default value.
      *
      * @see #setValue(Object)
+     * @see Parameters#getValue(ParameterDescriptor)
      */
     @Override
     public T getValue() {
@@ -228,6 +229,7 @@ public class DefaultParameterValue<T> ex
      * @throws IllegalStateException if the value is not defined and there is no default
value.
      *
      * @see #setValue(boolean)
+     * @see Parameters#booleanValue(ParameterDescriptor)
      */
     @Override
     public boolean booleanValue() throws IllegalStateException {
@@ -251,6 +253,7 @@ public class DefaultParameterValue<T> ex
      *
      * @see #setValue(int)
      * @see #intValueList()
+     * @see Parameters#intValue(ParameterDescriptor)
      */
     @Override
     public int intValue() throws IllegalStateException {
@@ -277,6 +280,7 @@ public class DefaultParameterValue<T> ex
      *
      * @see #setValue(Object)
      * @see #intValue()
+     * @see Parameters#intValueList(ParameterDescriptor)
      */
     @Override
     public int[] intValueList() throws IllegalStateException {
@@ -302,6 +306,7 @@ public class DefaultParameterValue<T> ex
      * @see #getUnit()
      * @see #setValue(double)
      * @see #doubleValueList()
+     * @see Parameters#doubleValue(ParameterDescriptor)
      */
     @Override
     public double doubleValue() throws IllegalStateException {
@@ -374,6 +379,7 @@ public class DefaultParameterValue<T> ex
      * @see #getUnit()
      * @see #setValue(double,Unit)
      * @see #doubleValueList(Unit)
+     * @see Parameters#doubleValue(ParameterDescriptor)
      */
     @Override
     public double doubleValue(final Unit<?> unit) throws IllegalArgumentException,
IllegalStateException {
@@ -398,6 +404,7 @@ public class DefaultParameterValue<T> ex
      * @see #getUnit()
      * @see #setValue(double[],Unit)
      * @see #doubleValue(Unit)
+     * @see Parameters#doubleValueList(ParameterDescriptor)
      */
     @Override
     public double[] doubleValueList(final Unit<?> unit) throws IllegalArgumentException,
IllegalStateException {
@@ -419,6 +426,7 @@ public class DefaultParameterValue<T> ex
      *
      * @see #getValue()
      * @see #setValue(Object)
+     * @see Parameters#stringValue(ParameterDescriptor)
      */
     @Override
     public String stringValue() throws IllegalStateException {

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValueGroup.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValueGroup.java?rev=1662851&r1=1662850&r2=1662851&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValueGroup.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValueGroup.java
[UTF-8] Fri Feb 27 23:01:11 2015
@@ -97,15 +97,13 @@ import java.util.Objects;
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.4
- * @version 0.4
+ * @version 0.6
  * @module
  *
  * @see DefaultParameterDescriptorGroup
  * @see DefaultParameterValue
  */
-public class DefaultParameterValueGroup implements ParameterValueGroup,
-        LenientComparable, Serializable, Cloneable
-{
+public class DefaultParameterValueGroup extends Parameters implements LenientComparable,
Serializable {
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -157,6 +155,8 @@ public class DefaultParameterValueGroup
      * The list will verify those conditions and throws {@link org.opengis.parameter.InvalidParameterNameException},
      * {@link org.opengis.parameter.InvalidParameterCardinalityException} or other runtime
exceptions if a condition
      * is not meet.
+     *
+     * @return The values in this group.
      */
     @Override
     public List<GeneralParameterValue> values() {
@@ -197,13 +197,49 @@ public class DefaultParameterValueGroup
      * @param  name The name of the parameter to search for.
      * @return The parameter value for the given name.
      * @throws ParameterNotFoundException if there is no parameter value for the given name.
+     *
+     * @see #getValue(ParameterDescriptor)
      */
     @Override
     public ParameterValue<?> parameter(final String name) throws ParameterNotFoundException
{
+        ParameterValue<?> value = parameterIfExist(name);
+        if (value == null) {
+            /*
+             * No existing parameter found. Maybe the parameter is optional and not yet created.
+             * Get the descriptor of that parameter. If the descriptor is not found, or is
not
+             * a descriptor for a single parameter (not a group), or the parameter is disabled
+             * (maximum occurrence = 0), behaves as if the parameter was not found.
+             */
+            final GeneralParameterDescriptor descriptor = values.descriptor.descriptor(name);
+            if (!(descriptor instanceof ParameterDescriptor<?>) || descriptor.getMaximumOccurs()
== 0) {
+                throw new ParameterNotFoundException(Errors.format(Errors.Keys.ParameterNotFound_2,
+                        values.descriptor.getName(), name), name);
+            }
+            /*
+             * Create the optional parameter and add it to our internal list. Note that this
is
+             * not the only place were a ParameterValue may be created,  so do not extract
just
+             * this call to 'createValue()' in a user-overrideable method.
+             */
+            value = ((ParameterDescriptor<?>) descriptor).createValue();
+            values.addUnchecked(value);
+        }
+        return value;
+    }
+
+    /**
+     * Returns the value in this group for the specified name if it exists, or {@code null}
if none.
+     * This method does not create any new {@code ParameterValue} instance.
+     */
+    @Override
+    final ParameterValue<?> parameterIfExist(final String name) throws ParameterNotFoundException
{
         ArgumentChecks.ensureNonNull("name", name);
         final ParameterValueList values = this.values; // Protect against accidental changes.
-
-        // Quick search for an exact match.
+        /*
+         * Quick search for an exact match. By invoking 'descriptor(i)' instead of 'get(i)',
+         * we avoid the creation of mandatory ParameterValue which was deferred. If we find
+         * a matching name, the ParameterValue will be lazily created (if not already done)
+         * by the call to 'get(i)'.
+         */
         final int size = values.size();
         for (int i=0; i<size; i++) {
             final GeneralParameterDescriptor descriptor = values.descriptor(i);
@@ -213,8 +249,11 @@ public class DefaultParameterValueGroup
                 }
             }
         }
-        // More costly search before to give up.
-        int fallback = -1, ambiguity = -1;
+        /*
+         * More costly search, including aliases, before to give up.
+         */
+        int fallback  = -1;
+        int ambiguity = -1;
         for (int i=0; i<size; i++) {
             final GeneralParameterDescriptor descriptor = values.descriptor(i);
             if (descriptor instanceof ParameterDescriptor<?>) {
@@ -229,23 +268,12 @@ public class DefaultParameterValueGroup
         }
         if (fallback >= 0) {
             if (ambiguity < 0) {
-                return (ParameterValue<?>) values.get(fallback);
+                return (ParameterValue<?>) values.get(fallback);   // May lazily create
a ParameterValue.
             }
             throw new ParameterNotFoundException(Errors.format(Errors.Keys.AmbiguousName_3,
                     values.descriptor(fallback).getName(), values.descriptor(ambiguity).getName(),
name), name);
         }
-        /*
-         * No existing parameter found. The parameter may be optional. Check if a descriptor
exists.
-         * If such a descriptor is found, create the parameter, add it to the values list
and returns it.
-         */
-        final GeneralParameterDescriptor descriptor = values.descriptor.descriptor(name);
-        if (descriptor instanceof ParameterDescriptor<?> && descriptor.getMaximumOccurs()
!= 0) {
-            final ParameterValue<?> value = ((ParameterDescriptor<?>) descriptor).createValue();
-            values.addUnchecked(value);
-            return value;
-        }
-        throw new ParameterNotFoundException(Errors.format(Errors.Keys.ParameterNotFound_2,
-                values.descriptor.getName(), name), name);
+        return null;
     }
 
     /**
@@ -385,16 +413,12 @@ public class DefaultParameterValueGroup
      * Included parameter values and subgroups are cloned recursively.
      *
      * @return A copy of this group of parameter values.
+     *
+     * @see #copy(ParameterValueGroup, ParameterValueGroup)
      */
     @Override
-    @SuppressWarnings("unchecked")
     public DefaultParameterValueGroup clone() {
-        final DefaultParameterValueGroup copy;
-        try {
-            copy = (DefaultParameterValueGroup) super.clone();
-        } catch (CloneNotSupportedException e) {
-            throw new AssertionError(e);
-        }
+        final DefaultParameterValueGroup copy = (DefaultParameterValueGroup) super.clone();
         copy.values = new ParameterValueList(copy.values);
         return copy;
     }

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=1662851&r1=1662850&r2=1662851&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] Fri Feb 27 23:01:11 2015
@@ -18,30 +18,115 @@ package org.apache.sis.parameter;
 
 import java.util.Map;
 import java.util.HashMap;
+import java.util.List;
+import java.io.Serializable;
 import javax.measure.unit.Unit;
 import org.opengis.util.MemberName;
+import org.opengis.metadata.Identifier;
+import org.opengis.metadata.citation.Citation;
 import org.opengis.parameter.*; // We use almost all types from this package.
 import org.apache.sis.internal.jaxb.metadata.replace.ServiceParameter;
 import org.apache.sis.measure.Range;
 import org.apache.sis.measure.NumberRange;
 import org.apache.sis.measure.MeasurementRange;
+import org.apache.sis.referencing.IdentifiedObjects;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.ObjectConverters;
 import org.apache.sis.util.resources.Errors;
-import org.apache.sis.util.Static;
+
+import static org.apache.sis.referencing.IdentifiedObjects.isHeuristicMatchForName;
 
 
 /**
- * Static methods working on parameters and their descriptors.
+ * Convenience methods for fetching parameter values despite the variations in parameter
names, value types and units.
+ * See {@link DefaultParameterValueGroup} javadoc for a description of the standard way to
get and set a particular
+ * parameter in a group. The remaining of this javadoc is specific to Apache SIS.
+ *
+ * {@section Convenience static methods}
+ * This class provides the following convenience static methods:
+ * <ul>
+ *   <li>{@link #cast(ParameterValue, Class) cast(…, Class)} for type safety with
parameterized types.</li>
+ *   <li>{@link #getMemberName(ParameterDescriptor)} for inter-operability between
ISO 19111 and ISO 19115.</li>
+ *   <li>{@link #getValueDomain(ParameterDescriptor)} for information purpose.</li>
+ *   <li>{@link #copy(ParameterValueGroup, ParameterValueGroup)} for copying values
into an existing instance.</li>
+ * </ul>
+ *
+ * {@section Finding a parameter despite different names}
+ * The same parameter may be known under different names. For example the
+ * {@linkplain org.apache.sis.referencing.datum.DefaultEllipsoid#getSemiMajorAxis() length
of the semi-major axis of the
+ * ellipsoid} is commonly known as {@code "semi_major"}. But that parameter can also be named
{@code "semi_major_axis"}
+ * or {@code "earth_radius"} in NetCDF files, or simply {@code "a"} in the Proj.4 library.
+ *
+ * <p>The common way to get a parameter is to invoke the {@link #parameter(String)}
method.
+ * But we do not always know in advance which of the above-cited names is recognized by an
arbitrary
+ * {@code ParameterValueGroup} implementation.</p>
+ *
+ * <div class="note"><b>Note:</b>
+ * This uncertainty is mitigated with the Apache SIS implementation since it compares the
given {@code String}
+ * argument against all parameter's {@linkplain DefaultParameterDescriptor#getAlias() aliases}
in addition to
+ * the {@linkplain DefaultParameterDescriptor#getName() name}. However we do not have the
guarantee that all
+ * implementations do that.</div>
+ *
+ * This {@code Parameters} class provides an alternative way to search for parameters,
+ * which use a given {@link ParameterDescriptor} argument instead than a {@code String}.
+ * {@code Parameters} uses the additional information provided by the descriptor for
+ * choosing a {@code String} argument that {@link #parameter(String)} is more likely to know.
+ * See for example {@link #getValue(ParameterDescriptor)}.
+ *
+ * {@section Note for subclass implementors}
+ * All methods in this class get their information from the {@link ParameterValueGroup} methods.
+ * In addition, each method in this class is isolated from all others: overriding one of
those
+ * methods have no impact on other methods.
+ *
+ * <div class="note"><b>Note on this class name:</b>
+ * Despite implementing the {@link ParameterValueGroup} interface, this class is not named
+ * {@code AbstractParameterValueGroup} because it does not implement any method from the
interface.
+ * Extending this class or extending {@link Object} make almost no difference for implementors.
+ * This {@code Parameters} class intend is rather to extend the API with methods that are
convenient
+ * for the way Apache SIS uses parameters.
+ * Consequently this class is intended for users rather than implementors.</div>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
- * @version 0.5
+ * @version 0.6
  * @module
  */
-public final class Parameters extends Static {
+public abstract class Parameters implements ParameterValueGroup, Cloneable {
     /**
-     * Do not allow instantiation of this class.
+     * For subclass constructors only.
      */
-    private Parameters() {
+    protected Parameters() {
+    }
+
+    /**
+     * Returns the given parameter value group as a {@code Parameters} instance.
+     * If the given parameters is already an instance of {@code Parameters}, then it is returned
as-is.
+     * Otherwise this method returns a wrapper which delegate all method invocations to the
given instance.
+     *
+     * @param  parameters The object to cast or wrap, or {@code null}.
+     * @return The given argument as an instance of {@code Parameters} (may be the same reference),
+     *         or {@code null} if the given argument was null.
+     */
+    public static Parameters castOrWrap(final ParameterValueGroup parameters) {
+        if (parameters == null || parameters instanceof Parameters) {
+            return (Parameters) parameters;
+        } else {
+            return new Wrapper(parameters);
+        }
+    }
+
+    /** Wrappers used as a fallback by {@link Parameters#castOrWrap(ParameterValueGroup)}.
*/
+    private static final class Wrapper extends Parameters implements Serializable {
+        private static final long serialVersionUID = -5491790565456920471L;
+        private final ParameterValueGroup delegate;
+        Wrapper(final ParameterValueGroup delegate) {this.delegate = delegate;}
+
+        @Override public ParameterDescriptorGroup    getDescriptor()        {return delegate.getDescriptor();}
+        @Override public List<GeneralParameterValue> values()               {return
delegate.values();}
+        @Override public ParameterValue<?>           parameter(String name) {return
delegate.parameter(name);}
+        @Override public List<ParameterValueGroup>   groups   (String name) {return
delegate.groups(name);}
+        @Override public ParameterValueGroup         addGroup (String name) {return delegate.addGroup(name);}
+        @Override public Parameters                  clone()                {return new Wrapper(delegate.clone());}
     }
 
     /**
@@ -114,6 +199,301 @@ public final class Parameters extends St
     }
 
     /**
+     * Returns the name or alias of the given parameter for the authority code space expected
by this group.
+     * If no name or alias for this group's authority can be found, then the primary name
will be returned.
+     *
+     * @param  source The parameter for which the name is wanted.
+     * @return The name of the given parameter.
+     */
+    private String getName(final GeneralParameterDescriptor source) {
+        final ParameterDescriptorGroup descriptor = getDescriptor();
+        if (descriptor != null) {   // Paranoiac check (should never be null)
+            final Identifier group = descriptor.getName();
+            if (group != null) {    // Paranoiac check (should never be null)
+                final Citation authority = group.getAuthority();
+                if (authority != null) {
+                    final String name = IdentifiedObjects.getName(source, group.getAuthority());
+                    if (name != null) {
+                        return name;
+                    }
+                }
+            }
+        }
+        return IdentifiedObjects.getName(source, null);
+    }
+
+    /**
+     * Returns the parameter of the given name, or {@code null} if it does not exist.
+     * The default implementation iterates over the {@link #values()} and compares the descriptor
names.
+     * The {@link DefaultParameterValueGroup} subclass will override this method with a more
efficient
+     * implementation which avoid creating some deferred parameters.
+     */
+    ParameterValue<?> parameterIfExist(final String name) throws ParameterNotFoundException
{
+        for (final GeneralParameterValue value : values()) {
+            if (value instanceof ParameterValue<?>) {
+                if (isHeuristicMatchForName(value.getDescriptor(), name)) {
+                    return (ParameterValue<?>) value;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns the parameter value for the specified operation parameter.
+     * This method tries to do the same work than {@link #parameter(String)} but without
+     * instantiating optional parameters if that parameter was not already instantiated.
+     *
+     * @param  parameter The parameter to search.
+     * @return The requested parameter value, or {@code null} if none.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     */
+    private ParameterValue<?> getParameter(final ParameterDescriptor<?> parameter)
throws ParameterNotFoundException {
+        ArgumentChecks.ensureNonNull("parameter", parameter);
+        /*
+         * Search for an identifier matching this group's authority. For example if this
ParameterValueGroup
+         * was created from an EPSG database, then we want to use the EPSG names instead
than the OGC names.
+         */
+        final String name = getName(parameter);
+        if (parameter.getMinimumOccurs() == 0) {
+            /*
+             * The parameter is optional. We do not want to invoke 'parameter(name)' because
we do not want
+             * to create a new parameter if the user did not supplied one.  We search the
parameter ourself
+             * (so we don't create any) and return null if we do not find any.
+             *
+             * If we find a parameter,  we can return it directly only if this object is
an instance of a known
+             * implementation (currently DefaultParameterValueGroup only), otherwise we do
not know if the user
+             * overrode the 'parameter' method  (we do not use Class.getMethod(...).getDeclaringClass()
because
+             * it is presumed not worth the cost).  In case of doubt, we delegate to 'parameter(name)'.
+             */
+            final ParameterValue<?> value = parameterIfExist(name);
+            if (value == null || getClass() == DefaultParameterValueGroup.class) {
+                return value;
+            }
+        }
+        return parameter(name);
+    }
+
+    /**
+     * Returns the value of the parameter identified by the given descriptor.
+     * This method uses the following information from the given {@code parameter} descriptor:
+     *
+     * <ul>
+     *   <li>The most appropriate {@linkplain DefaultParameterDescriptor#getName()
name} or
+     *       {@linkplain DefaultParameterDescriptor#getAlias() alias} to use for searching
+     *       in this {@code ParameterValueGroup};</li>
+     *   <li>The {@linkplain DefaultParameterDescriptor#getDefaultValue() default value}
+     *       to return if there is no value associated to the above-cited name or alias;</li>
+     *   <li>The {@linkplain DefaultParameterDescriptor#getUnit() unit of measurement}
+     *       (if any) of numerical value to return;</li>
+     *   <li>The {@linkplain DefaultParameterDescriptor#getValueClass() type} of value
to return.</li>
+     * </ul>
+     *
+     * This method can be useful when the {@code ParameterDescriptor} are known in advance,
for example in the
+     * implementation of some {@linkplain org.apache.sis.referencing.operation.DefaultOperationMethod
coordinate
+     * operation method}. If the caller has no such {@code ParameterDescriptor} at hand,
then the
+     * {@link DefaultParameterValueGroup#parameter(String) parameter(String)} method is probably
more convenient.
+     *
+     * @param  <T> The type of the parameter value.
+     * @param  parameter The name or alias of the parameter to look for, together with the
desired type and unit of value.
+     * @return The requested parameter value if it exists, or the {@linkplain DefaultParameterDescriptor#getDefaultValue()
+     *         default value} otherwise (which may be {@code null}).
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     *
+     * @see DefaultParameterValueGroup#parameter(String)
+     * @see DefaultParameterValue#getValue()
+     *
+     * @since 0.6
+     */
+    public <T> T getValue(final ParameterDescriptor<T> parameter) throws ParameterNotFoundException
{
+        final ParameterValue<?> p = getParameter(parameter);
+        if (p != null) {
+            final Object value;
+            final Class<T> type = parameter.getValueClass();
+            final Unit<?>  unit = parameter.getUnit();
+            if (unit == null) {
+                value = p.getValue();
+            } else if (type.isArray()) {
+                value = p.doubleValueList(unit);
+            } else {
+                value = p.doubleValue(unit);
+            }
+            if (value != null) {
+                return ObjectConverters.convert(value, type);
+            }
+        }
+        return parameter.getDefaultValue();     // Returning null is allowed here.
+    }
+
+    /**
+     * Returns the default value of the given descriptor, or throws an exception if the
+     * descriptor does not define a default value. This check should be kept consistent
+     * with the {@link DefaultParameterValue#incompatibleValue(Object)} check.
+     */
+    private static <T> T defaultValue(final ParameterDescriptor<T> parameter)
throws IllegalStateException {
+        final T value = parameter.getDefaultValue();
+        if (value != null) {
+            return value;
+        } else {
+            throw new IllegalStateException(Errors.format(
+                    Errors.Keys.MissingValueForParameter_1, Verifier.getName(parameter)));
+        }
+    }
+
+    /**
+     * Returns the boolean value of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this
+     * method uses the given {@code parameter} argument.
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter value if it exists, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#booleanValue()
+     *
+     * @since 0.6
+     */
+    public boolean booleanValue(final ParameterDescriptor<Boolean> parameter) throws
ParameterNotFoundException {
+        final ParameterValue<?> value = getParameter(parameter);
+        return (value != null) ? value.booleanValue() : defaultValue(parameter);
+    }
+
+    /**
+     * Returns the integer value of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this
+     * method uses the given {@code parameter} argument.
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter value if it exists, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#intValue()
+     *
+     * @since 0.6
+     */
+    public int intValue(final ParameterDescriptor<? extends Number> parameter) throws
ParameterNotFoundException {
+        final ParameterValue<?> value = getParameter(parameter);
+        return (value != null) ? value.intValue() : defaultValue(parameter).intValue();
+    }
+
+    /**
+     * Returns the integer values of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this
+     * method uses the given {@code parameter} argument.
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter values if they exist, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#intValueList()
+     *
+     * @since 0.6
+     */
+    public int[] intValueList(final ParameterDescriptor<int[]> parameter) throws ParameterNotFoundException
{
+        final ParameterValue<?> value = getParameter(parameter);
+        return (value != null) ? value.intValueList() : defaultValue(parameter);
+    }
+
+    /**
+     * Returns the floating point value of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this method
+     * uses the given {@code parameter} argument.
+     *
+     * <p>If the given descriptor supplies a {@linkplain DefaultParameterDescriptor#getUnit()
+     * unit of measurement}, then the returned value will be converted into that unit.</p>
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter value if it exists, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#doubleValue(Unit)
+     *
+     * @since 0.6
+     */
+    public double doubleValue(final ParameterDescriptor<? extends Number> parameter)
throws ParameterNotFoundException {
+        final ParameterValue<?> value = getParameter(parameter);
+        if (value != null) {
+            final Unit<?> unit = parameter.getUnit();
+            return (unit != null) ? value.doubleValue(unit) : value.doubleValue();
+        } else {
+            return defaultValue(parameter).doubleValue();
+        }
+    }
+
+    /**
+     * Returns the floating point values of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this method
+     * uses the given {@code parameter} argument.
+     *
+     * <p>If the given descriptor supplies a {@linkplain DefaultParameterDescriptor#getUnit()
+     * unit of measurement}, then the returned values will be converted into that unit.</p>
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter values if they exists, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#doubleValueList(Unit)
+     *
+     * @since 0.6
+     */
+    public double[] doubleValueList(final ParameterDescriptor<double[]> parameter)
throws ParameterNotFoundException {
+        final ParameterValue<?> value = getParameter(parameter);
+        if (value != null) {
+            final Unit<?> unit = parameter.getUnit();
+            return (unit != null) ? value.doubleValueList(unit) : value.doubleValueList();
+        } else {
+            return defaultValue(parameter);
+        }
+    }
+
+    /**
+     * Returns the string value of the parameter identified by the given descriptor.
+     * See {@link #getValue(ParameterDescriptor)} for more information about how this
+     * method uses the given {@code parameter} argument.
+     *
+     * @param  parameter The name or alias of the parameter to look for.
+     * @return The requested parameter value if it exists, or the <strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is no default
value.
+     *
+     * @see DefaultParameterValue#stringValue()
+     *
+     * @since 0.6
+     */
+    public String stringValue(final ParameterDescriptor<? extends CharSequence> parameter)
throws ParameterNotFoundException {
+        final ParameterValue<?> value = getParameter(parameter);
+        return (value != null) ? value.stringValue() : defaultValue(parameter).toString();
+    }
+
+    /**
+     * Returns the parameter identified by the given descriptor.
+     * If the identified parameter is optional and not yet created, then it will be created
now.
+     *
+     * @param  <T> The type of the parameter value.
+     * @param  parameter The parameter to look for.
+     * @return The requested parameter instance.
+     * @throws ParameterNotFoundException if the given {@code parameter} name or alias is
not legal for this group.
+     *
+     * @see DefaultParameterValueGroup#parameter(String)
+     *
+     * @since 0.6
+     */
+    public <T> ParameterValue<T> getOrCreate(final ParameterDescriptor<T>
parameter) throws ParameterNotFoundException {
+        return cast(parameter(getName(parameter)), parameter.getValueClass());
+    }
+
+    /**
      * Casts the given parameter descriptor to the given type.
      * An exception is thrown immediately if the parameter does not have the expected
      * {@linkplain DefaultParameterDescriptor#getValueClass() value class}.
@@ -184,6 +564,8 @@ public final class Parameters extends St
      * @throws InvalidParameterNameException if a {@code source} parameter name is unknown
to the {@code destination}.
      * @throws InvalidParameterValueException if the value of a {@code source} parameter
is invalid for the {@code destination}.
      *
+     * @see #clone()
+     *
      * @since 0.5
      */
     public static void copy(final ParameterValueGroup values, final ParameterValueGroup destination)
@@ -276,4 +658,21 @@ public final class Parameters extends St
             throw new IndexOutOfBoundsException(name);
         }
     }
+
+    /**
+     * Returns a deep copy of this group of parameter values.
+     * Included parameter values and subgroups are cloned recursively.
+     *
+     * @return A copy of this group of parameter values.
+     *
+     * @see #copy(ParameterValueGroup, ParameterValueGroup)
+     */
+    @Override
+    public Parameters clone() {
+        try {
+            return (Parameters) super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new AssertionError(e);   // Should never happen since we are Cloneable
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/package-info.java?rev=1662851&r1=1662850&r2=1662851&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/package-info.java
[UTF-8] Fri Feb 27 23:01:11 2015
@@ -27,8 +27,8 @@
  *     do not contain the actual parameter value.</li>
  *   <li><b>Parameter values</b> are (<var>descriptor</var>,
<var>value</var>) tuples, together with convenience methods
  *     for performing unit conversions and getting the values as instances of some commonly
used types.</li>
- *   <li>Builders, formatters and static methods aim to simplify the creation of {@code
ParameterDescriptor}s,
- *     the search for parameter values and visualizing them in a tabular format.</li>
+ *   <li><b>Builders</b>, <b>formatters</b> and search methods
aim to simplify the creation of
+ *     {@code ParameterDescriptor}s, the search for parameter values and visualizing them
in a tabular format.</li>
  * </ul>
  *
  * <p>Parameters are organized in <cite>groups</cite>.
@@ -84,8 +84,8 @@
  * if the given value is not assignable to the expected class or is not inside the value
domain.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @since   0.5
- * @version 0.4
+ * @since   0.4
+ * @version 0.6
  * @module
  */
 package org.apache.sis.parameter;

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=1662851&r1=1662850&r2=1662851&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] Fri Feb 27 23:01:11 2015
@@ -16,8 +16,9 @@
  */
 package org.apache.sis.parameter;
 
-import java.util.Collection;
 import java.util.Set;
+import java.util.Collection;
+import java.util.Collections;
 import javax.measure.unit.SI;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.parameter.ParameterDirection;
@@ -42,12 +43,14 @@ import static org.junit.Assert.*;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
- * @version 0.5
+ * @version 0.6
  * @module
  */
 @DependsOn({
     DefaultParameterDescriptorTest.class,
-    DefaultParameterValueTest.class
+    DefaultParameterDescriptorGroupTest.class,
+    DefaultParameterValueTest.class,
+    DefaultParameterValueGroupTest.class
 })
 public final strictfp class ParametersTest extends TestCase {
     /**
@@ -155,4 +158,38 @@ public final strictfp class ParametersTe
         assertEquals("Optional 4 (second occurrence)", 50,
                 ((ParameterValue<?>) destination.values().get(4)).intValue());
     }
+
+    /**
+     * Tests {@link Parameters#getValue(ParameterDescriptor)} and {@link Parameters#intValue(ParameterDescriptor)}.
+     *
+     * @since 0.6
+     */
+    @Test
+    public void testGetIntValue() {
+        final ParameterDescriptor<Integer> descriptor = DefaultParameterDescriptorTest.create("My
param", 5, 15, 10);
+        final ParameterDescriptor<Integer> incomplete = DefaultParameterDescriptorTest.createSimpleOptional("My
param", Integer.class);
+        final Parameters group = Parameters.castOrWrap(new DefaultParameterDescriptorGroup(Collections.singletonMap(
+                DefaultParameterDescriptorGroup.NAME_KEY, "My group"), 1, 1, incomplete).createValue());
+        /*
+         * Test when the ParameterValueGroup is empty. We test both with the "incomplete"
descriptor,
+         * which contain no default value, and with the complete one, which provide a default
value.
+         */
+        assertNull("No value and no default value.", group.getValue(incomplete));
+        assertEquals("No value, should fallback on default.", Integer.valueOf(10), group.getValue(descriptor));
+        try {
+            group.intValue(incomplete);
+            fail("Can not return when there is no value.");
+        } catch (IllegalStateException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("My param"));
+        }
+        /*
+         * Define a value and test again.
+         */
+        group.parameter("My param").setValue(12);
+        assertEquals(Integer.valueOf(12), group.getValue(incomplete));
+        assertEquals(Integer.valueOf(12), group.getValue(descriptor));
+        assertEquals(12, group.intValue(incomplete));
+        assertEquals(12, group.intValue(descriptor));
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Static.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Static.java?rev=1662851&r1=1662850&r2=1662851&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Static.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Static.java [UTF-8]
Fri Feb 27 23:01:11 2015
@@ -71,7 +71,8 @@ package org.apache.sis.util;
  *     <td>Parses axis names and creates transforms between {@link org.opengis.referencing.cs.CoordinateSystem}
  *         instances.</td></tr>
  * <tr><td>{@link org.apache.sis.parameter.Parameters}</td>
- *     <td>Creates, searches or modifies {@link org.opengis.parameter.ParameterValue}
instances.</td></tr>
+ *     <td>Creates, searches or modifies {@link org.opengis.parameter.ParameterValue}
instances
+ *         in a group of parameters.</td></tr>
  *
  * <tr><th colspan="2" class="hsep">Input / Output (including CRS, XML, images)</th></tr>
  * <tr><td>{@link org.apache.sis.io.IO}</td>
@@ -97,7 +98,7 @@ package org.apache.sis.util;
  *     <td>Create {@link ObjectConverter} instances, or collection views using object
converters.</td></tr>
  * </table>
  *
- * @author Martin Desruisseaux (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
  * @version 0.3
  * @module



Mime
View raw message