/** * Sets the attribute value. * * @param value the new value, or {@code null} for removing all values from this attribute. */ @Override public void setValue(final V value) { values.clear(); if (value != null) { values.add(value); } }
/** * Tests {@link CheckedArrayList#addAll(Collection)}. */ @Test public void testAddAll() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); assertTrue(list.add("One")); assertTrue(list.addAll(Arrays.asList("Two", "Three"))); assertEquals(Arrays.asList("One", "Two", "Three"), list); }
/** * Creates a collection which will initially contain only the given value. * At the difference of {@link Collections#singletonList(Object)}, this method returns a modifiable list. */ @SuppressWarnings("unchecked") private static <V> Collection<V> singletonList(final Class<V> valueClass, final int minimumOccurs, final Object value) { final CheckedArrayList<V> values = new CheckedArrayList<>(valueClass, Math.max(minimumOccurs, 4)); values.add((V) value); // Type will be checked by CheckedArrayList. return values; }
/** * Sets the attribute value. * * @param value the new value, or {@code null} for removing all values from this attribute. */ @Override public void setValue(final V value) { values.clear(); if (value != null) { values.add(value); } }
/** * Creates a collection which will initially contain only the given value. * At the difference of {@link Collections#singletonList(Object)}, this method returns a modifiable list. */ @SuppressWarnings("unchecked") private static <V> Collection<V> singletonList(final Class<V> valueClass, final int minimumOccurs, final Object value) { final CheckedArrayList<V> values = new CheckedArrayList<>(valueClass, Math.max(minimumOccurs, 4)); values.add((V) value); // Type will be checked by CheckedArrayList. return values; }
/** * Creates a new attribute of the given type initialized to the * {@linkplain DefaultAttributeType#getDefaultValue() default value}. * * @param type information about the attribute (base Java class, domain of values, <i>etc.</i>). */ public MultiValuedAttribute(final DefaultAttributeType<V> type) { super(type); values = new CheckedArrayList<>(type.getValueClass()); final V value = type.getDefaultValue(); if (value != null) { values.add(value); } }
/** * Creates a new attribute of the given type initialized to the * {@linkplain DefaultAttributeType#getDefaultValue() default value}. * * @param type information about the attribute (base Java class, domain of values, <i>etc.</i>). */ public MultiValuedAttribute(final DefaultAttributeType<V> type) { super(type); values = new CheckedArrayList<>(type.getValueClass()); final V value = type.getDefaultValue(); if (value != null) { values.add(value); } }
/** * Sets the feature. * * @param value the new value, or {@code null} for removing all values from this association. */ @Override public void setValue(final AbstractFeature value) { values.clear(); if (value != null) { ensureValid(role.getValueType(), value.getType()); values.add(value); } }
/** * Sets the feature. * * @param value the new value, or {@code null} for removing all values from this association. */ @Override public void setValue(final AbstractFeature value) { values.clear(); if (value != null) { ensureValid(role.getValueType(), value.getType()); values.add(value); } }
/** * Tests {@link CheckedArrayList#add(Object)}. */ @Test public void testAdd() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); assertTrue(list.add("One")); assertTrue(list.add("Two")); assertTrue(list.add("Three")); assertEquals(Arrays.asList("One", "Two", "Three"), list); }
/** * Sets the feature values. All previous values are replaced by the given collection. * * @param newValues the new values. */ @Override public void setValues(final Collection<? extends AbstractFeature> newValues) { if (newValues != values) { ArgumentChecks.ensureNonNull("values", newValues); // The parameter name in public API is "values". final DefaultFeatureType base = role.getValueType(); values.clear(); for (final AbstractFeature value : newValues) { ensureValid(base, value.getType()); values.add(value); } } }
/** * Sets the feature values. All previous values are replaced by the given collection. * * @param newValues the new values. */ @Override public void setValues(final Collection<? extends AbstractFeature> newValues) { if (newValues != values) { ArgumentChecks.ensureNonNull("values", newValues); // The parameter name in public API is "values". final DefaultFeatureType base = role.getValueType(); values.clear(); for (final AbstractFeature value : newValues) { ensureValid(base, value.getType()); values.add(value); } } }
/** * Ensures that we can not element of the wrong type in a sublist. */ @Test @DependsOnMethod("testAddWrongType") public void testAddWrongTypeToSublist() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); assertTrue(list.add("One")); assertTrue(list.add("Two")); assertTrue(list.add("Three")); testAddWrongType(list.subList(1, 3)); // Exception message is JDK-dependent, so we can not test it. }
/** * Ensures that we can not add null elements. */ @Test public void testAddNull() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); try { list.add(null); } catch (NullArgumentException e) { final String message = e.getMessage(); assertTrue(message.contains("CheckedArrayList<String>")); } }