sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1565621 - in /sis/branches/JDK7/core: sis-referencing/src/main/java/org/apache/sis/internal/referencing/ sis-referencing/src/main/java/org/apache/sis/parameter/ sis-utility/src/main/java/org/apache/sis/util/ sis-utility/src/main/java/org/a...
Date Fri, 07 Feb 2014 11:43:16 GMT
Author: desruisseaux
Date: Fri Feb  7 11:43:15 2014
New Revision: 1565621

URL: http://svn.apache.org/r1565621
Log:
DefaultParameterValue cleaning.

Added:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
  (with props)
Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WKTUtilities.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/AbstractParameterValue.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Static.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WKTUtilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WKTUtilities.java?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WKTUtilities.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WKTUtilities.java
[UTF-8] Fri Feb  7 11:43:15 2014
@@ -21,6 +21,7 @@ import org.opengis.parameter.ParameterVa
 import org.opengis.parameter.GeneralParameterValue;
 import org.opengis.referencing.IdentifiedObject;
 import org.apache.sis.referencing.IdentifiedObjects;
+import org.apache.sis.parameter.DefaultParameterValue;
 import org.apache.sis.io.wkt.ElementKind;
 import org.apache.sis.io.wkt.FormattableObject;
 import org.apache.sis.io.wkt.Formatter;
@@ -69,7 +70,7 @@ public final class WKTUtilities extends 
      * @param parameter The parameter to append to the WKT, or {@code null} if none.
      * @param formatter The formatter where to append the parameter.
      */
