Class CheckedArrayList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
org.apache.sis.internal.util.CheckedArrayList<E>
Type Parameters:
E - the type of elements in the list.
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess, CheckedContainer<E>

public final class CheckedArrayList<E> extends ArrayList<E> implements CheckedContainer<E>
A checked ArrayList. The type checks are performed at run-time in addition to the compile-time checks.

Using this class is similar to wrapping an ArrayList using the methods provided in the standard Collections class, except for the following differences:

  • Avoid one level of indirection.
  • Does not accept null elements.
The checks are performed only on a best effort basis. In current implementation, holes are known to exist in use cases like sublist(…).set(…) or when using the list iterator.
Since:
0.3
Version:
1.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial version UID for compatibility with different versions.
      See Also:
    • type

      private final Class<E> type
      The element type.
  • Constructor Details

    • CheckedArrayList

      public CheckedArrayList(Class<E> type)
      Constructs a list of the specified type.
      Parameters:
      type - the element type (cannot be null).
    • CheckedArrayList

      public CheckedArrayList(Class<E> type, int capacity)
      Constructs a list of the specified type and initial capacity.
      Parameters:
      type - the element type (should not be null).
      capacity - the initial capacity.
  • Method Details

    • castOrCopy

      public static <E> CheckedArrayList<E> castOrCopy(Collection<?> collection, Class<E> type)
      Returns the given collection as a CheckedArrayList instance of the given element type.
      Type Parameters:
      E - the element type.
      Parameters:
      collection - the collection or null.
      type - the element type.
      Returns:
      the given collection as a CheckedArrayList, or null if the given collection was null.
      Throws:
      ClassCastException - if an element is not of the expected type.
      Since:
      0.5
    • getElementType

      public Class<E> getElementType()
      Returns the element type given at construction time.
      Specified by:
      getElementType in interface CheckedContainer<E>
      Returns:
      the element type.
    • illegalElement

      public static String illegalElement(Collection<?> collection, Object element, Class<?> expectedType)
      Invoked when an illegal element has been given to the add(E) method. The element may be illegal either because null or because of invalid type. This method will perform only one of the following actions:
      • If a unmarshalling process is under way, then this method logs a warning and returns null. The add(E) caller method shall return false without throwing exception. This is a violation of Collection.add(Object) contract, but is required for unmarshalling of empty XML elements (see SIS-139 and SIS-157).
      • If no unmarshalling process is under way, then this method returns a String containing the error message to give to the exception to be thrown. The add(E) caller method is responsible to thrown an exception with that message. We let the caller throw the exception for reducing the stack trace depth, so the first element on the stack trace is the public add(E) method.
      Parameters:
      collection - the collection in which the user attempted to add an invalid element.
      element - the element that the user attempted to add (may be null).
      expectedType - the type of elements that the collection expected.
      Returns:
      the message to give to the exception to be thrown, or null if no message shall be thrown.
      See Also:
    • ensureValid

      private boolean ensureValid(E element)
      Ensures that the given element is non-null and assignable to the type specified at construction time.
      Parameters:
      element - the object to check, or null.
      Returns:
      true if the instance is valid, false if it shall be ignored.
      Throws:
      NullPointerException - if the given element is null.
      ClassCastException - if the given element is not of the expected type.
    • ensureValidCollection

      private List<E> ensureValidCollection(Collection<? extends E> collection)
      Ensures that all elements of the given collection can be added to this list.
      Parameters:
      collection - the collection to check, or null.
      Returns:
      the potentially filtered collection of elements to add.
      Throws:
      NullPointerException - if an element is null.
      ClassCastException - if an element is not of the expected type.
    • set

      public E set(int index, E element)
      Replaces the element at the specified position in this list with the specified element.
      Specified by:
      set in interface List<E>
      Overrides:
      set in class ArrayList<E>
      Parameters:
      index - index of element to replace.
      element - element to be stored at the specified position.
      Returns:
      the element previously at the specified position.
      Throws:
      IndexOutOfBoundsException - if index out of range.
      NullPointerException - if the given element is null.
      ClassCastException - if the given element is not of the expected type.
    • add

      public boolean add(E element)
      Appends the specified element to the end of this list.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      element - element to be appended to this list.
      Returns:
      always true.
      Throws:
      NullPointerException - if the given element is null.
      ClassCastException - if the given element is not of the expected type.
    • add

      public void add(int index, E element)
      Inserts the specified element at the specified position in this list.
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      index - index at which the specified element is to be inserted.
      element - element to be inserted.
      Throws:
      IndexOutOfBoundsException - if index out of range.
      NullPointerException - if the given element is null.
      ClassCastException - if the given element is not of the expected type.
    • addAll

      public boolean addAll(Collection<? extends E> collection)
      Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      collection - the elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      NullPointerException - if an element is null.
      ClassCastException - if an element is not of the expected type.
    • addAll

      public boolean addAll(int index, Collection<? extends E> collection)
      Inserts all of the elements in the specified collection into this list, starting at the specified position.
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      index - index at which to insert first element fromm the specified collection.
      collection - elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      NullPointerException - if an element is null.
      ClassCastException - if an element is not of the expected type.
    • subList

      public List<E> subList(int fromIndex, int toIndex)
      Returns a checked sublist.

      Limitations

      Current implementation checks only the type. It does not prevent the insertion of null values.
      Specified by:
      subList in interface List<E>
      Overrides:
      subList in class ArrayList<E>
      Parameters:
      fromIndex - index of the first element.
      toIndex - index after the last element.
      Returns:
      the sublist in the given index range.