/** * Appends the specified element to the end of this list. * * @param element element to be appended to this list. * @return always {@code true}. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public boolean add(final E element) { if (ensureValid(element)) { return super.add(element); } return false; }
/** * Inserts the specified element at the specified position in this list. * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * @throws IndexOutOfBoundsException if index out of range. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public void add(final int index, final E element) { if (ensureValid(element)) { super.add(index, element); } }
/** * Appends the specified element to the end of this list. * * @param element element to be appended to this list. * @return always {@code true}. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public boolean add(final E element) { if (ensureValid(element)) { return super.add(element); } return false; }
/** * Inserts the specified element at the specified position in this list. * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * @throws IndexOutOfBoundsException if index out of range. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public void add(final int index, final E element) { if (ensureValid(element)) { super.add(index, element); } }
/** * Replaces the element at the specified position in this list with the specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @throws IndexOutOfBoundsException if index out of range. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public E set(final int index, final E element) { if (ensureValid(element)) { return super.set(index, element); } return get(index); }
/** * Replaces the element at the specified position in this list with the specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @throws IndexOutOfBoundsException if index out of range. * @throws NullPointerException if the given element is {@code null}. * @throws ClassCastException if the given element is not of the expected type. */ @Override public E set(final int index, final E element) { if (ensureValid(element)) { return super.set(index, element); } return get(index); }
/** * Ensures that all elements of the given collection can be added to this list. * * @param collection the collection to check, or {@code null}. * @return the potentially filtered collection of elements to add. * @throws NullPointerException if an element is {@code null}. * @throws ClassCastException if an element is not of the expected type. */ @SuppressWarnings("unchecked") private List<E> ensureValidCollection(final Collection<? extends E> collection) { int count = 0; final Object[] array = collection.toArray(); for (final Object element : array) { if (ensureValid((E) element)) { array[count++] = element; } } // Not-so-unsafe cast: we verified in the above loop that all elements are instance of E. // The array itself may not be an instance of E[], but this is not important for Mediator. return new Mediator<>(ArraysExt.resize((E[]) array, count)); }
/** * Ensures that all elements of the given collection can be added to this list. * * @param collection the collection to check, or {@code null}. * @return the potentially filtered collection of elements to add. * @throws NullPointerException if an element is {@code null}. * @throws ClassCastException if an element is not of the expected type. */ @SuppressWarnings("unchecked") private List<E> ensureValidCollection(final Collection<? extends E> collection) { int count = 0; final Object[] array = collection.toArray(); for (final Object element : array) { if (ensureValid((E) element)) { array[count++] = element; } } // Not-so-unsafe cast: we verified in the above loop that all elements are instance of E. // The array itself may not be an instance of E[], but this is not important for Mediator. return new Mediator<>(ArraysExt.resize((E[]) array, count)); }