KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > ComparisonMap


1 /******************************************************************************
2  * ComparisonMap.java
3  *****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11
12 import java.util.*;
13
14 /**
15  * Hash table that stores keys case-sensitively, but compares them
16  * case-insensitively
17  *
18  * This differs from
19  * org.apache.commons.collections.map.CaseInsensitiveMap in that it
20  * preserves the first case used for a key (rather than converting all
21  * keys to lower case)
22  *
23  */

24 public class ComparisonMap extends HashMap {
25
26     /** canonical lower-case map */
27     private HashMap keyMap;
28
29     public Set normalizedKeySet() {
30         return keyMap.keySet();
31     }
32
33     public ComparisonMap() {
34         super();
35         keyMap = new HashMap();
36     }
37
38     public ComparisonMap(int initialCapacity) {
39         super(initialCapacity);
40         keyMap = new HashMap(initialCapacity);
41     }
42
43     public ComparisonMap(int initialCapacity, float loadFactor) {
44         super(initialCapacity, loadFactor);
45         keyMap = new HashMap(initialCapacity, loadFactor);
46     }
47
48     public ComparisonMap(Map m) {
49         this(m.size());
50         putAll(m);
51     }
52
53     private Object JavaDoc caseInsensitiveKey(Object JavaDoc key) {
54         return key instanceof String JavaDoc ? ((String JavaDoc)key).toLowerCase() : key;
55     }
56
57     public void clear() {
58         keyMap.clear();
59         super.clear();
60     }
61
62     public Object JavaDoc clone() {
63         return new ComparisonMap(this);
64     }
65
66     public boolean containsKey(Object JavaDoc key) {
67         return keyMap.containsKey(caseInsensitiveKey(key));
68     }
69
70     /* containsValue just works */
71
72     /* entrySet just works, keys have original case */
73
74     public Object JavaDoc get(Object JavaDoc key) {
75         key = caseInsensitiveKey(key);
76         if (keyMap.containsKey(key)) {
77             return super.get(keyMap.get(key));
78         } else {
79             return null;
80         }
81     }
82
83     /* isEmpty just works */
84
85     /* keySet just works, keys have original case */
86
87     public Object JavaDoc put (Object JavaDoc key, Object JavaDoc value) {
88         keyMap.put(caseInsensitiveKey(key), key);
89         return super.put(key, value);
90     }
91
92     public void putAll(Map m) {
93         for (Iterator i = m.entrySet().iterator(); i.hasNext(); ) {
94             Map.Entry entry = (Map.Entry) i.next();
95             put(entry.getKey(), entry.getValue());
96         }
97     }
98
99     public Object JavaDoc remove(Object JavaDoc key) {
100         key = caseInsensitiveKey(key);
101         if (keyMap.containsKey(key)) {
102             return super.remove(keyMap.remove(key));
103         } else {
104             return null;
105         }
106     }
107
108     /* size just works */
109
110     /* values just works */
111
112 }
113
Popular Tags