Search in sources :

Example 1 with InvalidParameterCardinalityException

use of org.opengis.parameter.InvalidParameterCardinalityException in project sis by apache.

the class ParameterValueList method add.

/**
 * Adds a {@link ParameterValue} or an other {@link ParameterValueGroup} to this list.
 * If an existing parameter is already included for the same name and adding the new
 * parameter would increase the number past what is allowable by {@code maximumOccurs},
 * then an {@link InvalidParameterCardinalityException} will be thrown.
 *
 * @param  parameter  new parameter to be added to this group.
 * @return always {@code true} since this object changes as a result of this call.
 * @throws IllegalArgumentException if the specified parameter is not allowable by the groups descriptor.
 * @throws InvalidParameterCardinalityException if adding this parameter would result in more parameters
 *         than allowed by {@code maximumOccurs}.
 */
@Override
public boolean add(final GeneralParameterValue parameter) {
    ArgumentChecks.ensureNonNull("parameter", parameter);
    final GeneralParameterDescriptor desc = parameter.getDescriptor();
    ensureDescriptorExists(desc);
    /*
         * If we had an uninitialized parameter (a parameter created by the DefaultParameterValueGroup constructor
         * and never been queried or set by the user), then the given parameter will replace the uninitialized.
         * The intent is to allow users to set its own parameters by a call to group.values().addAll(myParam).
         * Otherwise the given parameter will be added, in which case we need to check the cardinality.
         */
    final Identifier name = desc.getName();
    int count = 0;
    for (int i = 0; i < size; i++) {
        final GeneralParameterValue value = values[i];
        if (name.equals(value.getDescriptor().getName())) {
            if (value instanceof UninitializedParameter) {
                values[i] = parameter;
                return true;
            }
            count++;
        }
    }
    final int max = desc.getMaximumOccurs();
    if (count >= max) {
        throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooManyOccurrences_2, max, name), name.getCode());
    }
    addUnchecked(parameter);
    modCount++;
    return true;
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Identifier(org.opengis.metadata.Identifier) InvalidParameterCardinalityException(org.opengis.parameter.InvalidParameterCardinalityException) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor)

Example 2 with InvalidParameterCardinalityException

use of org.opengis.parameter.InvalidParameterCardinalityException in project sis by apache.

the class ParameterValueList method ensureCanAdd.

/**
 * Verifies if adding a parameter with the given descriptor is allowed by the cardinality constraints. If adding
 * the parameter would result in more occurrences than {@link DefaultParameterDescriptor#getMaximumOccurs()},
 * then this method throws an {@link InvalidParameterCardinalityException}.
 */
private void ensureCanAdd(final GeneralParameterDescriptor desc) {
    final Identifier name = desc.getName();
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (name.equals(values[i].getDescriptor().getName())) {
            count++;
        }
    }
    final int max = desc.getMaximumOccurs();
    if (count >= max) {
        throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooManyOccurrences_2, max, name), name.getCode());
    }
}
Also used : Identifier(org.opengis.metadata.Identifier) InvalidParameterCardinalityException(org.opengis.parameter.InvalidParameterCardinalityException)

Example 3 with InvalidParameterCardinalityException

use of org.opengis.parameter.InvalidParameterCardinalityException in project sis by apache.

the class ParameterValueList method ensureCanRemove.

/**
 * Verifies if removing the given value is allowed by the cardinality constraints. If removing the parameter
 * would result in less occurrences than {@link DefaultParameterDescriptor#getMinimumOccurs()},
 * then this method throws an {@link InvalidParameterCardinalityException}.
 */
private void ensureCanRemove(final GeneralParameterDescriptor desc) {
    final int min = desc.getMinimumOccurs();
    if (min != 0) {
        // Optimization for a common case.
        final Identifier name = desc.getName();
        int count = 0;
        for (int i = 0; i < size; i++) {
            if (name.equals(values[i].getDescriptor().getName())) {
                if (++count > min) {
                    return;
                }
            }
        }
        throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooFewOccurrences_2, min, name), name.getCode());
    }
}
Also used : Identifier(org.opengis.metadata.Identifier) InvalidParameterCardinalityException(org.opengis.parameter.InvalidParameterCardinalityException)

Aggregations

Identifier (org.opengis.metadata.Identifier)3 InvalidParameterCardinalityException (org.opengis.parameter.InvalidParameterCardinalityException)3 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)1 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)1