KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 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.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * A case-insensitive <code>Map</code>.
26  * <p>
27  * As entries are added to the map, keys are converted to all lowercase. A new
28  * key is compared to existing keys by comparing <code>newKey.toString().toLower()</code>
29  * to the lowercase values in the current <code>KeySet.</code>
30  * <p>
31  * Null keys are supported.
32  * <p>
33  * The <code>keySet()</code> method returns all lowercase keys, or nulls.
34  * <p>
35  * Example:
36  * <pre><code>
37  * Map map = new CaseInsensitiveMap();
38  * map.put("One", "One");
39  * map.put("Two", "Two");
40  * map.put(null, "Three");
41  * map.put("one", "Four");
42  * </code></pre>
43  * creates a <code>CaseInsensitiveMap</code> with three entries.<br>
44  * <code>map.get(null)</code> returns <code>"Three"</code> and <code>map.get("ONE")</code>
45  * returns <code>"Four".</code> The <code>Set</code> returned by <code>keySet()</code>
46  * equals <code>{"one", "two", null}.</code>
47  *
48  * @since Commons Collections 3.0
49  * @version $Revision: 1.4 $ $Date: 2004/02/18 01:13:19 $
50  *
51  * @author Commons-Collections team
52  */

53 public class CaseInsensitiveMap extends AbstractHashedMap implements Serializable JavaDoc, Cloneable JavaDoc {
54
55     /** Serialisation version */
56     private static final long serialVersionUID = -7074655917369299456L;
57
58     /**
59      * Constructs a new empty map with default size and load factor.
60      */

61     public CaseInsensitiveMap() {
62         super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
63     }
64
65     /**
66      * Constructs a new, empty map with the specified initial capacity.
67      *
68      * @param initialCapacity the initial capacity
69      * @throws IllegalArgumentException if the initial capacity is less than one
70      */

71     public CaseInsensitiveMap(int initialCapacity) {
72         super(initialCapacity);
73     }
74
75     /**
76      * Constructs a new, empty map with the specified initial capacity and
77      * load factor.
78      *
79      * @param initialCapacity the initial capacity
80      * @param loadFactor the load factor
81      * @throws IllegalArgumentException if the initial capacity is less than one
82      * @throws IllegalArgumentException if the load factor is less than zero
83      */

84     public CaseInsensitiveMap(int initialCapacity, float loadFactor) {
85         super(initialCapacity, loadFactor);
86     }
87
88     /**
89      * Constructor copying elements from another map.
90      * <p>
91      * Keys will be converted to lower case strings, which may cause
92      * some entries to be removed (if string representation of keys differ
93      * only by character case).
94      *
95      * @param map the map to copy
96      * @throws NullPointerException if the map is null
97      */

98     public CaseInsensitiveMap(Map JavaDoc map) {
99         super(map);
100     }
101
102     //-----------------------------------------------------------------------
103
/**
104      * Overrides convertKey() from {@link AbstractHashedMap} to convert keys to
105      * lower case.
106      * <p>
107      * Returns null if key is null.
108      *
109      * @param key the key convert
110      * @return the converted key
111      */

112     protected Object JavaDoc convertKey(Object JavaDoc key) {
113         if (key != null) {
114             return key.toString().toLowerCase();
115         } else {
116             return AbstractHashedMap.NULL;
117         }
118     }
119
120     //-----------------------------------------------------------------------
121
/**
122      * Clones the map without cloning the keys or values.
123      *
124      * @return a shallow clone
125      */

126     public Object JavaDoc clone() {
127         return super.clone();
128     }
129
130     /**
131      * Write the map out using a custom routine.
132      */

133     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
134         out.defaultWriteObject();
135         doWriteObject(out);
136     }
137
138     /**
139      * Read the map in using a custom routine.
140      */

141     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
142         in.defaultReadObject();
143         doReadObject(in);
144     }
145  
146 }
147
Popular Tags