-    public static void append(final GeneralParameterValue parameter, final Formatter formatter)
{
+    public static void append(GeneralParameterValue parameter, final Formatter formatter)
{
         if (parameter instanceof ParameterValueGroup) {
             for (final GeneralParameterValue param : ((ParameterValueGroup) parameter).values())
{
                 append(param, formatter);
@@ -77,7 +78,7 @@ public final class WKTUtilities extends 
         }
         if (parameter instanceof ParameterValue<?>) {
             if (!(parameter instanceof FormattableObject)) {
-                // TODO - wrap in a SIS implementation.
+                parameter = new DefaultParameterValue<>((ParameterValue<?>) parameter);
             }
             formatter.append((FormattableObject) parameter);
         }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/AbstractParameterValue.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/AbstractParameterValue.java?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/AbstractParameterValue.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/AbstractParameterValue.java
[UTF-8] Fri Feb  7 11:43:15 2014
@@ -38,7 +38,7 @@ import static org.apache.sis.util.Argume
  * The base class of single parameter value or group of parameter values.
  *
  * @author  Martin Desruisseaux (IRD)
- * @since   0.4 (derived from Geotk-2.0)
+ * @since   0.4 (derived from geotk-2.0)
  * @version 0.4
  * @module
  */
@@ -67,47 +67,22 @@ public class AbstractParameterValue exte
 
     /**
      * Creates a new instance initialized with the values from the specified parameter object.
-     * This is a <cite>shallow</cite> copy constructor, since the properties
contained in the
-     * given object are not recursively copied.
+     * This is a <cite>shallow</cite> copy constructor, since the values contained
in the given
+     * object are not cloned.
      *
-     * @param object The parameter to copy values from, or {@code null} if none.
+     * @param parameter The parameter to copy values from, or {@code null} if none.
      *
      * @see #castOrCopy(GeneralParameterValue)
      */
-    protected AbstractParameterValue(final GeneralParameterValue object) {
-        ensureNonNull("object", object);
-        descriptor = object.getDescriptor();
+    protected AbstractParameterValue(final GeneralParameterValue parameter) {
+        ensureNonNull("parameter", parameter);
+        descriptor = parameter.getDescriptor();
         if (descriptor == null) {
             throw new IllegalArgumentException(Errors.format(Errors.Keys.MissingValueForProperty_1,
"descriptor"));
         }
     }
 
     /**
-     * Returns a SIS parameter implementation with the values of the given arbitrary implementation.
-     * This method performs the first applicable actions in the following choices:
-     *
-     * <ul>
-     *   <li>If the given object is {@code null}, then this method returns {@code null}.</li>
-     *   <li>Otherwise if the given object is already an instance of
-     *       {@code AbstractParameterValue}, then it is returned unchanged.</li>
-     *   <li>Otherwise a new {@code AbstractParameterValue} instance is created using
the
-     *       {@linkplain #AbstractParameterValue(GeneralParameterValue) copy constructor}
-     *       and returned. Note that this is a <cite>shallow</cite> copy operation,
since
-     *       the other properties contained in the given object are not recursively copied.</li>
-     * </ul>
-     *
-     * @param  object The object to get as a SIS implementation, or {@code null} if none.
-     * @return A SIS implementation containing the values of the given object (may be the
-     *         given object itself), or {@code null} if the argument was null.
-     */
-    public static AbstractParameterValue castOrCopy(final GeneralParameterValue object) {
-        if (object == null || object instanceof AbstractParameterValue) {
-            return (AbstractParameterValue) object;
-        }
-        return new AbstractParameterValue(object);
-    }
-
-    /**
      * Returns the abstract definition of this parameter or group of parameters.
      *
      * @return The abstract definition of this parameter or group of parameters.
@@ -148,15 +123,9 @@ public class AbstractParameterValue exte
             return null;
         }
         final String error;
-        /*
-         * Note: the implementation below is similar (except for different error message)
to the
-         * one in Parameters.isValidValue(ParameterDescriptor, Object). If one implementation
is
-         * modified, the other should be updated accordingly. The main difference is that
null
-         * values are replaced by the default value instead than being a conformance error.
-         */
         final Class<T> type = descriptor.getValueClass();
         if (!type.isInstance(value)) {
-            error = Errors.format(Errors.Keys.IllegalParameterClass_3, getName(descriptor),
type, value.getClass());
+            error = Errors.format(Errors.Keys.IllegalParameterValueClass_3, getName(descriptor),
type, value.getClass());
         } else {
             /*
              * Before to verify if the given value is inside the bounds, we need to convert
the value
@@ -195,10 +164,10 @@ public class AbstractParameterValue exte
             if ((minimum != null && minimum.compareTo(converted) > 0) ||
                 (maximum != null && maximum.compareTo(converted) < 0))
             {
-                error = Errors.format(Errors.Keys.ValueOutOfRange_4, getName(descriptor),
minimum, maximum, value);
+                error = Errors.format(Errors.Keys.ValueOutOfRange_4, getName(descriptor),
minimum, maximum, converted);
             } else {
                 final Set<T> validValues = descriptor.getValidValues();
-                if (validValues!=null && !validValues.contains(value)) {
+                if (validValues!=null && !validValues.contains(converted)) {
                     error = Errors.format(Errors.Keys.IllegalArgumentValue_2, getName(descriptor),
value);
                 } else {
                     /*

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java
[UTF-8] Fri Feb  7 11:43:15 2014
@@ -23,6 +23,7 @@ import java.net.URISyntaxException;
 import javax.measure.unit.Unit;
 import javax.measure.converter.UnitConverter;
 import javax.measure.converter.ConversionException;
+import org.opengis.metadata.citation.Citation;
 import org.opengis.parameter.ParameterValue;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.parameter.InvalidParameterTypeException;
@@ -43,18 +44,22 @@ import java.nio.file.Path;
  * {@link #setValue(Object)} methods. The type and constraints on parameter values are given
by
  * the {@linkplain #getDescriptor() descriptor}.
  *
- * <p>The following table summarizes the ISO attributes with the corresponding getter
and setter methods:</p>
+ * <p>The following table summarizes the ISO 19111 attributes with the corresponding
getter and
+ * setter methods:</p>
+ *
  * <table class="sis">
- *   <tr><th>ISO attributes</th>   <th>Setter methods</th>
                   <th>Getter methods</th></tr>
- *   <tr><td>booleanValue</td>     <td>{@link #setValue(boolean)}</td>
       <td>{@link #booleanValue()}    : {@code boolean}</td></tr>
- *   <tr><td>integerValue</td>     <td>{@link #setValue(int)}</td>
           <td>{@link #intValue()}        : {@code int}</td></tr>
- *   <tr><td></td>                 <td>{@link #setValue(double)}</td>
        <td>{@link #doubleValue()}     : {@code double}</td></tr>
- *   <tr><td>value</td>            <td>{@link #setValue(double, Unit)}</td>
  <td>{@link #doubleValue(Unit)} : {@code double}</td></tr>
- *   <tr><td>valueList</td>        <td>{@link #setValue(double[],
Unit)}</td> <td>{@link #doubleValueList()} : {@code double[]}</td></tr>
- *   <tr><td></td>                 <td>{@link #setValue(Object)}</td>
        <td>{@link #getValue()}        : {@code Object}</td></tr>
- *   <tr><td>stringValue</td>      <td></td>              
                   <td>{@link #stringValue()}     : {@code String}</td></tr>
- *   <tr><td>integerValueList</td> <td></td>              
                   <td>{@link #intValueList()}    : {@code int[]}</td></tr>
- *   <tr><td>valueFile</td>        <td></td>              
                   <td>{@link #valueFile()}       : {@code URI}</td></tr>
+ *   <tr><th>ISO attribute</th>             <th>Java type</th>
       <th>Getter method</th>                  <th>Setter method</th></tr>
+ *   <tr><td></td>                          <td>{@link Object}</td>
  <td>{@link #getValue()}</td>            <td>{@link #setValue(Object)}</td></tr>
+ *   <tr><td>{@code stringValue}</td>       <td>{@link String}</td>
  <td>{@link #stringValue()}</td>         <td>{@code  setValue(Object)}</td></tr>
+ *   <tr><td>{@code value}</td>             <td>{@code double}</td>
  <td>{@link #doubleValue()}</td>         <td>{@link #setValue(double)}</td></tr>
+ *   <tr><td></td>                          <td>{@code double}</td>
  <td>{@link #doubleValue(Unit)}</td>     <td>{@link #setValue(double, Unit)}</td></tr>
+ *   <tr><td>{@code valueList}</td>         <td>{@code double[]}</td>
<td>{@link #doubleValueList()}</td>     <td>{@code  setValue(Object)}</td></tr>
+ *   <tr><td></td>                          <td>{@code double[]}</td>
<td>{@link #doubleValueList(Unit)}</td> <td>{@link #setValue(double[], Unit)}</td></tr>
+ *   <tr><td>{@code integerValue}</td>      <td>{@code int}</td>
     <td>{@link #intValue()}</td>            <td>{@link #setValue(int)}</td></tr>
+ *   <tr><td>{@code integerValueList}</td>  <td>{@code int[]}</td>
   <td>{@link #intValueList()}</td>        <td>{@code  setValue(Object)}</td></tr>
+ *   <tr><td>{@code booleanValue}</td>      <td>{@code boolean}</td>
 <td>{@link #booleanValue()}</td>        <td>{@link #setValue(boolean)}</td></tr>
+ *   <tr><td>{@code valueFile}</td>         <td>{@link URI}</td>
     <td>{@link #valueFile()}</td>           <td>{@code  setValue(Object)}</td></tr>
+ *   <tr><td>{@code valueFileCitation}</td> <td>{@link Citation}</td>
<td>{@code  getValue()}</td>            <td>{@code  setValue(Object)}</td></tr>
  * </table>
  *
  * Instances of {@code ParameterValue} are created by the {@link ParameterDescriptor#createValue()}
method.
@@ -69,10 +74,9 @@ import java.nio.file.Path;
  * ultimately delegates to the following methods:
  *
  * <ul>
- *   <li>All getter methods will invoke {@link #getValue()} and - if needed - {@link
#getUnit()},
+ *   <li>All getter methods will invoke {@link #getValue()} and {@link #getUnit()}
(if needed),
  *       then perform their processing on the values returned by those methods.</li>
- *   <li>All setter methods will first convert (if needed) and verify the argument
validity,
- *       then pass the values to the {@link #setValue(Object)} method.</li>
+ *   <li>All setter methods delegates to the {@link #setValue(Object, Unit)} method.</li>
  * </ul>
  *
  * Consequently, the above-cited methods provide single points that subclasses can override
@@ -81,7 +85,7 @@ import java.nio.file.Path;
  * @param <T> The value type.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @since   0.4 (derived from Geotk-2.0)
+ * @since   0.4 (derived from geotk-2.0)
  * @version 0.4
  * @module
  *
@@ -97,15 +101,14 @@ public class DefaultParameterValue<T> ex
     /**
      * The value, or {@code null} if undefined.
      * Except for the constructors, the {@link #equals(Object)} and the {@link #hashCode()}
methods,
-     * this field shall be read only by {@link #getValue()} and written by {@link #setValue(Object)}.
+     * this field shall be read only by {@link #getValue()} and written by {@link #setValue(Object,
Unit)}.
      */
     private T value;
 
     /**
      * The unit of measure for the value, or {@code null} if it doesn't apply.
      * Except for the constructors, the {@link #equals(Object)} and the {@link #hashCode()}
methods,
-     * this field shall be read only by {@link #getUnit()} and written by {@link #setValue(double,
Unit)}
-     * or {@link #setValue(double[], Unit)}.
+     * this field shall be read only by {@link #getUnit()} and written by {@link #setValue(Object,
Unit)}.
      */
     private Unit<?> unit;
 
@@ -115,13 +118,28 @@ public class DefaultParameterValue<T> ex
      *
      * @param descriptor The abstract definition of this parameter.
      */
-    protected DefaultParameterValue(final ParameterDescriptor<T> descriptor) {
+    public DefaultParameterValue(final ParameterDescriptor<T> descriptor) {
         super(descriptor);
         value = descriptor.getDefaultValue();
         unit  = descriptor.getUnit();
     }
 
     /**
+     * Creates a new instance initialized with the values from the specified parameter object.
+     * This is a <cite>shallow</cite> copy constructor, since the value contained
in the given
+     * object is not cloned.
+     *
+     * @param parameter The parameter to copy values from, or {@code null} if none.
+     *
+     * @see #castOrCopy(ParameterValue)
+     */
+    public DefaultParameterValue(final ParameterValue<T> parameter) {
+        super(parameter); // Require <T>, not <? extends T>.
+        value = parameter.getValue();
+        unit  = parameter.getUnit();
+    }
+
+    /**
      * Returns the abstract definition of this parameter.
      */
     @Override
@@ -153,7 +171,7 @@ public class DefaultParameterValue<T> ex
     /**
      * Returns the parameter value as an object.
      * If no value has been set, then this method returns the
-     * {@linkplain ParameterDescriptor#getDefaultValue() default value} (which may be null).
+     * {@linkplain DefaultParameterDescriptor#getDefaultValue() default value} (which may
be null).
      *
      * {@section Implementation note for subclasses}
      * All getter methods will invoke this {@code getValue()} method.
@@ -173,6 +191,9 @@ public class DefaultParameterValue<T> ex
      * Returns the boolean value of this parameter.
      * A boolean value does not have an associated unit of measure.
      *
+     * <p>The default implementation invokes {@link #getValue()} and casts the result
if possible,
+     * or throws an exception otherwise.</p>
+     *
      * @return The boolean value represented by this parameter.
      * @throws InvalidParameterTypeException if the value is not a boolean type.
      * @throws IllegalStateException if the value is not defined and there is no default
value.
@@ -192,6 +213,9 @@ public class DefaultParameterValue<T> ex
      * Returns the integer value of this parameter, usually used for a count.
      * An integer value does not have an associated unit of measure.
      *
+     * <p>The default implementation invokes {@link #getValue()} and casts the result
if possible,
+     * or throws an exception otherwise.</p>
+     *
      * @return The numeric value represented by this parameter after conversion to type {@code
int}.
      * @throws InvalidParameterTypeException if the value is not an integer type.
      * @throws IllegalStateException if the value is not defined and there is no default
value.
@@ -215,6 +239,10 @@ public class DefaultParameterValue<T> ex
      * Returns an ordered sequence of two or more integer values of this parameter, usually
used for counts.
      * These integer values do not have an associated unit of measure.
      *
+     * <p>The default implementation invokes {@link #getValue()} and casts the result
if possible,
+     * or throws an exception otherwise. Note that the returned array is a direct reference
to the
+     * array stored by this parameter.</p>
+     *
      * @return The sequence of values represented by this parameter.
      * @throws InvalidParameterTypeException if the value is not an array of {@code int}s.
      * @throws IllegalStateException if the value is not defined and there is no default
value.
@@ -241,6 +269,9 @@ public class DefaultParameterValue<T> ex
      *        way for consistency will other methods defined in this class, since all of
them except
      *        <code>getValue()</code> throw an exception in such case.}
      *
+     * <p>The default implementation invokes {@link #getValue()} and casts the result
if possible,
+     * or throws an exception otherwise.</p>
+     *
      * @return The numeric value represented by this parameter after conversion to type {@code
double}.
      *         This method returns {@link Double#NaN} only if such "value" has been explicitely
set.
      * @throws InvalidParameterTypeException if the value is not a numeric type.
@@ -263,6 +294,10 @@ public class DefaultParameterValue<T> ex
      * Returns an ordered sequence of two or more numeric values of this parameter,
      * where each value has the same associated unit of measure.
      *
+     * <p>The default implementation invokes {@link #getValue()} and casts the result
if possible,
+     * or throws an exception otherwise. Note that the returned array is a direct reference
to the
+     * array stored by this parameter.</p>
+     *
      * @return The sequence of values represented by this parameter.
      * @throws InvalidParameterTypeException if the value is not an array of {@code double}s.
      * @throws IllegalStateException if the value is not defined and there is no default
value.
@@ -306,6 +341,9 @@ public class DefaultParameterValue<T> ex
      * Returns the numeric value of this parameter in the given unit of measure.
      * This convenience method applies unit conversions on the fly as needed.
      *
+     * <p>The default implementation invokes {@link #doubleValue()} and {@link #getUnit()},
+     * then converts the values to the given unit of measurement.</p>
+     *
      * @param  unit The unit of measure for the value to be returned.
      * @return The numeric value represented by this parameter after conversion to type
      *         {@code double} and conversion to {@code unit}.
@@ -326,6 +364,9 @@ public class DefaultParameterValue<T> ex
      * Returns an ordered sequence of numeric values in the specified unit of measure.
      * This convenience method applies unit conversions on the fly as needed.
      *
+     * <p>The default implementation invokes {@link #doubleValueList()} and {@link
#getUnit()},
+     * clone the array, then converts the values to the given unit of measurement.</p>
+     *
      * @param  unit The unit of measure for the value to be returned.
      * @return The sequence of values represented by this parameter after conversion to type
      *         {@code double} and conversion to {@code unit}.
@@ -429,6 +470,8 @@ public class DefaultParameterValue<T> ex
      * Sets the parameter value as an object. The object type is typically a {@link Double},
{@link Integer},
      * {@link Boolean}, {@link String}, {@link URI}, {@code double[]} or {@code int[]}.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param  value The parameter value, or {@code null} to restore the default value.
      * @throws InvalidParameterValueException if the type of {@code value} is inappropriate
for this parameter,
      *         or if the value is illegal for some other reason (for example the value is
numeric and out of range).
@@ -436,14 +479,17 @@ public class DefaultParameterValue<T> ex
      * @see #getValue()
      */
     @Override
-    @SuppressWarnings("unchecked") // Safe because type was checked by the constructor.
     public void setValue(final Object value) throws InvalidParameterValueException {
-        this.value = ensureValidValue((ParameterDescriptor<T>) descriptor, value, unit);
+        setValue(value, unit);
+        // Really 'unit', not 'getUnit()' since units are not expected to be involved in
this method.
+        // We just want the current unit setting to be unchanged.
     }
 
     /**
      * Sets the parameter value as a boolean.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param  value The parameter value.
      * @throws InvalidParameterValueException if the boolean type is inappropriate for this
parameter.
      *
@@ -451,12 +497,14 @@ public class DefaultParameterValue<T> ex
      */
     @Override
     public void setValue(final boolean value) throws InvalidParameterValueException {
-        setValue((Object) Boolean.valueOf(value));
+        setValue(value, unit); // Same comment than setValue(Object).
     }
 
     /**
      * Sets the parameter value as an integer.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param  value The parameter value.
      * @throws InvalidParameterValueException if the integer type is inappropriate for this
parameter,
      *         or if the value is illegal for some other reason (for example a value out
of range).
@@ -465,12 +513,14 @@ public class DefaultParameterValue<T> ex
      */
     @Override
     public void setValue(final int value) throws InvalidParameterValueException {
-        setValue(value);
+        setValue(value, unit); // Same comment than setValue(Object).
     }
 
     /**
      * Sets the parameter value as a floating point. The unit, if any, stay unchanged.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param value The parameter value.
      * @throws InvalidParameterValueException if the floating point type is inappropriate
for this
      *         parameter, or if the value is illegal for some other reason (for example a
value out
@@ -481,12 +531,14 @@ public class DefaultParameterValue<T> ex
      */
     @Override
     public void setValue(final double value) throws InvalidParameterValueException {
-        setValue(value);
+        setValue(value, unit); // Same comment than setValue(Object).
     }
 
     /**
      * Sets the parameter value as a floating point and its associated unit.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param  value The parameter value.
      * @param  unit The unit for the specified value.
      * @throws InvalidParameterValueException if the floating point type is inappropriate
for this parameter,
@@ -497,19 +549,14 @@ public class DefaultParameterValue<T> ex
      */
     @Override
     public void setValue(final double value, final Unit<?> unit) throws InvalidParameterValueException
{
-        final Unit<?> old = this.unit;
-        this.unit = unit;
-        try {
-            setValue(value);
-        } catch (InvalidParameterValueException e) {
-            this.unit = old;
-            throw e;
-        }
+        setValue(value, unit);
     }
 
     /**
      * Sets the parameter value as an array of floating point and their associated unit.
      *
+     * <p>The default implementation delegates to {@link #setValue(Object, Unit)}.</p>
+     *
      * @param  values The parameter values.
      * @param  unit The unit for the specified value.
      * @throws InvalidParameterValueException if the floating point array type is inappropriate
for this parameter,
@@ -517,14 +564,24 @@ public class DefaultParameterValue<T> ex
      */
     @Override
     public void setValue(final double[] values, final Unit<?> unit) throws InvalidParameterValueException
{
-        final Unit<?> old = this.unit;
-        this.unit = unit;
-        try {
-            setValue(value);
-        } catch (InvalidParameterValueException e) {
-            this.unit = old;
-            throw e;
-        }
+        setValue(value, unit);
+    }
+
+    /**
+     * Sets the parameter value and its associated unit.
+     * This method is invoked by all setter methods in this class, thus providing a single
point that
+     * subclasses can override if they want to perform more processing on the value before
its storage,
+     * or to be notified about value changes.
+     *
+     * @param  value The parameter value, or {@code null} to restore the default value.
+     * @param  unit  The unit associated to the new parameter value, or {@code null}.
+     * @throws InvalidParameterValueException if the type of {@code value} is inappropriate
for this parameter,
+     *         or if the value is illegal for some other reason (for example the value is
numeric and out of range).
+     */
+    @SuppressWarnings("unchecked") // Safe because descriptor type is enforced by constructor
signature.
+    protected void setValue(final Object value, final Unit<?> unit) throws InvalidParameterValueException
{
+        this.value = ensureValidValue((ParameterDescriptor<T>) descriptor, value, unit);
+        this.unit  = unit; // Assign only on success.
     }
 
     /**

Added: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java?rev=1565621&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
(added)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
[UTF-8] Fri Feb  7 11:43:15 2014
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.parameter;
+
+import org.opengis.parameter.ParameterDescriptor;
+import org.opengis.parameter.ParameterValue;
+import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.Static;
+
+
+/**
+ * Static methods working on parameters.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4 (derived from geotk-2.1)
+ * @version 0.4
+ * @module
+ */
+public final class Parameters extends Static {
+    /**
+     * Do not allow instantiation of this class.
+     */
+    private Parameters() {
+    }
+
+    /**
+     * Casts the given parameter descriptor to the given type.
+     * An exception is thrown immediately if the parameter does not have the expected value
class.
+     *
+     * @param  <T> The expected value class.
+     * @param  descriptor The descriptor to cast, or {@code null}.
+     * @param  type The expected value class.
+     * @return The descriptor casted to the given type, or {@code null} if the given descriptor
was null.
+     * @throws ClassCastException if the given descriptor doesn't have the expected value
class.
+     *
+     * @category verification
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> ParameterDescriptor<T> cast(final ParameterDescriptor<?>
descriptor, final Class<T> type)
+            throws ClassCastException
+    {
+        if (descriptor != null) {
+            final Class<?> actual = descriptor.getValueClass();
+            // We require a strict equality - not type.isAssignableFrom(actual) - because
in
+            // the later case we could have (to be strict) to return a <? extends T>
type.
+            if (!type.equals(actual)) {
+                throw new ClassCastException(Errors.format(Errors.Keys.IllegalParameterType_2,
+                        descriptor.getName().getCode(), actual));
+            }
+        }
+        return (ParameterDescriptor<T>) descriptor;
+    }
+
+    /**
+     * Casts the given parameter value to the given type.
+     * An exception is thrown immediately if the parameter does not have the expected value
class.
+     *
+     * @param  <T>   The expected value class.
+     * @param  value The value to cast, or {@code null}.
+     * @param  type  The expected value class.
+     * @return The value casted to the given type, or {@code null} if the given value was
null.
+     * @throws ClassCastException if the given value doesn't have the expected value class.
+     *
+     * @category verification
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> ParameterValue<T> cast(final ParameterValue<?> value,
final Class<T> type)
+            throws ClassCastException
+    {
+        if (value != null) {
+            final ParameterDescriptor<?> descriptor = value.getDescriptor();
+            final Class<?> actual = descriptor.getValueClass();
+            if (!type.equals(actual)) { // Same comment than cast(ParameterDescriptor)...
+                throw new ClassCastException(Errors.format(Errors.Keys.IllegalParameterType_2,
+                        descriptor.getName().getCode(), actual));
+            }
+        }
+        return (ParameterValue<T>) value;
+    }
+}

Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Static.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Static.java?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Static.java [UTF-8]
(original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Static.java [UTF-8]
Fri Feb  7 11:43:15 2014
@@ -67,6 +67,8 @@ package org.apache.sis.util;
  * <tr><td>{@link org.apache.sis.referencing.cs.CoordinateSystems}</td>
  *     <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>
  *
  * <tr><th colspan="2" class="hsep">Input / Output (including CRS, XML, images)</th></tr>
  * <tr><td>{@link org.apache.sis.io.IO}</td>

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Fri Feb  7 11:43:15 2014
@@ -271,10 +271,15 @@ public final class Errors extends Indexe
         public static final short IllegalOrdinateRange_3 = 39;
 
         /**
-         * Parameter “{0}” does not accept instances of ‘{2}’. Expected an instance
of ‘{1}’ or derived
-         * type.
+         * Parameter ‘{0}’ can’t be of type ‘{1}’.
          */
-        public static final short IllegalParameterClass_3 = 138;
+        public static final short IllegalParameterType_2 = 143;
+
+        /**
+         * Parameter “{0}” does not accept values of ‘{2}’ type. Expected an instance
of ‘{1}’ or
+         * derived type.
+         */
+        public static final short IllegalParameterValueClass_3 = 138;
 
         /**
          * Property “{0}” does not accept instances of ‘{1}’.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Fri Feb  7 11:43:15 2014
@@ -64,7 +64,8 @@ IllegalMemberType_2               = Memb
 IllegalOperationForValueClass_1   = This operation can not be applied to values of class
\u2018{0}\u2019.
 IllegalOptionValue_2              = Option \u2018{0}\u2019 can not take the \u201c{1}\u201d
value.
 IllegalOrdinateRange_3            = The [{0} \u2026 {1}] range of ordinate values is not
valid for the \u201c{2}\u201d axis.
-IllegalParameterClass_3           = Parameter \u201c{0}\u201d does not accept instances of
\u2018{2}\u2019. Expected an instance of \u2018{1}\u2019 or derived type.
+IllegalParameterType_2            = Parameter \u2018{0}\u2019 can\u2019t be of type \u2018{1}\u2019.
+IllegalParameterValueClass_3      = Parameter \u201c{0}\u201d does not accept values of \u2018{2}\u2019
type. Expected an instance of \u2018{1}\u2019 or derived type.
 IllegalPropertyClass_2            = Property \u201c{0}\u201d does not accept instances of
\u2018{1}\u2019.
 IllegalRange_2                    = Range [{0} \u2026 {1}] is not valid.
 IllegalUnicodeCodePoint_2         = Value {1} for \u201c{0}\u201d is not a valid Unicode
code point.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1565621&r1=1565620&r2=1565621&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Fri Feb  7 11:43:15 2014
@@ -54,7 +54,8 @@ IllegalMemberType_2               = Le m
 IllegalOperationForValueClass_1   = Cette op\u00e9ration ne peut pas s\u2019appliquer aux
valeurs de classe \u2018{0}\u2019.
 IllegalOptionValue_2              = L\u2019option \u2018{0}\u2019 n\u2019accepte pas la valeur
\u00ab\u202f{1}\u202f\u00bb.
 IllegalOrdinateRange_3            = La plage de valeurs de coordonn\u00e9es [{0} \u2026 {1}]
n\u2019est pas valide pour l\u2019axe \u00ab\u202f{2}\u202f\u00bb.
-IllegalParameterClass_3           = Le param\u00e8tre \u00ab\u202f{0}\u202f\u00bb n\u2019accepte
pas les valeurs de type \u2018{2}\u2019. Une instance de \u2018{1}\u2019 ou d\u2019un type
d\u00e9riv\u00e9 \u00e9tait attendue.
+IllegalParameterType_2            = Le param\u00e8tre \u2018{0}\u2019 ne peut pas \u00eatre
du type \u2018{1}\u2019.
+IllegalParameterValueClass_3      = Le param\u00e8tre \u00ab\u202f{0}\u202f\u00bb n\u2019accepte
pas les valeurs de type \u2018{2}\u2019. Une instance de \u2018{1}\u2019 ou d\u2019un type
d\u00e9riv\u00e9 \u00e9tait attendue.
 IllegalPropertyClass_2            = La propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f\u00bb n\u2019accepte
pas les valeurs de type \u2018{1}\u2019.
 IllegalRange_2                    = La plage [{0} \u2026 {1}] n\u2019est pas valide.
 IllegalUnicodeCodePoint_2         = La valeur {1} de \u00ab\u202f{0}\u202f\u00bb n\u2019est
pas un code Unicode valide.



Mime
View raw message