DefaultParameterValue changes for revisions 7703:7857

Changes in this commit and actions taken:

Command line:

svn diff --extensions "--unified --ignore-space-change --ignore-all-space --ignore-eol-style" -r7703:7857 https://svn.osgeo.org/geotools/trunk/modules/library/referencing/src/main/java/org/geotools/parameter/Parameter.java
Revision 7703Revision 7857
package org.geotools.parameter;

// J2SE dependencies
import java.net.URL;
import java.util.Set;
import java.util.Arrays;
import java.util.Locale;
import javax.units.Unit;
import javax.units.SI;
import javax.units.NonSI;
package org.geotools.parameter;

// J2SE dependencies
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Collection;
import java.util.Set;
import java.util.Arrays;
import javax.units.Unit;
import javax.units.SI;
import javax.units.NonSI;
// OpenGIS dependencies
import org.opengis.util.CodeList;
import org.opengis.parameter.OperationParameter;
import org.opengis.parameter.GeneralOperationParameter;
import org.opengis.parameter.InvalidParameterTypeException;
import org.opengis.parameter.InvalidParameterValueException;

// Geotools dependencies
import org.geotools.resources.Utilities;
import org.geotools.resources.cts.Resources;
import org.geotools.resources.cts.ResourceKeys;
// OpenGIS dependencies
import org.opengis.util.CodeList;
import org.opengis.parameter.ParameterDescriptor;
import org.opengis.parameter.GeneralParameterDescriptor;
import org.opengis.parameter.InvalidParameterTypeException;
import org.opengis.parameter.InvalidParameterValueException;

// Geotools dependencies
import org.geotools.data.DataSourceException;
import org.geotools.resources.Utilities;
import org.geotools.resources.cts.Resources;
import org.geotools.resources.cts.ResourceKeys;
/**
 * A parameter value used by an operation method. Most parameter values are numeric, but
 * other types of parameter values are possible. The parameter type can be fetch with the
 * <code>{@linkplain #getValue()}.{@linkplain Object#getClass() getClass()}</code> idiom.
 * The {@link #getValue()} and {@link #setValue(Object)} methods can be invoked at any time.
 * Others getters and setters are parameter-type dependents.
 *
 * @version $Id$
 * @author Martin Desruisseaux
 *
 * @see org.geotools.parameter.OperationParameter
 * @see org.geotools.parameter.ParameterValueGroup
 */
