use of org.opengis.parameter.GeneralParameterDescriptor in project sis by apache.
the class DefaultParameterValueGroupTest method testValuesAddAllWithSubgroups.
/**
* Tests {@code DefaultParameterValueGroup.values().addAll(…)} with subgroups.
*
* @since 0.6
*/
@Test
@DependsOnMethod({ "testValuesAddAll", "testAddGroup", "testEqualsAndHashCode" })
public void testValuesAddAllWithSubgroups() {
final DefaultParameterDescriptorGroup group, subGroup;
final List<GeneralParameterDescriptor> descriptors = new ArrayList<>(descriptor.descriptors());
subGroup = new DefaultParameterDescriptorGroup(singletonMap(NAME_KEY, "theSubGroup"), 2, 4, descriptors.toArray(new GeneralParameterDescriptor[descriptors.size()]));
descriptors.add(subGroup);
group = new DefaultParameterDescriptorGroup(singletonMap(NAME_KEY, "theGroup"), descriptors.toArray(new GeneralParameterDescriptor[descriptors.size()]));
/*
* Prepare the GeneralParameterValue instances that we are going to add in the above group.
* We assign arbitrary integer values to each instance if order to be able to differentiate
* them, but the purpose of this test is not to verify those integer values.
*
* We intentionally:
* - Omit the creation of a mandatory parameter value
* - Create more sub-groups than the minimum required.
*/
final ParameterValue<?> v2 = (ParameterValue<?>) descriptor.descriptor("Mandatory 2").createValue();
final ParameterValue<?> v3 = (ParameterValue<?>) descriptor.descriptor("Optional 3").createValue();
final ParameterValueGroup g1 = subGroup.createValue();
final ParameterValueGroup g2 = subGroup.createValue();
final ParameterValueGroup g3 = subGroup.createValue();
v2.setValue(4);
v3.setValue(8);
g1.parameter("Mandatory 1").setValue(3);
g2.parameter("Optional 4").setValue(7);
g3.parameter("Mandatory 2").setValue(5);
final List<GeneralParameterValue> expected = new ArrayList<>(6);
assertTrue(expected.add(v2));
assertTrue(expected.add(v3));
assertTrue(expected.add(g1));
assertTrue(expected.add(g2));
assertTrue(expected.add(g3));
/*
* A newly created group should be initialized with 4 GeneralParameterValue instances because of
* the mandatory ones. After we added our own instances created above, the group should contains
* 6 instances (one more than what we added) because of the "Mandatory 1" parameter that we did
* not provided. Note that the element order in the 'values' collection does not need to be the
* order in which we provided our GeneralParameterValue instances.
*/
final List<GeneralParameterValue> values = group.createValue().values();
assertEquals("Initial size", 4, values.size());
assertTrue("List shall be modified", values.addAll(expected));
assertEquals("Size after addAll(…)", 6, values.size());
final ParameterValue<?> v1 = (ParameterValue<?>) values.get(0);
assertEquals("Default value", DefaultParameterDescriptorGroupTest.DEFAULT_VALUE, v1.getValue());
assertTrue(expected.add(v1));
assertSetEquals(expected, values);
}
use of org.opengis.parameter.GeneralParameterDescriptor in project sis by apache.
the class ParameterValueList method set.
/**
* Sets the parameter at the given index. The descriptor of the given parameter must be one of those
* in the {@link DefaultParameterDescriptorGroup#descriptors()} list, and storing that parameter must
* be allowed by the cardinality constraints.
*/
@Override
public GeneralParameterValue set(final int index, final GeneralParameterValue parameter) {
ArgumentChecks.ensureValidIndex(size, index);
final GeneralParameterValue value = values[index];
ArgumentChecks.ensureNonNull("parameter", parameter);
final GeneralParameterDescriptor desc = parameter.getDescriptor();
if (!value.getDescriptor().equals(desc)) {
ensureDescriptorExists(desc);
ensureCanRemove(desc);
ensureCanAdd(desc);
}
values[index] = parameter;
return value;
}
use of org.opengis.parameter.GeneralParameterDescriptor 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;
}
use of org.opengis.parameter.GeneralParameterDescriptor in project sis by apache.
the class AbstractParameterDescriptor method formatTo.
/**
* Formats this descriptor as a pseudo-<cite>Well Known Text</cite> element. The WKT specification
* does not define any representation of parameter descriptors. Apache SIS fallback on a list of
* {@linkplain DefaultParameterDescriptor#formatTo(Formatter) descriptors}.
* The text formatted by this method is {@linkplain Formatter#setInvalidWKT flagged as invalid WKT}.
*
* @param formatter the formatter where to format the inner content of this WKT element.
* @return {@code "Parameter"} or {@code "ParameterGroup"}.
*/
@Override
protected String formatTo(final Formatter formatter) {
super.formatTo(formatter);
formatter.setInvalidWKT(this, null);
if (this instanceof ParameterDescriptorGroup) {
for (GeneralParameterDescriptor parameter : ((ParameterDescriptorGroup) this).descriptors()) {
if (!(parameter instanceof FormattableObject)) {
if (parameter instanceof ParameterDescriptor<?>) {
parameter = new DefaultParameterDescriptor<>((ParameterDescriptor<?>) parameter);
} else if (parameter instanceof ParameterDescriptorGroup) {
parameter = new DefaultParameterDescriptorGroup((ParameterDescriptorGroup) parameter);
} else {
continue;
}
}
formatter.newLine();
formatter.append((FormattableObject) parameter);
}
return WKTKeywords.ParameterGroup;
} else if (this instanceof ParameterDescriptor<?>) {
final Object defaultValue = ((ParameterDescriptor<?>) this).getDefaultValue();
if (defaultValue != null) {
formatter.appendAny(defaultValue);
}
formatter.append(((ParameterDescriptor<?>) this).getUnit());
}
return WKTKeywords.Parameter;
}
use of org.opengis.parameter.GeneralParameterDescriptor in project sis by apache.
the class DefaultParameterDescriptorGroup method descriptor.
/**
* Returns the first parameter in this group for the specified name.
* This method does not search in sub-groups.
*
* @param name the name of the parameter to search for.
* @return the parameter for the given identifier name.
* @throws ParameterNotFoundException if there is no parameter for the given name.
*/
@Override
@SuppressWarnings("null")
public GeneralParameterDescriptor descriptor(final String name) throws ParameterNotFoundException {
// Quick search for an exact match.
ArgumentChecks.ensureNonNull("name", name);
for (final GeneralParameterDescriptor param : descriptors) {
if (name.equals(param.getName().getCode())) {
return param;
}
}
// More costly search before to give up.
GeneralParameterDescriptor fallback = null, ambiguity = null;
for (final GeneralParameterDescriptor param : descriptors) {
if (IdentifiedObjects.isHeuristicMatchForName(param, name)) {
if (fallback == null) {
fallback = param;
} else {
ambiguity = param;
}
}
}
if (fallback != null && ambiguity == null) {
return fallback;
}
throw new ParameterNotFoundException(ambiguity != null ? Errors.format(Errors.Keys.AmbiguousName_3, IdentifiedObjects.toString(fallback.getName()), IdentifiedObjects.toString(ambiguity.getName()), name) : Resources.format(Resources.Keys.ParameterNotFound_2, Verifier.getDisplayName(this), name), name);
}
Aggregations