KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TypedMap


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.map;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.commons.collections.functors.InstanceofPredicate;
21
22 /**
23  * Decorates another <code>Map</code> to validate that elements added
24  * 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  * <p>
30  * The returned implementation is Serializable from Commons Collections 3.1.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.6 $ $Date: 2004/05/07 23:17:13 $
34  *
35  * @author Stephen Colebourne
36  * @author Matthew Hawthorne
37  */

38 public class TypedMap {
39
40     /**
41      * Factory method to create a typed map.
42      * <p>
43      * If there are any elements already in the map being decorated, they
44      * are validated.
45      *
46      * @param map the map to decorate, must not be null
47      * @param keyType the type to allow as keys, must not be null
48      * @param valueType the type to allow as values, must not be null
49      * @throws IllegalArgumentException if list or type is null
50      * @throws IllegalArgumentException if the list contains invalid elements
51      */

52     public static Map JavaDoc decorate(Map JavaDoc map, Class JavaDoc keyType, Class JavaDoc valueType) {
53         return new PredicatedMap(
54             map,
55             InstanceofPredicate.getInstance(keyType),
56             InstanceofPredicate.getInstance(valueType)
57         );
58     }
59
60     /**
61      * Restrictive constructor.
62      */

63     protected TypedMap() {
64     }
65
66 }
67
Popular Tags