public class ParameterValue extends GeneralParameterValue
                         implements org.opengis.parameter.ParameterValue
{
    /**
/**
 * A parameter value used by an operation method.
 * <p>
 * Most CRS parameter values are numeric, but other types of parameter values are possible. The parameter type can be fetch with the
 * <code>{@linkplain #getValue()}.{@linkplain Object#getClass() getClass()}</code> idiom.
 * The {@link #getValue()} and {@link #setValue(Object)} methods can be invoked at any time.
 * Others getters and setters are parameter-type dependents.
 * </p>
 * <p>
 * This implementation extends the capabilities of the geoapi interface with an additional
 * functionality proivded by parse( text ), and text() methods.
 * </p>
 * @version $Id$
 * @author Martin Desruisseaux
 *
 * @see org.geotools.parameter.ParameterDescriptor
 * @see org.geotools.parameter.ParameterGroup
 */
public class Parameter extends AbstractParameter
                         implements org.opengis.parameter.ParameterValue
{
    /**
/**
 * Construct a parameter from the specified name and value. This convenience constructor
 * creates a default {@link org.geotools.parameter.OperationParameter} object. But if such
 * an object was available, then the preferred way to get a <code>ParameterValue</code>
 * is to invokes {@link org.geotools.parameter.OperationParameter#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 */
public ParameterValue(final String name, final int value) {
    this(new org.geotools.parameter.OperationParameter(name,
             0, Integer.MIN_VALUE, Integer.MAX_VALUE));
    this.value = wrap(value);
}
/**
 * Construct a parameter from the specified name and value. This convenience constructor
 * creates a default {@link org.geotools.parameter.ParameterDescriptor} object. But if such
 * an object was available, then the preferred way to get a <code>ParameterValue</code>
 * is to invokes {@link org.geotools.parameter.ParameterDescriptor#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 */
public Parameter(final String name, final int value) {
    this(new org.geotools.parameter.ParameterDescriptor(name,
             0, Integer.MIN_VALUE, Integer.MAX_VALUE));
    this.value = wrap(value);
}
/**
 * Construct a parameter from the specified name and value. This convenience constructor
 * creates a default {@link org.geotools.parameter.OperationParameter} object. But if such
 * an object was available, then the preferred way to get a <code>ParameterValue</code> is
 * to invokes {@link org.geotools.parameter.OperationParameter#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 * @param unit  The unit for the parameter value.
 */
public ParameterValue(final String name, final double value, final Unit unit) {
    this(new org.geotools.parameter.OperationParameter(name,
             Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, normalize(unit)));
    this.value = wrap(value);
    this.unit  = unit;
/**
 * Construct a parameter from the specified name and value. This convenience constructor
 * creates a default {@link org.geotools.parameter.ParameterDescriptor} object. But if such
 * an object was available, then the preferred way to get a <code>ParameterValue</code> is
 * to invokes {@link org.geotools.parameter.ParameterDescriptor#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 * @param unit  The unit for the parameter value.
 */
public Parameter(final String name, final double value, final Unit unit) {
    this(new org.geotools.parameter.ParameterDescriptor(name,
             Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, normalize(unit)));
    this.value = wrap(value);
    this.unit  = unit;
/**
 * Construct a parameter from the specified enumeration. This convenience constructor
 * creates a default {@link org.geotools.parameter.OperationParameter} object. But if
 * such an object was available, then the preferred way to get a <code>ParameterValue</code>
 * is to invokes {@link org.geotools.parameter.OperationParameter#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 */
public ParameterValue(final String name, final CodeList value) {
    this(new org.geotools.parameter.OperationParameter(name, value));
    this.value = value;
}
/**
 * Construct a parameter from the specified enumeration. This convenience constructor
 * creates a default {@link org.geotools.parameter.ParameterDescriptor} object. But if
 * such an object was available, then the preferred way to get a <code>ParameterValue</code>
 * is to invokes {@link org.geotools.parameter.ParameterDescriptor#createValue}.
 *
 * @param name  The parameter name.
 * @param value The parameter value.
 */
public Parameter(final String name, final CodeList value) {
    this(new org.geotools.parameter.ParameterDescriptor(name, value));
    this.value = value;
}
 *
 * @param descriptor The abstract definition of this parameter.
 */
public ParameterValue(final OperationParameter descriptor) {
    super(descriptor);
    value = descriptor.getDefaultValue();
    unit  = descriptor.getUnit();
 *
 * @param descriptor The abstract definition of this parameter.
 */
public Parameter(final ParameterDescriptor descriptor) {
    super(descriptor);
    value = descriptor.getDefaultValue();
    unit  = descriptor.getUnit();
/**
 * Ensures that the given value is valid according the specified parameter descriptor.
 * This convenience method ensures that <code>value</code> is assignable to the
 * {@linkplain org.geotools.parameter.OperationParameter#getValueClass expected class}, is between the
 * {@linkplain org.geotools.parameter.OperationParameter#getMinimumValue minimum} and
 * {@linkplain org.geotools.parameter.OperationParameter#getMaximumValue maximum} values and is one of the
 * {@linkplain org.geotools.parameter.OperationParameter#getValidValues set of valid values}.
 * If the value fails any of those tests, then an exception is thrown.
 *
 * @param  descriptor The parameter descriptor to check against.
/**
 * Ensures that the given value is valid according the specified parameter descriptor.
 * This convenience method ensures that <code>value</code> is assignable to the
 * {@linkplain org.geotools.parameter.ParameterDescriptor#getValueClass expected class}, is between the
 * {@linkplain org.geotools.parameter.ParameterDescriptor#getMinimumValue minimum} and
 * {@linkplain org.geotools.parameter.ParameterDescriptor#getMaximumValue maximum} values and is one of the
 * {@linkplain org.geotools.parameter.ParameterDescriptor#getValidValues set of valid values}.
 * If the value fails any of those tests, then an exception is thrown.
 *
 * @param  descriptor The parameter descriptor to check against.
 * @param  value The value to check, or <code>null</code>.
 * @throws InvalidParameterValueException if the parameter value is invalid.
 */
public static void ensureValidValue(final OperationParameter descriptor, final Object value)
        throws InvalidParameterValueException
{
    if (value == null) {
 * @param  value The value to check, or <code>null</code>.
 * @throws InvalidParameterValueException if the parameter value is invalid.
 */
public static void ensureValidValue(final ParameterDescriptor descriptor, final Object value)
        throws InvalidParameterValueException
{
    if (value == null) {
 */
private String getClassTypeError() {
    return Resources.format(ResourceKeys.ERROR_ILLEGAL_OPERATION_FOR_VALUE_CLASS_$1,
           Utilities.getShortName(((OperationParameter)descriptor).getValueClass()));
}

/**
 * Returns the parameter name in the default locale.
 */
static String getName(final GeneralOperationParameter descriptor) {
    return descriptor.getName(Locale.getDefault());
}

/**
 */
private String getClassTypeError() {
    return Resources.format(ResourceKeys.ERROR_ILLEGAL_OPERATION_FOR_VALUE_CLASS_$1,
           Utilities.getShortName(((ParameterDescriptor)descriptor).getValueClass()));
}

/**
 * Returns the parameter name in the default locale.
 */
static String getName(final GeneralParameterDescriptor descriptor) {
    return descriptor.getName().toString();
}

/**
 */
public void setValue(final double value, final Unit unit) throws InvalidParameterValueException {
    ensureNonNull("unit", unit);
    final Unit targetUnit = ((OperationParameter) descriptor).getUnit();
    if (targetUnit == null) {
        throw new IllegalStateException(Resources.format(
                  ResourceKeys.ERROR_UNITLESS_PARAMETER_$1, getName(descriptor)));
 */
public void setValue(final double value, final Unit unit) throws InvalidParameterValueException {
    ensureNonNull("unit", unit);
    final Unit targetUnit = ((ParameterDescriptor) descriptor).getUnit();
    if (targetUnit == null) {
        throw new IllegalStateException(Resources.format(
                  ResourceKeys.ERROR_UNITLESS_PARAMETER_$1, getName(descriptor)));
        throw new IllegalArgumentException(Resources.format(expectedID, unit));
    }
    final Double converted = wrap(unit.getConverterTo(targetUnit).convert(value));
    ensureValidValue((OperationParameter) descriptor, converted);
    this.value = wrap(value);
    this.unit  = unit;
}
        throw new IllegalArgumentException(Resources.format(expectedID, unit));
    }
    final Double converted = wrap(unit.getConverterTo(targetUnit).convert(value));
    ensureValidValue((ParameterDescriptor) descriptor, converted);
    this.value = wrap(value);
    this.unit  = unit;
}
 */
public void setValue(final double value) throws InvalidParameterValueException {
    final Double check = wrap(value);
    ensureValidValue((OperationParameter) descriptor, check);
    this.value = check;
}
 */
public void setValue(final double value) throws InvalidParameterValueException {
    final Double check = wrap(value);
    ensureValidValue((ParameterDescriptor) descriptor, check);
    this.value = check;
}
 */
public void setValue(final int value) throws InvalidParameterValueException {
    final Integer check = wrap(value);
    ensureValidValue((OperationParameter) descriptor, check);
    this.value = check;
}
 */
public void setValue(final int value) throws InvalidParameterValueException {
    final Integer check = wrap(value);
    ensureValidValue((ParameterDescriptor) descriptor, check);
    this.value = check;
}
 */
public void setValue(final boolean value) throws InvalidParameterValueException {
    final Boolean check = Boolean.valueOf(value);
    ensureValidValue((OperationParameter) descriptor, check);
    this.value = check;
}
 */
public void setValue(final boolean value) throws InvalidParameterValueException {
    final Boolean check = Boolean.valueOf(value);
    ensureValidValue((ParameterDescriptor) descriptor, check);
    this.value = check;
}
 * @see #getValue
 */
public void setValue(final Object value) throws InvalidParameterValueException {
    ensureValidValue((OperationParameter) descriptor, value);
    this.value = value;
}
 * @see #getValue
 */
public void setValue(final Object value) throws InvalidParameterValueException {
    ensureValidValue((ParameterDescriptor) descriptor, value);
    this.value = value;
}
 */
public void setValue(double[] values, final Unit unit) throws InvalidParameterValueException {
    ensureNonNull("unit", unit);
    final Unit targetUnit = ((OperationParameter) descriptor).getUnit();
    if (targetUnit == null) {
        throw new IllegalStateException(Resources.format(
                  ResourceKeys.ERROR_UNITLESS_PARAMETER_$1, getName(descriptor)));
 */
public void setValue(double[] values, final Unit unit) throws InvalidParameterValueException {
    ensureNonNull("unit", unit);
    final Unit targetUnit = ((ParameterDescriptor) descriptor).getUnit();
    if (targetUnit == null) {
        throw new IllegalStateException(Resources.format(
                  ResourceKeys.ERROR_UNITLESS_PARAMETER_$1, getName(descriptor)));
    for (int i=0; i<converted.length; i++) {
        converted[i] = converter.convert(converted[i]);
    }
    ensureValidValue((OperationParameter) descriptor, converted);
    this.value = values;
    this.unit  = unit;
}
    for (int i=0; i<converted.length; i++) {
        converted[i] = converter.convert(converted[i]);
    }
    ensureValidValue((ParameterDescriptor) descriptor, converted);
    this.value = values;
    this.unit  = unit;
}
 */
public boolean equals(final Object object) {
    if (super.equals(object)) {
        final ParameterValue that = (ParameterValue) object;
        return Utilities.equals(this.value, that.value) &&
               Utilities.equals(this.unit,  that.unit);
    }
 */
public boolean equals(final Object object) {
    if (super.equals(object)) {
        final Parameter that = (Parameter) object;
        return Utilities.equals(this.value, that.value) &&
               Utilities.equals(this.unit,  that.unit);
    }
        if (unit  != null) code += 37*unit.hashCode();
        return code;
    }
}
        if (unit  != null) code += 37*unit.hashCode();
        return code;
    }

    /**
     * Set the Parameter value of the provided text.
     *
     * @param text parsed using valueOf( text )
     */
    public void parse( String text ) throws IOException {
        setValue( valueOf( text ) );
}
    /** Text representation of parameter value.
     * <p>
     * Should be suitable for use with parse( text )
     * @return
     */
    public String text(){
        Object obj = getValue();
        if( obj == null ){
            return null;
        }
        return obj.toString();
    }
    /**
     * Parses the text into a value for this Parameter.
     * <p>
     * Default implementation uses reflection to look for a constructor
     * that takes a single String.
     * </p>
     * @param text Text to parse
     * @return value value of type getDescriptor().getValueClass()
     *
     * @throws IOException If text could not be parsed to getDescriptor().getValueClass()
     */
    protected Object valueOf( String text ) throws IOException {

        ParameterDescriptor descriptor =
            (ParameterDescriptor) this.getDescriptor();

        Class type = descriptor.getValueClass();

        if (text == null) {
            return null;
        }
        if (type == String.class) {
            return text;
        }
        if (text.length() == 0) {
            return null;
        }

        Constructor constructor;

        try {
            constructor = type.getConstructor(new Class[] { String.class });
        } catch (SecurityException e) {
            //  type( String ) constructor is not public
            throw new IOException("Could not create " + type.getName()
                + " from text");
        } catch (NoSuchMethodException e) {
            // No type( String ) constructor
            throw new IOException("Could not create " + type.getName()
                + " from text");
        }

        try {
            return constructor.newInstance(new Object[] { text, });
        } catch (IllegalArgumentException illegalArgumentException) {
            throw new DataSourceException("Could not create "
                + type.getName() + ": from '" + text + "'",
                illegalArgumentException);
        } catch (InstantiationException instantiaionException) {
            throw new DataSourceException("Could not create "
                + type.getName() + ": from '" + text + "'",
                instantiaionException);
        } catch (IllegalAccessException illegalAccessException) {
            throw new DataSourceException("Could not create "
                + type.getName() + ": from '" + text + "'",
                illegalAccessException);
        } catch (InvocationTargetException targetException) {
            Throwable cause = targetException.getCause();
            throw new DataSourceException( cause );

        }
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        String name = descriptor.getName().toString( null );
        Object value = getValue();

        StringBuffer buf = new StringBuffer();
        buf.append( "[<" );
        buf.append( descriptor.getName().toString( null ) );
        buf.append( "> " );
        if( value == null ){
            buf.append( "null" );
        }
        else if( value.getClass().isArray() ){
            int length = Array.getLength( value );
            if( length == 0 ){
                buf.append( "(,)" );
            }
            else {
                buf.append( "(");
                for( int i = 0; i< Math.min(5,length);i++){
                    buf.append( Array.get( value, 0 ) );
                    buf.append( "," );
                }
                if( length > 5 ){
                    buf.append( "..." );
                }
                buf.append(")");
            }
        }
        else {
            buf.append( value );
        }
        buf.append("]");
        return buf.toString();
    }
}