/** * Creates a new association of the given role. * * @param role Information about the association. */ public MultiValuedAssociation(final DefaultAssociationRole role) { super(role); values = new CheckedArrayList<>(AbstractFeature.class); }
/** * Returns all identifiers associated to this object, or an empty collection if none. * Those identifiers are marshalled in XML as {@code id} or {@code uuid} attributes. */ @Override @SuppressWarnings("ReturnOfCollectionOrArrayField") public Collection<Identifier> getIdentifiers() { if (identifiers == null) { identifiers = new CheckedArrayList<>(Identifier.class); } return identifiers; }
/** * Returns all identifiers associated to this object, or an empty collection if none. * Those identifiers are marshalled in XML as {@code id} or {@code uuid} attributes. */ @Override @SuppressWarnings("ReturnOfCollectionOrArrayField") public Collection<Identifier> getIdentifiers() { if (identifiers == null) { identifiers = new CheckedArrayList<>(Identifier.class); } return identifiers; }
/** * Creates a new association of the given role. * * @param role Information about the association. */ public MultiValuedAssociation(final DefaultAssociationRole role) { super(role); values = new CheckedArrayList<>(AbstractFeature.class); }
/** * Creates a modifiable list for elements of the given type. This method is defined mostly * for consistency with {@link #createSet(Class, Collection)}. * * @param source the collection to be copied in the new list. This method uses this information * only for computing initial capacity; it does not perform the actual copy. */ private static <E> List<E> createList(final Class<E> elementType, final Collection<?> source) { return new CheckedArrayList<>(elementType, source.size()); }
/** * 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 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 association of the given role initialized to the given values. * * @param role Information about the association. * @param values The initial values, or {@code null} for initializing to an empty list. */ MultiValuedAssociation(final DefaultAssociationRole role, final Object values) { super(role); if (values == null) { this.values = new CheckedArrayList<>(AbstractFeature.class); } else { this.values = CheckedArrayList.castOrCopy((CheckedArrayList<?>) values, AbstractFeature.class); } }
/** * Creates a new association of the given role initialized to the given values. * * @param role Information about the association. * @param values The initial values, or {@code null} for initializing to an empty list. */ MultiValuedAssociation(final DefaultAssociationRole role, final Object values) { super(role); if (values == null) { this.values = new CheckedArrayList<>(AbstractFeature.class); } else { this.values = CheckedArrayList.castOrCopy((CheckedArrayList<?>) values, AbstractFeature.class); } }
/** * Creates a list with the content of the {@code source} collection, * or returns {@code null} if the source is {@code null} or empty. * This is a convenience method for copying fields in subclass copy constructors. * * @param <E> the type represented by the {@code Class} argument. * @param source the source collection, or {@code null}. * @param elementType the base type of elements to put in the list. * @return a list containing the {@code source} elements, * or {@code null} if the source was null or empty. */ protected final <E> List<E> copyList(final Collection<? extends E> source, final Class<E> elementType) { if (isNullOrEmpty(source)) { return null; } final List<E> target = new CheckedArrayList<>(elementType, source.size()); target.addAll(source); return target; }
/** * 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 given values. * Note that a {@code null} value may not be the same as the default value. * * @param type information about the attribute (base Java class, domain of values, <i>etc.</i>). * @param values the initial values, or {@code null} for initializing to an empty list. */ @SuppressWarnings("unchecked") MultiValuedAttribute(final DefaultAttributeType<V> type, final Object values) { super(type); final Class<V> valueClass = type.getValueClass(); if (values == null) { this.values = new CheckedArrayList<>(valueClass); } else { final Class<?> actual = ((CheckedContainer<?>) values).getElementType(); if (actual == valueClass) { this.values = (CheckedArrayList<V>) values; } else { throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_3, "values", valueClass, actual)); } } }
/** * 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 given values. * Note that a {@code null} value may not be the same as the default value. * * @param type information about the attribute (base Java class, domain of values, <i>etc.</i>). * @param values the initial values, or {@code null} for initializing to an empty list. */ @SuppressWarnings("unchecked") MultiValuedAttribute(final DefaultAttributeType<V> type, final Object values) { super(type); final Class<V> valueClass = type.getValueClass(); if (values == null) { this.values = new CheckedArrayList<>(valueClass); } else { final Class<?> actual = ((CheckedContainer<?>) values).getElementType(); if (actual == valueClass) { this.values = (CheckedArrayList<V>) values; } else { throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_3, "values", valueClass, actual)); } } }
/** * Ensures that we can not element of the wrong type. */ @Test public void testAddWrongType() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); final String message = testAddWrongType(list); assertTrue("element", message.contains("element")); assertTrue("Integer", message.contains("Integer")); assertTrue("String", message.contains("String")); }
/** * Ensures that we can not add null elements. */ @Test public void testAddAllNull() { final CheckedArrayList<String> list = new CheckedArrayList<>(String.class); final Collection<String> toAdd = Arrays.asList("One", null, "Three"); try { list.addAll(toAdd); } catch (NullArgumentException e) { final String message = e.getMessage(); assertTrue(message.contains("CheckedArrayList<String>")); } }
/** * 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>")); } }
/** * 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); }
/** * 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); }
/** * 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. }