/** * Returns the given collection as a {@code CheckedArrayList} instance of the given element type. * * @param <E> the element type. * @param collection the collection or {@code null}. * @param type the element type. * @return the given collection as a {@code CheckedArrayList}, or {@code null} if the given collection was null. * @throws ClassCastException if an element is not of the expected type. * * @since 0.5 */ @SuppressWarnings("unchecked") public static <E> CheckedArrayList<E> castOrCopy(final Collection<?> collection, final Class<E> type) { if (collection == null) { return null; } if (collection instanceof CheckedArrayList<?> && ((CheckedArrayList<?>) collection).type == type) { return (CheckedArrayList<E>) collection; } else { final CheckedArrayList<E> list = new CheckedArrayList<>(type, collection.size()); list.addAll((Collection) collection); // addAll will perform the type checks. return list; } }
/** * Returns the given collection as a {@code CheckedArrayList} instance of the given element type. * * @param <E> the element type. * @param collection the collection or {@code null}. * @param type the element type. * @return the given collection as a {@code CheckedArrayList}, or {@code null} if the given collection was null. * @throws ClassCastException if an element is not of the expected type. * * @since 0.5 */ @SuppressWarnings("unchecked") public static <E> CheckedArrayList<E> castOrCopy(final Collection<?> collection, final Class<E> type) { if (collection == null) { return null; } if (collection instanceof CheckedArrayList<?> && ((CheckedArrayList<?>) collection).type == type) { return (CheckedArrayList<E>) collection; } else { final CheckedArrayList<E> list = new CheckedArrayList<>(type, collection.size()); list.addAll((Collection) collection); // addAll will perform the type checks. return list; } }
/** * Sets the attribute values. All previous values are replaced by the given collection. * * @param newValues the new values. */ @Override public void setValues(final Collection<? extends V> newValues) { if (newValues != values) { ArgumentChecks.ensureNonNull("values", newValues); // The parameter name in public API is "values". values.clear(); values.addAll(newValues); } }
/** * Sets the attribute values. All previous values are replaced by the given collection. * * @param newValues the new values. */ @Override public void setValues(final Collection<? extends V> newValues) { if (newValues != values) { ArgumentChecks.ensureNonNull("values", newValues); // The parameter name in public API is "values". values.clear(); values.addAll(newValues); } }
/** * 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>")); } }
/** * 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); }