PredicatedCollection.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.collection;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashSet;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.Queue;
  26. import java.util.Set;

  27. import org.apache.commons.collections4.Bag;
  28. import org.apache.commons.collections4.MultiSet;
  29. import org.apache.commons.collections4.Predicate;
  30. import org.apache.commons.collections4.bag.HashBag;
  31. import org.apache.commons.collections4.bag.PredicatedBag;
  32. import org.apache.commons.collections4.functors.NotNullPredicate;
  33. import org.apache.commons.collections4.list.PredicatedList;
  34. import org.apache.commons.collections4.multiset.HashMultiSet;
  35. import org.apache.commons.collections4.multiset.PredicatedMultiSet;
  36. import org.apache.commons.collections4.queue.PredicatedQueue;
  37. import org.apache.commons.collections4.set.PredicatedSet;

  38. /**
  39.  * Decorates another {@link Collection} to validate that additions
  40.  * match a specified predicate.
  41.  * <p>
  42.  * This collection exists to provide validation for the decorated collection.
  43.  * It is normally created to decorate an empty collection.
  44.  * If an object cannot be added to the collection, an IllegalArgumentException is thrown.
  45.  * </p>
  46.  * <p>
  47.  * One usage would be to ensure that no null entries are added to the collection:
  48.  * </p>
  49.  * <pre>
  50.  * Collection coll = PredicatedCollection.predicatedCollection(new ArrayList(), NotNullPredicate.INSTANCE);
  51.  * </pre>
  52.  * <p>
  53.  * This class is Serializable from Commons Collections 3.1.
  54.  * </p>
  55.  *
  56.  * @param <E> the type of the elements in the collection
  57.  * @since 3.0
  58.  */
  59. public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {

  60.     /**
  61.      * Builder for creating predicated collections.
  62.      * <p>
  63.      * Create a Builder with a predicate to validate elements against, then add any elements
  64.      * to the builder. Elements that fail the predicate will be added to a rejected list.
  65.      * Finally, create or decorate a collection using the createPredicated[List,Set,Bag,Queue] methods.
  66.      * <p>
  67.      * An example:
  68.      * <pre>
  69.      *   Predicate&lt;String&gt; predicate = NotNullPredicate.notNullPredicate();
  70.      *   PredicatedCollectionBuilder&lt;String&gt; builder = PredicatedCollection.builder(predicate);
  71.      *   builder.add("item1");
  72.      *   builder.add(null);
  73.      *   builder.add("item2");
  74.      *   List&lt;String&gt; predicatedList = builder.createPredicatedList();
  75.      * </pre>
  76.      * <p>
  77.      * At the end of the code fragment above predicatedList is protected by the predicate supplied
  78.      * to the builder, and it contains item1 and item2.
  79.      * <p>
  80.      * More elements can be added to the builder once a predicated collection has been created,
  81.      * but these elements will not be reflected in already created collections.
  82.      *
  83.      * @param <E>  the element type
  84.      * @since 4.1
  85.      */
  86.     public static class Builder<E> {

  87.         /** The predicate to use. */
  88.         private final Predicate<? super E> predicate;

  89.         /** The buffer containing valid elements. */
  90.         private final List<E> accepted = new ArrayList<>();

  91.         /** The buffer containing rejected elements. */
  92.         private final List<E> rejected = new ArrayList<>();

  93.         /**
  94.          * Constructs a PredicatedCollectionBuilder with the specified Predicate.
  95.          *
  96.          * @param predicate  the predicate to use
  97.          * @throws NullPointerException if predicate is null
  98.          */
  99.         public Builder(final Predicate<? super E> predicate) {
  100.             this.predicate = Objects.requireNonNull(predicate, "predicate");
  101.         }

  102.         /**
  103.          * Adds the item to the builder.
  104.          * <p>
  105.          * If the predicate is true, it is added to the list of accepted elements,
  106.          * otherwise it is added to the rejected list.
  107.          *
  108.          * @param item  the element to add
  109.          * @return the PredicatedCollectionBuilder.
  110.          */
  111.         public Builder<E> add(final E item) {
  112.             if (predicate.test(item)) {
  113.                 accepted.add(item);
  114.             } else {
  115.                 rejected.add(item);
  116.             }
  117.             return this;
  118.         }

  119.         /**
  120.          * Adds all elements from the given collection to the builder.
  121.          * <p>
  122.          * All elements for which the predicate evaluates to true will be added to the
  123.          * list of accepted elements, otherwise they are added to the rejected list.
  124.          *
  125.          * @param items  the elements to add to the builder
  126.          * @return the PredicatedCollectionBuilder.
  127.          */
  128.         public Builder<E> addAll(final Collection<? extends E> items) {
  129.             if (items != null) {
  130.                 for (final E item : items) {
  131.                     add(item);
  132.                 }
  133.             }
  134.             return this;
  135.         }

  136.         /**
  137.          * Create a new predicated bag filled with the accepted elements.
  138.          * <p>
  139.          * The builder is not modified by this method, so it is possible to create more collections
  140.          * or add more elements afterwards. Further changes will not propagate to the returned bag.
  141.          *
  142.          * @return a new predicated bag.
  143.          */
  144.         public Bag<E> createPredicatedBag() {
  145.             return createPredicatedBag(new HashBag<>());
  146.         }

  147.         /**
  148.          * Decorates the given bag with validating behavior using the predicate. All accepted elements
  149.          * are appended to the bag. If the bag already contains elements, they are validated.
  150.          * <p>
  151.          * The builder is not modified by this method, so it is possible to create more collections
  152.          * or add more elements afterwards. Further changes will not propagate to the returned bag.
  153.          *
  154.          * @param bag  the bag to decorate, must not be null
  155.          * @return the decorated bag.
  156.          * @throws NullPointerException if bag is null
  157.          * @throws IllegalArgumentException if bag contains invalid elements
  158.          */
  159.         public Bag<E> createPredicatedBag(final Bag<E> bag) {
  160.             Objects.requireNonNull(bag, "bag");
  161.             final PredicatedBag<E> predicatedBag = PredicatedBag.predicatedBag(bag, predicate);
  162.             predicatedBag.addAll(accepted);
  163.             return predicatedBag;
  164.         }

  165.         /**
  166.          * Create a new predicated list filled with the accepted elements.
  167.          * <p>
  168.          * The builder is not modified by this method, so it is possible to create more collections
  169.          * or add more elements afterwards. Further changes will not propagate to the returned list.
  170.          *
  171.          * @return a new predicated list.
  172.          */
  173.         public List<E> createPredicatedList() {
  174.             return createPredicatedList(new ArrayList<>());
  175.         }

  176.         /**
  177.          * Decorates the given list with validating behavior using the predicate. All accepted elements
  178.          * are appended to the list. If the list already contains elements, they are validated.
  179.          * <p>
  180.          * The builder is not modified by this method, so it is possible to create more collections
  181.          * or add more elements afterwards. Further changes will not propagate to the returned list.
  182.          *
  183.          * @param list  the List to decorate, must not be null
  184.          * @return the decorated list.
  185.          * @throws NullPointerException if list is null
  186.          * @throws IllegalArgumentException if list contains invalid elements
  187.          */
  188.         public List<E> createPredicatedList(final List<E> list) {
  189.             Objects.requireNonNull(list, "list");
  190.             final List<E> predicatedList = PredicatedList.predicatedList(list, predicate);
  191.             predicatedList.addAll(accepted);
  192.             return predicatedList;
  193.         }

  194.         /**
  195.          * Create a new predicated multiset filled with the accepted elements.
  196.          * <p>
  197.          * The builder is not modified by this method, so it is possible to create more collections
  198.          * or add more elements afterwards. Further changes will not propagate to the returned multiset.
  199.          *
  200.          * @return a new predicated multiset.
  201.          */
  202.         public MultiSet<E> createPredicatedMultiSet() {
  203.             return createPredicatedMultiSet(new HashMultiSet<>());
  204.         }

  205.         /**
  206.          * Decorates the given multiset with validating behavior using the predicate. All accepted elements
  207.          * are appended to the multiset. If the multiset already contains elements, they are validated.
  208.          * <p>
  209.          * The builder is not modified by this method, so it is possible to create more collections
  210.          * or add more elements afterwards. Further changes will not propagate to the returned multiset.
  211.          *
  212.          * @param multiset  the multiset to decorate, must not be null
  213.          * @return the decorated multiset.
  214.          * @throws NullPointerException if multiset is null
  215.          * @throws IllegalArgumentException if multiset contains invalid elements
  216.          */
  217.         public MultiSet<E> createPredicatedMultiSet(final MultiSet<E> multiset) {
  218.             Objects.requireNonNull(multiset, "multiset");
  219.             final PredicatedMultiSet<E> predicatedMultiSet =
  220.                     PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
  221.             predicatedMultiSet.addAll(accepted);
  222.             return predicatedMultiSet;
  223.         }

  224.         /**
  225.          * Create a new predicated queue filled with the accepted elements.
  226.          * <p>
  227.          * The builder is not modified by this method, so it is possible to create more collections
  228.          * or add more elements afterwards. Further changes will not propagate to the returned queue.
  229.          *
  230.          * @return a new predicated queue.
  231.          */
  232.         public Queue<E> createPredicatedQueue() {
  233.             return createPredicatedQueue(new LinkedList<>());
  234.         }

  235.         /**
  236.          * Decorates the given queue with validating behavior using the predicate. All accepted elements
  237.          * are appended to the queue. If the queue already contains elements, they are validated.
  238.          * <p>
  239.          * The builder is not modified by this method, so it is possible to create more collections
  240.          * or add more elements afterwards. Further changes will not propagate to the returned queue.
  241.          *
  242.          * @param queue  the queue to decorate, must not be null
  243.          * @return the decorated queue.
  244.          * @throws NullPointerException if queue is null
  245.          * @throws IllegalArgumentException if queue contains invalid elements
  246.          */
  247.         public Queue<E> createPredicatedQueue(final Queue<E> queue) {
  248.             Objects.requireNonNull(queue, "queue");
  249.             final PredicatedQueue<E> predicatedQueue = PredicatedQueue.predicatedQueue(queue, predicate);
  250.             predicatedQueue.addAll(accepted);
  251.             return predicatedQueue;
  252.         }

  253.         /**
  254.          * Create a new predicated set filled with the accepted elements.
  255.          * <p>
  256.          * The builder is not modified by this method, so it is possible to create more collections
  257.          * or add more elements afterwards. Further changes will not propagate to the returned set.
  258.          *
  259.          * @return a new predicated set.
  260.          */
  261.         public Set<E> createPredicatedSet() {
  262.             return createPredicatedSet(new HashSet<>());
  263.         }

  264.         /**
  265.          * Decorates the given list with validating behavior using the predicate. All accepted elements
  266.          * are appended to the set. If the set already contains elements, they are validated.
  267.          * <p>
  268.          * The builder is not modified by this method, so it is possible to create more collections
  269.          * or add more elements afterwards. Further changes will not propagate to the returned set.
  270.          *
  271.          * @param set  the set to decorate, must not be null
  272.          * @return the decorated set.
  273.          * @throws NullPointerException if set is null
  274.          * @throws IllegalArgumentException if set contains invalid elements
  275.          */
  276.         public Set<E> createPredicatedSet(final Set<E> set) {
  277.             Objects.requireNonNull(set, "set");
  278.             final PredicatedSet<E> predicatedSet = PredicatedSet.predicatedSet(set, predicate);
  279.             predicatedSet.addAll(accepted);
  280.             return predicatedSet;
  281.         }

  282.         /**
  283.          * Returns an unmodifiable collection containing all rejected elements.
  284.          *
  285.          * @return an unmodifiable collection
  286.          */
  287.         public Collection<E> rejectedElements() {
  288.             return Collections.unmodifiableCollection(rejected);
  289.         }

  290.     }

  291.     /** Serialization version */
  292.     private static final long serialVersionUID = -5259182142076705162L;

  293.     /**
  294.      * Returns a Builder with the given predicate.
  295.      *
  296.      * @param <E>  the element type
  297.      * @param predicate  the predicate to use
  298.      * @return a new Builder for predicated collections
  299.      * @since 4.1
  300.      */
  301.     public static <E> Builder<E> builder(final Predicate<? super E> predicate) {
  302.         return new Builder<>(predicate);
  303.     }

  304.     /**
  305.      * Returns a Builder with a NotNullPredicate.
  306.      *
  307.      * @param <E>  the element type
  308.      * @return a new Builder for predicated collections that ignores null values.
  309.      * @since 4.1
  310.      */
  311.     public static <E> Builder<E> notNullBuilder() {
  312.         return new Builder<>(NotNullPredicate.<E>notNullPredicate());
  313.     }

  314.     /**
  315.      * Factory method to create a predicated (validating) collection.
  316.      * <p>
  317.      * If there are any elements already in the collection being decorated, they
  318.      * are validated.
  319.      *
  320.      * @param <T> the type of the elements in the collection
  321.      * @param coll  the collection to decorate, must not be null
  322.      * @param predicate  the predicate to use for validation, must not be null
  323.      * @return a new predicated collection
  324.      * @throws NullPointerException if collection or predicate is null
  325.      * @throws IllegalArgumentException if the collection contains invalid elements
  326.      * @since 4.0
  327.      */
  328.     public static <T> PredicatedCollection<T> predicatedCollection(final Collection<T> coll,
  329.                                                                    final Predicate<? super T> predicate) {
  330.         return new PredicatedCollection<>(coll, predicate);
  331.     }

  332.     /** The predicate to use */
  333.     protected final Predicate<? super E> predicate;

  334.     /**
  335.      * Constructor that wraps (not copies).
  336.      * <p>
  337.      * If there are any elements already in the collection being decorated, they
  338.      * are validated.
  339.      *
  340.      * @param collection  the collection to decorate, must not be null
  341.      * @param predicate  the predicate to use for validation, must not be null
  342.      * @throws NullPointerException if collection or predicate is null
  343.      * @throws IllegalArgumentException if the collection contains invalid elements
  344.      */
  345.     protected PredicatedCollection(final Collection<E> collection, final Predicate<? super E> predicate) {
  346.         super(collection);
  347.         this.predicate = Objects.requireNonNull(predicate, "predicate");
  348.         for (final E item : collection) {
  349.             validate(item);
  350.         }
  351.     }

  352.     /**
  353.      * Override to validate the object being added to ensure it matches
  354.      * the predicate.
  355.      *
  356.      * @param object  the object being added
  357.      * @return the result of adding to the underlying collection
  358.      * @throws IllegalArgumentException if the add is invalid
  359.      */
  360.     @Override
  361.     public boolean add(final E object) {
  362.         validate(object);
  363.         return decorated().add(object);
  364.     }

  365.     /**
  366.      * Override to validate the objects being added to ensure they match
  367.      * the predicate. If anyone fails, no update is made to the underlying
  368.      * collection.
  369.      *
  370.      * @param coll  the collection being added
  371.      * @return the result of adding to the underlying collection
  372.      * @throws IllegalArgumentException if the add is invalid
  373.      */
  374.     @Override
  375.     public boolean addAll(final Collection<? extends E> coll) {
  376.         for (final E item : coll) {
  377.             validate(item);
  378.         }
  379.         return decorated().addAll(coll);
  380.     }

  381.     /**
  382.      * Validates the object being added to ensure it matches the predicate.
  383.      * <p>
  384.      * The predicate itself should not throw an exception, but return false to
  385.      * indicate that the object cannot be added.
  386.      *
  387.      * @param object  the object being added
  388.      * @throws IllegalArgumentException if the add is invalid
  389.      */
  390.     protected void validate(final E object) {
  391.         if (!predicate.test(object)) {
  392.             throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" +
  393.                                                predicate + "' rejected it");
  394.         }
  395.     }

  396. }