/** * Returns the given value casted to a {@code Set} of elements of the given type. * It is caller responsibility to ensure that the cast is valid, as element type * is verified only when assertions are enabled. */ @SuppressWarnings("unchecked") private static <E> Set<E> cast(final Object value, final Class<E> type) { assert ((CheckedContainer<?>) value).getElementType() == type; return (Set<E>) value; }
/** * Returns the given value casted to a {@code Set} of elements of the given type. * It is caller responsibility to ensure that the cast is valid, as element type * is verified only when assertions are enabled. */ @SuppressWarnings("unchecked") private static <E> Set<E> cast(final Object value, final Class<E> type) { assert ((CheckedContainer<?>) value).getElementType() == type; return (Set<E>) value; }
/** * Returns an assertion error message for mismatched collections. */ private static String mismatchedElement(final String header, final Iterable<?> object1, final Iterable<?> object2, final String tail) { Class<?> type = null; if (object1 instanceof CheckedContainer<?>) { type = ((CheckedContainer<?>) object1).getElementType(); } if (type == null && object2 instanceof CheckedContainer<?>) { type = ((CheckedContainer<?>) object2).getElementType(); } return header + '<' + (type != null ? type.getSimpleName() : "?") + ">: " + tail + " not equals."; }
/** * Returns an assertion error message for mismatched collections. */ private static String mismatchedElement(final String header, final Iterable<?> object1, final Iterable<?> object2, final String tail) { Class<?> type = null; if (object1 instanceof CheckedContainer<?>) { type = ((CheckedContainer<?>) object1).getElementType(); } if (type == null && object2 instanceof CheckedContainer<?>) { type = ((CheckedContainer<?>) object2).getElementType(); } return header + '<' + (type != null ? type.getSimpleName() : "?") + ">: " + tail + " not equal."; }
/** * Computes the single CRS list on deserialization. * * @param in the input stream from which to deserialize a compound CRS. * @throws IOException if an I/O error occurred while reading or if the stream contains invalid data. * @throws ClassNotFoundException if the class serialized on the stream is not on the classpath. */ @SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final List<? extends CoordinateReferenceSystem> components = this.components; if (components instanceof CheckedContainer<?>) { final Class<?> type = ((CheckedContainer<?>) components).getElementType(); if (type == SingleCRS.class) { singles = (List<SingleCRS>) components; return; } } setSingleComponents(components); }
/** * Computes the single CRS list on deserialization. * * @param in the input stream from which to deserialize a compound CRS. * @throws IOException if an I/O error occurred while reading or if the stream contains invalid data. * @throws ClassNotFoundException if the class serialized on the stream is not on the classpath. */ @SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final List<? extends CoordinateReferenceSystem> components = this.components; if (components instanceof CheckedContainer<?>) { final Class<?> type = ((CheckedContainer<?>) components).getElementType(); if (type == SingleCRS.class) { singles = (List<SingleCRS>) components; return; } } setSingleComponents(components); }
final E[] array = (E[]) Array.newInstance(((CheckedContainer<E>) list).getElementType(), length); list = UnmodifiableArrayList.wrap(list.toArray(array)); } else if (list instanceof List<?>) {
final E[] array = (E[]) Array.newInstance(((CheckedContainer<E>) collection).getElementType(), length); collection = UnmodifiableArrayList.wrap(collection.toArray(array)); } else if (collection instanceof List<?>) {
/** * Returns the values as a collection. This method tries to verify that the collection * contains elements of the expected type, but this verification is not always possible. * Consequently this method may, sometime, be actually unsafe. */ @SuppressWarnings("unchecked") // Actually not 100% safe, but we have done our best. static <V> Collection<V> getValues(final AbstractFeature feature, final String name, final Class<V> expected) { final Object values = feature.getPropertyValue(name); if (values instanceof Collection<?>) { if (values instanceof CheckedContainer<?>) { final Class<?> actual = ((CheckedContainer<?>) values).getElementType(); if (expected != actual) { // Really exact match, not Class.isAssignableFrom(Class). throw new ClassCastException(Errors.format(Errors.Keys.UnexpectedTypeForReference_3, name, expected, actual)); } } return (Collection<V>) values; } else { return singletonOrEmpty(expected.cast(values)); } }
/** * Returns the values as a collection. This method tries to verify that the collection * contains elements of the expected type, but this verification is not always possible. * Consequently this method may, sometime, be actually unsafe. */ @SuppressWarnings("unchecked") // Actually not 100% safe, but we have done our best. static <V> Collection<V> getValues(final AbstractFeature feature, final String name, final Class<V> expected) { final Object values = feature.getPropertyValue(name); if (values instanceof Collection<?>) { if (values instanceof CheckedContainer<?>) { final Class<?> actual = ((CheckedContainer<?>) values).getElementType(); if (expected != actual) { // Really exact match, not Class.isAssignableFrom(Class). throw new ClassCastException(Errors.format(Errors.Keys.UnexpectedTypeForReference_3, name, expected, actual)); } } return (Collection<V>) values; } else { return singletonOrEmpty(expected.cast(values)); } }
targetType = ((CheckedContainer<?>) values).getElementType(); } else { targetType = baseType;
targetType = ((CheckedContainer<?>) values).getElementType(); } else { targetType = baseType;
/** * 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 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)); } } }
if (addTo instanceof CheckedContainer<?>) { elementType = ((CheckedContainer<?>) addTo).getElementType();
if (addTo instanceof CheckedContainer<?>) { elementType = ((CheckedContainer<?>) addTo).getElementType();
/** * Validates a property information for {@link Citation#getTitle()}. * This is validation code to be shared with {@link PropertyAccessorTest#testInformation()}. */ static void validateTitle(final ExtendedElementInformation information) { assertParentIsCitation(information); assertEquals("title", information.getName()); final InternationalString definition = information.getDefinition(); assertEquals("Name by which the cited resource is known.", definition.toString(Locale.ENGLISH)); // Test other locale here, if any. assertInstanceOf("Specific to SIS implementation.", CheckedContainer.class, information); assertEquals(InternationalString.class, ((CheckedContainer<?>) information).getElementType()); assertEquals(Datatype.CHARACTER_STRING, information.getDataType()); assertEquals(Obligation.MANDATORY, information.getObligation()); assertEquals(Integer.valueOf(1), information.getMaximumOccurrence()); assertNull(information.getDomainValue()); }
/** * Validates information for {@link Citation#getPresentationForms()}. * This is validation code to be shared with {@link PropertyAccessorTest#testInformation()}. */ static void validatePresentationForm(final ExtendedElementInformation information) { assertParentIsCitation(information); assertEquals("presentationForm", information.getName()); final InternationalString definition = information.getDefinition(); assertEquals("Mode in which the resource is represented.", definition.toString(Locale.ENGLISH)); // Test other locale here, if any. assertInstanceOf("Specific to SIS implementation.", CheckedContainer.class, information); assertEquals(PresentationForm.class, ((CheckedContainer<?>) information).getElementType()); assertEquals(Datatype.CODE_LIST, information.getDataType()); assertEquals(Obligation.OPTIONAL, information.getObligation()); assertEquals(Integer.valueOf(Integer.MAX_VALUE), information.getMaximumOccurrence()); assertNull(information.getDomainValue()); }
if (value instanceof CheckedContainer<?>) { assertTrue("Wrong element type in collection.", elementType.isAssignableFrom(((CheckedContainer<?>) value).getElementType()));