/** * Called on a single {@linkplain ParameterValue parameter value}, or on elements of a parameter * value. This method ensures that {@linkplain ParameterDescriptor#getMinimumValue minimum * value}, {@linkplain ParameterDescriptor#getMaximumValue maximum value} and {@linkplain * ParameterDescriptor#getValidValues valid values} all think the provided value is okay. * * @param value The value to test. * @param descriptor The descriptor for the value. * @return true if parameter is valid. * @see Parameter#ensureValidValue */ private static boolean isValidValue( final Object value, final ParameterDescriptor<?> descriptor) { final Set<?> validValues = descriptor.getValidValues(); if (validValues != null && !validValues.contains(value)) { return false; } @SuppressWarnings("unchecked") // Type has been verified by the caller. final Comparable<Object> min = (Comparable) descriptor.getMinimumValue(); if (min != null && min.compareTo(value) > 0) { return false; } @SuppressWarnings("unchecked") final Comparable<Object> max = (Comparable) descriptor.getMaximumValue(); if (max != null && max.compareTo(value) < 0) { return false; } return true; }
if (value == null) { final Set<?> validValues = descriptor.getValidValues(); return validValues != null && validValues.contains(value);
/** * Constructs a descriptor with the same values than the specified one. This copy constructor * may be used in order to wraps an arbitrary implementation into a Geotools one. * * @param descriptor The descriptor to copy. * @since 2.2 */ public DefaultParameterDescriptor(final ParameterDescriptor<T> descriptor) { super(descriptor); valueClass = descriptor.getValueClass(); validValues = descriptor.getValidValues(); defaultValue = descriptor.getDefaultValue(); minimum = descriptor.getMinimumValue(); maximum = descriptor.getMaximumValue(); unit = descriptor.getUnit(); }
final EnumeratedParameter[] restrictions; restrictions = listDescriptor.getEnumeratedParameterValues(name); final Set<?> valids = descriptor.getValidValues(); if (valids == null || !Arrays.asList(restrictions).containsAll(valids)) { return false;
error = Errors.format(ErrorKeys.VALUE_OUT_OF_BOUNDS_$3, value, minimum, maximum); } else { final Set<?> validValues = descriptor.getValidValues(); if (validValues != null && !validValues.contains(value)) { error =
@Override public Set<T> getValidValues() {return descriptor.getValidValues();} @Override public Comparable<T> getMinimumValue() {return descriptor.getMinimumValue();}
"Set<AxisDirection>", new HashSet<AxisDirection>(Arrays.asList(AxisDirection.values())), op.getValidValues()); assertNull("defaultValue", op.getDefaultValue()); param.setValue(AxisDirection.DOWN);
/** * Creates a new descriptor with the same values than the specified one. * This copy constructor provides a way to convert an arbitrary implementation into a SIS one or a * user-defined one (as a subclass), usually in order to leverage some implementation-specific API. * * <p>This constructor performs a shallow copy, i.e. the properties are not cloned.</p> * * @param descriptor the descriptor to shallow copy. * * @see #castOrCopy(ParameterDescriptor) */ @SuppressWarnings("unchecked") protected DefaultParameterDescriptor(final ParameterDescriptor<T> descriptor) { super(descriptor); valueClass = descriptor.getValueClass(); validValues = descriptor.getValidValues(); defaultValue = descriptor.getDefaultValue(); valueDomain = Parameters.getValueDomain(descriptor); }
/** * Creates a new descriptor with the same values than the specified one. * This copy constructor provides a way to convert an arbitrary implementation into a SIS one or a * user-defined one (as a subclass), usually in order to leverage some implementation-specific API. * * <p>This constructor performs a shallow copy, i.e. the properties are not cloned.</p> * * @param descriptor the descriptor to shallow copy. * * @see #castOrCopy(ParameterDescriptor) */ @SuppressWarnings("unchecked") protected DefaultParameterDescriptor(final ParameterDescriptor<T> descriptor) { super(descriptor); valueClass = descriptor.getValueClass(); validValues = descriptor.getValidValues(); defaultValue = descriptor.getDefaultValue(); valueDomain = Parameters.getValueDomain(descriptor); }
assertNull("maximum", descriptor.getMaximumValue()); assertNull("unit", descriptor.getUnit()); assertNull("validValues", descriptor.getValidValues()); try { parameter.doubleValue(SI.METRE); assertNull("minimum", descriptor.getMinimumValue()); assertNull("maximum", descriptor.getMaximumValue()); assertNull("validValues", descriptor.getValidValues()); try { parameter.stringValue(); validValues = descriptor.getValidValues(); assertEquals("value", AxisDirection.NORTH, parameter.getValue()); assertEquals("name", "Test", descriptor.getName().getCode());
assertNull("Minimum", param.getMinimumValue()); assertNull("Maximum", param.getMaximumValue()); assertNull("Valid values", param.getValidValues()); assertLocalized("Remarks", param.getRemarks()); assertFalse(
/** * Creates a new descriptor with the same properties than the {@code provided} one, but completed with * information not found in GML. Those extra information are given by the {@code complete} descriptor. * * <p>It is the caller's responsibility to construct the {@code merged} properties as a merge of the properties * of the two given descriptors. This can be done with the help of {@link #mergeArrays(String, Class, Collection, * Map, Identifier)} among others.</p> */ private static <T> ParameterDescriptor<T> create(final Map<String,?> merged, final ParameterDescriptor<?> provided, final ParameterDescriptor<T> complete) { final Class<T> valueClass = complete.getValueClass(); return new DefaultParameterDescriptor<>(merged, provided.getMinimumOccurs(), provided.getMaximumOccurs(), // Values below this point are not provided in GML documents, // so they must be inferred from the pre-defined descriptor. valueClass, Parameters.getValueDomain(complete), CollectionsExt.toArray(complete.getValidValues(), valueClass), complete.getDefaultValue()); }
/** * Creates a new descriptor with the same properties than the {@code provided} one, but completed with * information not found in GML. Those extra information are given by the {@code complete} descriptor. * * <p>It is the caller's responsibility to construct the {@code merged} properties as a merge of the properties * of the two given descriptors. This can be done with the help of {@link #mergeArrays(String, Class, Collection, * Map, Identifier)} among others.</p> */ private static <T> ParameterDescriptor<T> create(final Map<String,?> merged, final ParameterDescriptor<?> provided, final ParameterDescriptor<T> complete) { final Class<T> valueClass = complete.getValueClass(); return new DefaultParameterDescriptor<>(merged, provided.getMinimumOccurs(), provided.getMaximumOccurs(), // Values below this point are not provided in GML documents, // so they must be inferred from the pre-defined descriptor. valueClass, Parameters.getValueDomain(complete), CollectionsExt.toArray(complete.getValidValues(), valueClass), complete.getDefaultValue()); }
/** * Validates the given parameter value. * * @param <T> The class of parameter values. * @param object The object to validate, or {@code null}. */ public <T> void validate(final ParameterValue<T> object) { if (object == null) { return; } final ParameterDescriptor<T> descriptor = object.getDescriptor(); mandatory("ParameterValue: must have a descriptor.", descriptor); validate(descriptor); final T value = object.getValue(); if (value != null) { if (descriptor != null) { final Class<T> valueClass = descriptor.getValueClass(); assertInstanceOf("ParameterValue: getValue() returns unexpected value.", valueClass, value); final Set<T> validValues = descriptor.getValidValues(); if (validValues != null) { assertContains("ParameterValue: getValue() not a member of getValidValues() set.", validValues, value); } assertBetween("ParameterValue: getValue() is out of bounds.", descriptor.getMinimumValue(), descriptor.getMaximumValue(), value); } } }
/** * Validates the given parameter value. * * @param <T> the class of parameter values. * @param object the object to validate, or {@code null}. */ public <T> void validate(final ParameterValue<T> object) { if (object == null) { return; } final ParameterDescriptor<T> descriptor = object.getDescriptor(); mandatory("ParameterValue: shall have a descriptor.", descriptor); validate(descriptor); final T value = object.getValue(); if (value != null) { if (descriptor != null) { final Class<T> valueClass = descriptor.getValueClass(); assertInstanceOf("ParameterValue: getValue() returns unexpected value.", valueClass, value); final Set<T> validValues = descriptor.getValidValues(); if (validValues != null) { validate(validValues); assertContains("ParameterValue: getValue() not a member of getValidValues() set.", validValues, value); } assertBetween("ParameterValue: getValue() is out of bounds.", descriptor.getMinimumValue(), descriptor.getMaximumValue(), value); } } }
that.getValidValues() == null && that.getMinimumValue() == null && that.getMaximumValue() == null;
/** * Verifies the properties of an operation method parameter. * * @param expected a parameter descriptor containing the expected properties (except remarks). * @param actual the parameter descriptor to verify. */ public static void verifyMethodParameter(final ParameterDescriptor<?> expected, final ParameterDescriptor<?> actual) { assertEquals("name", expected.getName(), actual.getName()); assertEquals("valueClass", expected.getValueClass(), actual.getValueClass()); assertEquals("validValues", expected.getValidValues(), actual.getValidValues()); assertEquals("unit", expected.getUnit(), actual.getUnit()); assertEquals("minimumOccurs", DEFAULT_OCCURRENCE, actual.getMinimumOccurs()); assertEquals("maximumOccurs", DEFAULT_OCCURRENCE, actual.getMaximumOccurs()); }
/** * Tests {@code DefaultParameterDescriptor} constructor * with valid and invalid minimum and maximum values. */ @Test @DependsOnMethod("testOptionalInteger") @SuppressWarnings("UnnecessaryBoxing") public void testRangeValidation() { try { create("Test range", 20, 4, 12); fail("minimum > maximum"); } catch (IllegalArgumentException exception) { assertEquals("Range [20 … 4] is not valid.", exception.getMessage()); } final ParameterDescriptor<Integer> descriptor = create("Test range", 4, 20, 12); assertEquals("name", "Test range", descriptor.getName().getCode()); assertEquals("valueClass", Integer.class, descriptor.getValueClass()); assertNull ("validValues", descriptor.getValidValues()); assertEquals("defaultValue", Integer.valueOf(12), descriptor.getDefaultValue()); assertEquals("minimumValue", Integer.valueOf( 4), descriptor.getMinimumValue()); assertEquals("maximumValue", Integer.valueOf(20), descriptor.getMaximumValue()); assertEquals("minimumOccurs", 1, descriptor.getMinimumOccurs()); assertEquals("maximumOccurs", 1, descriptor.getMaximumOccurs()); }
/** * Tests a descriptor for a parameter restricted to some values. * This is typically (but not necessarily) a code list parameter. */ @Test public void testEnumeration() { final String[] enumeration = {"Apple", "Orange", "りんご"}; final ParameterDescriptor<String> descriptor = create( "Enumeration param", String.class, enumeration, "Apple"); assertEquals ("name", "Enumeration param", descriptor.getName().getCode()); assertEquals ("valueClass", String.class, descriptor.getValueClass()); assertArrayEquals("validValues", enumeration, descriptor.getValidValues().toArray()); assertEquals ("defaultValue", "Apple", descriptor.getDefaultValue()); assertNull ("minimumValue", descriptor.getMinimumValue()); assertNull ("maximumValue", descriptor.getMaximumValue()); assertEquals ("minimumOccurs", 1, descriptor.getMinimumOccurs()); assertEquals ("maximumOccurs", 1, descriptor.getMaximumOccurs()); assertNull ("unit", descriptor.getUnit()); /* * Invalid operation: element not in the list of valid elements. */ try { DefaultParameterDescriptor<String> p = create("Enumeration param", String.class, enumeration, "Pear"); fail("Should not be allowed to create " + p); } catch (IllegalArgumentException e) { assertEquals("Parameter “Enumeration param” can not take the “Pear” value.", e.getMessage()); } }
/** * Tests the creation of a simple descriptor for an optional parameter without minimum or maximum value. */ @Test public void testOptionalInteger() { final ParameterDescriptor<Integer> descriptor = createSimpleOptional("Simple param", Integer.class); assertEquals("name", "Simple param", descriptor.getName().getCode()); assertEquals("valueClass", Integer.class, descriptor.getValueClass()); assertNull ("validValues", descriptor.getValidValues()); assertNull ("defaultValue", descriptor.getDefaultValue()); assertNull ("minimumValue", descriptor.getMinimumValue()); assertNull ("maximumValue", descriptor.getMaximumValue()); assertEquals("minimumOccurs", 0, descriptor.getMinimumOccurs()); assertEquals("maximumOccurs", 1, descriptor.getMaximumOccurs()); }