PredicatedList.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.list;

  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.ListIterator;

  21. import org.apache.commons.collections4.Predicate;
  22. import org.apache.commons.collections4.collection.PredicatedCollection;
  23. import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;

  24. /**
  25.  * Decorates another {@code List} to validate that all additions
  26.  * match a specified predicate.
  27.  * <p>
  28.  * This list exists to provide validation for the decorated list.
  29.  * It is normally created to decorate an empty list.
  30.  * If an object cannot be added to the list, an IllegalArgumentException is thrown.
  31.  * </p>
  32.  * <p>
  33.  * One usage would be to ensure that no null entries are added to the list.
  34.  * </p>
  35.  * <pre>
  36.  * {@code
  37.  * List<String> list =
  38.  *   PredicatedList.predicatedList(new ArrayList<String>(), PredicateUtils.notNullPredicate());
  39.  * }
  40.  * </pre>
  41.  * <p>
  42.  * This class is Serializable from Commons Collections 3.1.
  43.  * </p>
  44.  *
  45.  * @param <E> the type of the elements in the list.
  46.  * @since 3.0
  47.  */
  48. public class PredicatedList<E> extends PredicatedCollection<E> implements List<E> {

  49.     /**
  50.      * Inner class Iterator for the PredicatedList
  51.      */
  52.     protected class PredicatedListIterator extends AbstractListIteratorDecorator<E> {

  53.         /**
  54.          * Create a new predicated list iterator.
  55.          *
  56.          * @param iterator  the list iterator to decorate
  57.          */
  58.         protected PredicatedListIterator(final ListIterator<E> iterator) {
  59.             super(iterator);
  60.         }

  61.         @Override
  62.         public void add(final E object) {
  63.             validate(object);
  64.             getListIterator().add(object);
  65.         }

  66.         @Override
  67.         public void set(final E object) {
  68.             validate(object);
  69.             getListIterator().set(object);
  70.         }
  71.     }

  72.     /** Serialization version */
  73.     private static final long serialVersionUID = -5722039223898659102L;

  74.     /**
  75.      * Factory method to create a predicated (validating) list.
  76.      * <p>
  77.      * If there are any elements already in the list being decorated, they
  78.      * are validated.
  79.      *
  80.      * @param <T> the type of the elements in the list
  81.      * @param list  the list to decorate, must not be null
  82.      * @param predicate  the predicate to use for validation, must not be null
  83.      * @return a new predicated list
  84.      * @throws NullPointerException if list or predicate is null
  85.      * @throws IllegalArgumentException if the list contains invalid elements
  86.      * @since 4.0
  87.      */
  88.     public static <T> PredicatedList<T> predicatedList(final List<T> list, final Predicate<? super T> predicate) {
  89.         return new PredicatedList<>(list, predicate);
  90.     }

  91.     /**
  92.      * Constructor that wraps (not copies).
  93.      * <p>
  94.      * If there are any elements already in the list being decorated, they
  95.      * are validated.
  96.      *
  97.      * @param list  the list to decorate, must not be null
  98.      * @param predicate  the predicate to use for validation, must not be null
  99.      * @throws NullPointerException if list or predicate is null
  100.      * @throws IllegalArgumentException if the list contains invalid elements
  101.      */
  102.     protected PredicatedList(final List<E> list, final Predicate<? super E> predicate) {
  103.         super(list, predicate);
  104.     }

  105.     @Override
  106.     public void add(final int index, final E object) {
  107.         validate(object);
  108.         decorated().add(index, object);
  109.     }

  110.     @Override
  111.     public boolean addAll(final int index, final Collection<? extends E> coll) {
  112.         for (final E aColl : coll) {
  113.             validate(aColl);
  114.         }
  115.         return decorated().addAll(index, coll);
  116.     }

  117.     /**
  118.      * Gets the list being decorated.
  119.      *
  120.      * @return the decorated list
  121.      */
  122.     @Override
  123.     protected List<E> decorated() {
  124.         return (List<E>) super.decorated();
  125.     }

  126.     @Override
  127.     public boolean equals(final Object object) {
  128.         return object == this || decorated().equals(object);
  129.     }

  130.     @Override
  131.     public E get(final int index) {
  132.         return decorated().get(index);
  133.     }

  134.     @Override
  135.     public int hashCode() {
  136.         return decorated().hashCode();
  137.     }

  138.     @Override
  139.     public int indexOf(final Object object) {
  140.         return decorated().indexOf(object);
  141.     }

  142.     @Override
  143.     public int lastIndexOf(final Object object) {
  144.         return decorated().lastIndexOf(object);
  145.     }

  146.     @Override
  147.     public ListIterator<E> listIterator() {
  148.         return listIterator(0);
  149.     }

  150.     @Override
  151.     public ListIterator<E> listIterator(final int i) {
  152.         return new PredicatedListIterator(decorated().listIterator(i));
  153.     }

  154.     @Override
  155.     public E remove(final int index) {
  156.         return decorated().remove(index);
  157.     }

  158.     @Override
  159.     public E set(final int index, final E object) {
  160.         validate(object);
  161.         return decorated().set(index, object);
  162.     }

  163.     @Override
  164.     public List<E> subList(final int fromIndex, final int toIndex) {
  165.         final List<E> sub = decorated().subList(fromIndex, toIndex);
  166.         return new PredicatedList<>(sub, predicate);
  167.     }

  168. }