KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > TypedCollection


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.collection;
17
18 import java.util.Collection JavaDoc;
19
20 import org.apache.commons.collections.functors.InstanceofPredicate;
21
22 /**
23  * Decorates a <code>Collection</code> to validate that elements added are of a specific type.
24  * <p>
25  * The validation of additions is performed via an instanceof test against
26  * a specified <code>Class</code>. If an object cannot be added to the
27  * collection, an IllegalArgumentException is thrown.
28  *
29  * @since Commons Collections 3.0
30  * @version $Revision: 1.5 $ $Date: 2004/05/15 12:39:13 $
31  *
32  * @author Stephen Colebourne
33  * @author Matthew Hawthorne
34  */

35 public class TypedCollection {
36
37     /**
38      * Factory method to create a typed collection.
39      * <p>
40      * If there are any elements already in the collection being decorated, they
41      * are validated.
42      *
43      * @param coll the collection to decorate, must not be null
44      * @param type the type to allow into the collection, must not be null
45      * @return a new typed collection
46      * @throws IllegalArgumentException if collection or type is null
47      * @throws IllegalArgumentException if the collection contains invalid elements
48      */

49     public static Collection JavaDoc decorate(Collection JavaDoc coll, Class JavaDoc type) {
50         return new PredicatedCollection(coll, InstanceofPredicate.getInstance(type));
51     }
52     
53     /**
54      * Restrictive constructor.
55      */

56     protected TypedCollection() {
57         super();
58     }
59
60 }
61
Popular Tags