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.functors.InstanceofPredicate;
21:
22: /**
23: * Decorates another <code>Set</code> to validate that elements
24: * added are of a specific type.
25: * <p>
26: * The validation of additions is performed via an instanceof test against
27: * a specified <code>Class</code>. If an object cannot be added to the
28: * collection, an IllegalArgumentException is thrown.
29: *
30: * @since Commons Collections 3.0
31: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
32: *
33: * @author Stephen Colebourne
34: * @author Matthew Hawthorne
35: */
36: public class TypedSet {
37:
38: /**
39: * Factory method to create a typed set.
40: * <p>
41: * If there are any elements already in the set being decorated, they
42: * are validated.
43: *
44: * @param set the set to decorate, must not be null
45: * @param type the type to allow into the collection, must not be null
46: * @throws IllegalArgumentException if set or type is null
47: * @throws IllegalArgumentException if the set contains invalid elements
48: */
49: public static Set decorate(Set set, Class type) {
50: return new PredicatedSet(set, InstanceofPredicate
51: .getInstance(type));
52: }
53:
54: /**
55: * Restrictive constructor.
56: */
57: protected TypedSet() {
58: }
59:
60: }
|