@Override public List<E> subList(final int fromIndex, final int toIndex) { final List<E> sub = decorated().subList(fromIndex, toIndex); return new PredicatedList<>(sub, predicate); }
@Override public ListIterator<E> listIterator() { return listIterator(0); }
/** * Returns a predicated (validating) list backed by the given list. * <p> * Only objects that pass the test in the given predicate can be added to the list. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original list after invoking this method, * as it is a backdoor for adding invalid objects. * * @param <E> the element type * @param list the list to predicate, must not be null * @param predicate the predicate for the list, must not be null * @return a predicated list backed by the given list * @throws NullPointerException if the List or Predicate is null */ public static <E> List<E> predicatedList(final List<E> list, final Predicate<E> predicate) { return PredicatedList.predicatedList(list, predicate); }
@Override public int hashCode() { return decorated().hashCode(); }
/** * Factory method to create a predicated (validating) list. * <p> * If there are any elements already in the list being decorated, they * are validated. * * @param <T> the type of the elements in the list * @param list the list to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated list * @throws NullPointerException if list or predicate is null * @throws IllegalArgumentException if the list contains invalid elements * @since 4.0 */ public static <T> PredicatedList<T> predicatedList(final List<T> list, final Predicate<? super T> predicate) { return new PredicatedList<>(list, predicate); }
@Override public E remove(final int index) { return decorated().remove(index); }
@Override public boolean equals(final Object object) { return object == this || decorated().equals(object); }
/** * Decorates the given list with validating behavior using the predicate. All accepted elements * are appended to the list. If the list already contains elements, they are validated. * <p> * The builder is not modified by this method, so it is possible to create more collections * or add more elements afterwards. Further changes will not propagate to the returned list. * * @param list the List to decorate, must not be null * @return the decorated list. * @throws NullPointerException if list is null * @throws IllegalArgumentException if list contains invalid elements */ public List<E> createPredicatedList(final List<E> list) { if (list == null) { throw new NullPointerException("List must not be null."); } final List<E> predicatedList = PredicatedList.predicatedList(list, predicate); predicatedList.addAll(accepted); return predicatedList; }
@Override public E get(final int index) { return decorated().get(index); }
@Override public int indexOf(final Object object) { return decorated().indexOf(object); }
@Override public int lastIndexOf(final Object object) { return decorated().lastIndexOf(object); }
@Override public ListIterator<E> listIterator(final int i) { return new PredicatedListIterator(decorated().listIterator(i)); }