* more parameters than allowed by {@code maximumOccurs}.
*/
@Override
public boolean add(final GeneralParameterValue parameter) {
modCount++;
final GeneralParameterDescriptor type = parameter.getDescriptor();
final List<GeneralParameterDescriptor> descriptors = descriptor.descriptors();
final String name = type.getName().getCode();
if (!descriptors.contains(type)) {
/*
* For a more accurate error message, check if the operation failed because
* the parameter name was not found, or the parameter descriptor doesn't matches.
*/
for (final GeneralParameterDescriptor descriptor : descriptors) {
if (AbstractIdentifiedObject.nameMatches(descriptor, name)) {
/*
* Found a matching name. Consequently, the operation failed because
* the descriptor was illegal.
*/
throw new IllegalArgumentException(Errors.format(
ErrorKeys.ILLEGAL_DESCRIPTOR_FOR_PARAMETER_$1, name));
}
}
/*
* Found no matching name. Consequently, the operation failed because the name
* was invalid.
*/
final Object value;
if (parameter instanceof ParameterValue) {
value = ((ParameterValue) parameter).getValue();
} else {
value = "(group)";
}
throw new InvalidParameterNameException(Errors.format(
ErrorKeys.ILLEGAL_ARGUMENT_$2, name, value), name);
}
final int max = type.getMaximumOccurs();
if (max == 1) {
/*
* Optional or mandatory parameter - we will simply replace what is there.
*/
for (int i=values.size(); --i>=0;) {
final GeneralParameterValue oldValue = values.get(i);
final GeneralParameterDescriptor oldDescriptor = oldValue.getDescriptor();
if (type.equals(oldDescriptor)) {
assert AbstractIdentifiedObject.nameMatches(oldDescriptor, name) : parameter;
final boolean same = parameter.equals(oldValue);
values.set(i, parameter);
return !same;