01: /*
02: * Copyright 2003-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.set;
17:
18: import java.util.Set;
19:
20: import org.apache.commons.collections.Predicate;
21: import org.apache.commons.collections.collection.PredicatedCollection;
22:
23: /**
24: * Decorates another <code>Set</code> to validate that all additions
25: * match a specified predicate.
26: * <p>
27: * This set exists to provide validation for the decorated set.
28: * It is normally created to decorate an empty set.
29: * If an object cannot be added to the set, an IllegalArgumentException is thrown.
30: * <p>
31: * One usage would be to ensure that no null entries are added to the set.
32: * <pre>Set set = PredicatedSet.decorate(new HashSet(), NotNullPredicate.INSTANCE);</pre>
33: * <p>
34: * This class is Serializable from Commons Collections 3.1.
35: *
36: * @since Commons Collections 3.0
37: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
38: *
39: * @author Stephen Colebourne
40: * @author Paul Jack
41: */
42: public class PredicatedSet extends PredicatedCollection implements Set {
43:
44: /** Serialization version */
45: private static final long serialVersionUID = -684521469108685117L;
46:
47: /**
48: * Factory method to create a predicated (validating) set.
49: * <p>
50: * If there are any elements already in the set being decorated, they
51: * are validated.
52: *
53: * @param set the set to decorate, must not be null
54: * @param predicate the predicate to use for validation, must not be null
55: * @throws IllegalArgumentException if set or predicate is null
56: * @throws IllegalArgumentException if the set contains invalid elements
57: */
58: public static Set decorate(Set set, Predicate predicate) {
59: return new PredicatedSet(set, predicate);
60: }
61:
62: //-----------------------------------------------------------------------
63: /**
64: * Constructor that wraps (not copies).
65: * <p>
66: * If there are any elements already in the set being decorated, they
67: * are validated.
68: *
69: * @param set the set to decorate, must not be null
70: * @param predicate the predicate to use for validation, must not be null
71: * @throws IllegalArgumentException if set or predicate is null
72: * @throws IllegalArgumentException if the set contains invalid elements
73: */
74: protected PredicatedSet(Set set, Predicate predicate) {
75: super (set, predicate);
76: }
77:
78: /**
79: * Gets the set being decorated.
80: *
81: * @return the decorated set
82: */
83: protected Set getSet() {
84: return (Set) getCollection();
85: }
86:
87: }
|