Search in sources :

Example 1 with DefaultParameterValueGroup

use of org.apache.sis.parameter.DefaultParameterValueGroup in project sis by apache.

the class AbstractSingleOperation method setParameters.

/**
 * Invoked by JAXB for setting the unmarshalled parameters.
 * This method wraps the given parameters in a {@link ParameterValueGroup},
 * unless the given descriptors was already a {@code ParameterValueGroup}.
 *
 * @see DefaultOperationMethod#setDescriptors
 */
private void setParameters(final GeneralParameterValue[] values) {
    if (parameters == null) {
        if (!(method instanceof DefaultOperationMethod)) {
            // May be a non-null proxy if defined only by xlink:href.
            throw new IllegalStateException(Errors.format(Errors.Keys.MissingValueForProperty_1, "method"));
        }
        /*
             * The descriptors in the <gml:method> element do not know the class of parameter value
             * (String, Integer, Double, double[], etc.) because this information is not part of GML.
             * But this information is available to descriptors in the <gml:parameterValue> elements
             * because Apache SIS infers the type from the actual parameter value. The 'merge' method
             * below puts those information together.
             */
        final Map<GeneralParameterDescriptor, GeneralParameterDescriptor> replacements = new IdentityHashMap<>(4);
        final GeneralParameterDescriptor[] merged = CC_OperationParameterGroup.merge(method.getParameters().descriptors(), Parameters.getDescriptors(values), replacements);
        /*
             * Sometime Apache SIS recognizes the OperationMethod as one of its build-in methods and use the
             * build-in parameters. In such cases the unmarshalled ParameterDescriptorGroup can be used as-in.
             * But if the above 'merge' method has changed any parameter descriptor, then we will need to create
             * a new ParameterDescriptorGroup with the new descriptors.
             */
        for (int i = 0; i < merged.length; i++) {
            if (merged[i] != values[i].getDescriptor()) {
                ((DefaultOperationMethod) method).updateDescriptors(merged);
                // At this point, method.getParameters() may have changed.
                break;
            }
        }
        /*
             * Sometime the descriptors associated to ParameterValues need to be updated, for example because
             * the descriptors in OperationMethod contain more information (remarks, etc.). Those updates, if
             * needed, are applied on-the-fly by the copy operation below, using the information provided by
             * the 'replacements' map.
             */
        parameters = new DefaultParameterValueGroup(method.getParameters());
        CC_OperationMethod.store(values, parameters.values(), replacements);
        parameters = Parameters.unmodifiable(parameters);
    } else {
        MetadataUtilities.propertyAlreadySet(AbstractSingleOperation.class, "setParameters", "parameterValue");
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) DefaultParameterValueGroup(org.apache.sis.parameter.DefaultParameterValueGroup)

Example 2 with DefaultParameterValueGroup

use of org.apache.sis.parameter.DefaultParameterValueGroup in project sis by apache.

the class CC_OperationMethod method store.

/**
 * Stores the given {@code parameters} into the given {@code addTo} collection.
 * This method copies only the <em>references</em> if possible. However is some
 * cases the values may need to be copied in new parameter instances.
 *
 * <div class="note"><b>Note:</b>
 * this code is defined in this {@code CC_OperationMethod} class instead than in the
 * {@link DefaultOperationMethod} class in the hope to reduce the amount of code processed
 * by the JVM in the common case where JAXB (un)marshalling is not needed.</div>
 *
 * @param  parameters    the parameters to add to the {@code addTo} collection.
 * @param  addTo         where to store the {@code parameters}.
 * @param  replacements  the replacements to apply in the {@code GeneralParameterValue} instances.
 */
public static void store(final GeneralParameterValue[] parameters, final Collection<GeneralParameterValue> addTo, final Map<GeneralParameterDescriptor, GeneralParameterDescriptor> replacements) {
    for (GeneralParameterValue p : parameters) {
        final GeneralParameterDescriptor replacement = replacements.get(p.getDescriptor());
        if (replacement != null) {
            if (p instanceof ParameterValue<?>) {
                final ParameterValue<?> source = (ParameterValue<?>) p;
                final ParameterValue<?> target = new DefaultParameterValue<>((ParameterDescriptor<?>) replacement);
                final Object value = source.getValue();
                final Unit<?> unit = source.getUnit();
                if (unit == null) {
                    target.setValue(value);
                } else if (value instanceof double[]) {
                    target.setValue((double[]) value, unit);
                } else {
                    target.setValue(((Number) value).doubleValue(), unit);
                }
                p = target;
            } else if (p instanceof ParameterValueGroup) {
                final ParameterValueGroup source = (ParameterValueGroup) p;
                final ParameterValueGroup target = new DefaultParameterValueGroup((ParameterDescriptorGroup) replacement);
                final Collection<GeneralParameterValue> values = source.values();
                store(values.toArray(new GeneralParameterValue[values.size()]), target.values(), replacements);
                p = target;
            }
        }
        addTo.add(p);
    }
}
Also used : DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) ParameterValue(org.opengis.parameter.ParameterValue) DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) DefaultParameterValueGroup(org.apache.sis.parameter.DefaultParameterValueGroup) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) DefaultParameterValueGroup(org.apache.sis.parameter.DefaultParameterValueGroup) Collection(java.util.Collection)

Aggregations

DefaultParameterValueGroup (org.apache.sis.parameter.DefaultParameterValueGroup)2 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)2 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 DefaultParameterDescriptorGroup (org.apache.sis.parameter.DefaultParameterDescriptorGroup)1 DefaultParameterValue (org.apache.sis.parameter.DefaultParameterValue)1 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)1 ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)1 ParameterValue (org.opengis.parameter.ParameterValue)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1