001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.collection;
018
019import java.util.Collection;
020
021import org.apache.commons.collections4.Predicate;
022
023/**
024 * Decorates another {@link Collection} to validate that additions
025 * match a specified predicate.
026 * <p>
027 * This collection exists to provide validation for the decorated collection.
028 * It is normally created to decorate an empty collection.
029 * If an object cannot be added to the collection, an IllegalArgumentException is thrown.
030 * <p>
031 * One usage would be to ensure that no null entries are added to the collection.
032 * <pre>Collection coll = PredicatedCollection.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
033 * <p>
034 * This class is Serializable from Commons Collections 3.1.
035 *
036 * @param <E> the type of the elements in the collection
037 * @since 3.0
038 * @version $Id: PredicatedCollection.html 972421 2015-11-14 20:00:04Z tn $
039 */
040public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
041
042    /** Serialization version */
043    private static final long serialVersionUID = -5259182142076705162L;
044
045    /** The predicate to use */
046    protected final Predicate<? super E> predicate;
047
048    /**
049     * Factory method to create a predicated (validating) collection.
050     * <p>
051     * If there are any elements already in the collection being decorated, they
052     * are validated.
053     *
054     * @param <T> the type of the elements in the collection
055     * @param coll  the collection to decorate, must not be null
056     * @param predicate  the predicate to use for validation, must not be null
057     * @return a new predicated collection
058     * @throws IllegalArgumentException if collection or predicate is null
059     * @throws IllegalArgumentException if the collection contains invalid elements
060     * @since 4.0
061     */
062    public static <T> PredicatedCollection<T> predicatedCollection(final Collection<T> coll,
063                                                                   final Predicate<? super T> predicate) {
064        return new PredicatedCollection<T>(coll, predicate);
065    }
066
067    //-----------------------------------------------------------------------
068    /**
069     * Constructor that wraps (not copies).
070     * <p>
071     * If there are any elements already in the collection being decorated, they
072     * are validated.
073     *
074     * @param coll  the collection to decorate, must not be null
075     * @param predicate  the predicate to use for validation, must not be null
076     * @throws IllegalArgumentException if collection or predicate is null
077     * @throws IllegalArgumentException if the collection contains invalid elements
078     */
079    protected PredicatedCollection(final Collection<E> coll, final Predicate<? super E> predicate) {
080        super(coll);
081        if (predicate == null) {
082            throw new IllegalArgumentException("Predicate must not be null");
083        }
084        this.predicate = predicate;
085        for (final E item : coll) {
086            validate(item);
087        }
088    }
089
090    /**
091     * Validates the object being added to ensure it matches the predicate.
092     * <p>
093     * The predicate itself should not throw an exception, but return false to
094     * indicate that the object cannot be added.
095     *
096     * @param object  the object being added
097     * @throws IllegalArgumentException if the add is invalid
098     */
099    protected void validate(final E object) {
100        if (predicate.evaluate(object) == false) {
101            throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" +
102                                               predicate + "' rejected it");
103        }
104    }
105
106    //-----------------------------------------------------------------------
107    /**
108     * Override to validate the object being added to ensure it matches
109     * the predicate.
110     *
111     * @param object  the object being added
112     * @return the result of adding to the underlying collection
113     * @throws IllegalArgumentException if the add is invalid
114     */
115    @Override
116    public boolean add(final E object) {
117        validate(object);
118        return decorated().add(object);
119    }
120
121    /**
122     * Override to validate the objects being added to ensure they match
123     * the predicate. If any one fails, no update is made to the underlying
124     * collection.
125     *
126     * @param coll  the collection being added
127     * @return the result of adding to the underlying collection
128     * @throws IllegalArgumentException if the add is invalid
129     */
130    @Override
131    public boolean addAll(final Collection<? extends E> coll) {
132        for (final E item : coll) {
133            validate(item);
134        }
135        return decorated().addAll(coll);
136    }
137
138}