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.collections.collection;
18
19 import java.util.Collection;
20
21 import org.apache.commons.collections.Predicate;
22
23 /**
24 * Decorates another {@link Collection} to validate that additions
25 * match a specified predicate.
26 * <p>
27 * This collection exists to provide validation for the decorated collection.
28 * It is normally created to decorate an empty collection.
29 * If an object cannot be added to the collection, an IllegalArgumentException is thrown.
30 * <p>
31 * One usage would be to ensure that no null entries are added to the collection.
32 * <pre>Collection coll = PredicatedCollection.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
33 * <p>
34 * This class is Serializable from Commons Collections 3.1.
35 *
36 * @param <E> the type of the elements in the collection
37 * @since 3.0
38 * @version $Id: PredicatedCollection.java 1429905 2013-01-07 17:15:14Z ggregory $
39 */
40 public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
41
42 /** Serialization version */
43 private static final long serialVersionUID = -5259182142076705162L;
44
45 /** The predicate to use */
46 protected final Predicate<? super E> predicate;
47
48 /**
49 * Factory method to create a predicated (validating) collection.
50 * <p>
51 * If there are any elements already in the collection being decorated, they
52 * are validated.
53 *
54 * @param <T> the type of the elements in the collection
55 * @param coll the collection to decorate, must not be null
56 * @param predicate the predicate to use for validation, must not be null
57 * @return a new predicated collection
58 * @throws IllegalArgumentException if collection or predicate is null
59 * @throws IllegalArgumentException if the collection contains invalid elements
60 */
61 public static <T> PredicatedCollection<T> predicatedCollection(final Collection<T> coll,
62 final Predicate<? super T> predicate) {
63 return new PredicatedCollection<T>(coll, predicate);
64 }
65
66 //-----------------------------------------------------------------------
67 /**
68 * Constructor that wraps (not copies).
69 * <p>
70 * If there are any elements already in the collection being decorated, they
71 * are validated.
72 *
73 * @param coll the collection to decorate, must not be null
74 * @param predicate the predicate to use for validation, must not be null
75 * @throws IllegalArgumentException if collection or predicate is null
76 * @throws IllegalArgumentException if the collection contains invalid elements
77 */
78 protected PredicatedCollection(final Collection<E> coll, final Predicate<? super E> predicate) {
79 super(coll);
80 if (predicate == null) {
81 throw new IllegalArgumentException("Predicate must not be null");
82 }
83 this.predicate = predicate;
84 for (final E item : coll) {
85 validate(item);
86 }
87 }
88
89 /**
90 * Validates the object being added to ensure it matches the predicate.
91 * <p>
92 * The predicate itself should not throw an exception, but return false to
93 * indicate that the object cannot be added.
94 *
95 * @param object the object being added
96 * @throws IllegalArgumentException if the add is invalid
97 */
98 protected void validate(final E object) {
99 if (predicate.evaluate(object) == false) {
100 throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" +
101 predicate + "' rejected it");
102 }
103 }
104
105 //-----------------------------------------------------------------------
106 /**
107 * Override to validate the object being added to ensure it matches
108 * the predicate.
109 *
110 * @param object the object being added
111 * @return the result of adding to the underlying collection
112 * @throws IllegalArgumentException if the add is invalid
113 */
114 @Override
115 public boolean add(final E object) {
116 validate(object);
117 return decorated().add(object);
118 }
119
120 /**
121 * Override to validate the objects being added to ensure they match
122 * the predicate. If any one fails, no update is made to the underlying
123 * collection.
124 *
125 * @param coll the collection being added
126 * @return the result of adding to the underlying collection
127 * @throws IllegalArgumentException if the add is invalid
128 */
129 @Override
130 public boolean addAll(final Collection<? extends E> coll) {
131 for (final E item : coll) {
132 validate(item);
133 }
134 return decorated().addAll(coll);
135 }
136
137 }