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.set;
18
19 import java.util.Set;
20
21 import org.apache.commons.collections4.Predicate;
22 import org.apache.commons.collections4.collection.PredicatedCollection;
23
24 /**
25 * Decorates another {@code Set} to validate that all additions
26 * match a specified predicate.
27 * <p>
28 * This set exists to provide validation for the decorated set.
29 * It is normally created to decorate an empty set.
30 * If an object cannot be added to the set, an IllegalArgumentException is thrown.
31 * </p>
32 * <p>
33 * One usage would be to ensure that no null entries are added to the set.
34 * </p>
35 * <pre>Set set = PredicatedSet.decorate(new HashSet(), NotNullPredicate.INSTANCE);</pre>
36 * <p>
37 * This class is Serializable from Commons Collections 3.1.
38 * </p>
39 *
40 * @param <E> the type of the elements in this set
41 * @since 3.0
42 */
43 public class PredicatedSet<E> extends PredicatedCollection<E> implements Set<E> {
44
45 /** Serialization version */
46 private static final long serialVersionUID = -684521469108685117L;
47
48 /**
49 * Factory method to create a predicated (validating) set.
50 * <p>
51 * If there are any elements already in the set being decorated, they
52 * are validated.
53 *
54 * @param <E> the element type
55 * @param set the set to decorate, must not be null
56 * @param predicate the predicate to use for validation, must not be null
57 * @return a decorated set
58 * @throws NullPointerException if set or predicate is null
59 * @throws IllegalArgumentException if the set contains invalid elements
60 * @since 4.0
61 */
62 public static <E> PredicatedSet<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate) {
63 return new PredicatedSet<>(set, predicate);
64 }
65
66 /**
67 * Constructor that wraps (not copies).
68 * <p>
69 * If there are any elements already in the set being decorated, they
70 * are validated.
71 *
72 * @param set the set to decorate, must not be null
73 * @param predicate the predicate to use for validation, must not be null
74 * @throws NullPointerException if set or predicate is null
75 * @throws IllegalArgumentException if the set contains invalid elements
76 */
77 protected PredicatedSet(final Set<E> set, final Predicate<? super E> predicate) {
78 super(set, predicate);
79 }
80
81 /**
82 * Gets the set being decorated.
83 *
84 * @return the decorated set
85 */
86 @Override
87 protected Set<E> decorated() {
88 return (Set<E>) super.decorated();
89 }
90
91 @Override
92 public boolean equals(final Object object) {
93 return object == this || decorated().equals(object);
94 }
95
96 @Override
97 public int hashCode() {
98 return decorated().hashCode();
99 }
100
101 }