/** * 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); } }
/** * Verifies the validity of the given attribute value, and returns the value to store in the feature. * An attribute: * <ul> * <li>May be a singleton, in which case the value class is verified.</li> * <li>May be a collection, in which case the class each elements in the collection is verified.</li> * </ul> * * @param value the value, which shall be non-null. */ private static <T> Object verifyAttributeValue(final DefaultAttributeType<T> type, final Object value) { final Class<T> valueClass = type.getValueClass(); final boolean isSingleton = Field.isSingleton(type.getMaximumOccurs()); if (valueClass.isInstance(value)) { return isSingleton ? value : singletonList(valueClass, type.getMinimumOccurs(), value); } else if (!isSingleton && value instanceof Collection<?>) { return CheckedArrayList.castOrCopy((Collection<?>) value, valueClass); } else { throw new ClassCastException(illegalValueClass(type, valueClass, value)); } }
/** * Verifies the validity of the given attribute value, and returns the value to store in the feature. * An attribute: * <ul> * <li>May be a singleton, in which case the value class is verified.</li> * <li>May be a collection, in which case the class each elements in the collection is verified.</li> * </ul> * * @param value the value, which shall be non-null. */ private static <T> Object verifyAttributeValue(final DefaultAttributeType<T> type, final Object value) { final Class<T> valueClass = type.getValueClass(); final boolean isSingleton = Field.isSingleton(type.getMaximumOccurs()); if (valueClass.isInstance(value)) { return isSingleton ? value : singletonList(valueClass, type.getMinimumOccurs(), value); } else if (!isSingleton && value instanceof Collection<?>) { return CheckedArrayList.castOrCopy((Collection<?>) value, valueClass); } else { throw new ClassCastException(illegalValueClass(type, valueClass, value)); } }
return CheckedArrayList.castOrCopy((Collection<?>) value, AbstractFeature.class); } else { throw new ClassCastException(illegalValueClass(role, AbstractFeature.class, value));
return CheckedArrayList.castOrCopy((Collection<?>) value, AbstractFeature.class); } else { throw new ClassCastException(illegalValueClass(role, AbstractFeature.class, value));
/** * Tests {@link CheckedArrayList#castOrCopy(Collection, Class)}. */ @Test @DependsOnMethod("testAddAll") public void testCastOrCopy() { assertNull(CheckedArrayList.castOrCopy(null, String.class)); final List<String> fruits = Arrays.asList("Apple", "Orange", "Raisin"); final CheckedArrayList<String> asStrings = CheckedArrayList.castOrCopy(fruits, String.class); assertEquals ("Should have the given element type.", String.class, asStrings.getElementType()); assertNotSame("Should have created a new instance.", fruits, asStrings); assertEquals ("Should contain the same data.", fruits, asStrings); assertSame ("Should cast existing instance.", asStrings, CheckedArrayList.castOrCopy(asStrings, String.class)); final CheckedArrayList<CharSequence> asChars = CheckedArrayList.castOrCopy(asStrings, CharSequence.class); assertEquals ("Should have the given element type.", CharSequence.class, asChars.getElementType()); assertNotSame("Should have created a new instance.", asStrings, asChars); assertEquals ("Should contain the same data.", asStrings, asChars); assertEquals ("Should contain the same data.", fruits, asChars); try { CheckedArrayList.castOrCopy(asChars, Integer.class); fail("Should not be allowed to cast String to Integer."); } catch (ClassCastException e) { final String message = e.getMessage(); assertTrue(message, message.contains("String")); assertTrue(message, message.contains("Integer")); } } }