001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.felix.framework.util;
020
021 import java.util.*;
022
023 /**
024 * Simple utility class that creates a map for string-based keys.
025 * This map can be set to use case-sensitive or case-insensitive
026 * comparison when searching for the key. Any keys put into this
027 * map will be converted to a <tt>String</tt> using the
028 * <tt>toString()</tt> method, since it is only intended to
029 * compare strings.
030 **/
031 public class StringMap implements Map
032 {
033 private TreeMap m_map;
034
035 public StringMap()
036 {
037 this(true);
038 }
039
040 public StringMap(boolean caseSensitive)
041 {
042 m_map = new TreeMap(new StringComparator(caseSensitive));
043 }
044
045 public StringMap(Map map, boolean caseSensitive)
046 {
047 this(caseSensitive);
048 putAll(map);
049 }
050
051 public boolean isCaseSensitive()
052 {
053 return ((StringComparator) m_map.comparator()).isCaseSensitive();
054 }
055
056 public void setCaseSensitive(boolean b)
057 {
058 if (isCaseSensitive() != b)
059 {
060 TreeMap map = new TreeMap(new StringComparator(b));
061 map.putAll(m_map);
062 m_map = map;
063 }
064 }
065
066 public int size()
067 {
068 return m_map.size();
069 }
070
071 public boolean isEmpty()
072 {
073 return m_map.isEmpty();
074 }
075
076 public boolean containsKey(Object arg0)
077 {
078 return m_map.containsKey(arg0);
079 }
080
081 public boolean containsValue(Object arg0)
082 {
083 return m_map.containsValue(arg0);
084 }
085
086 public Object get(Object arg0)
087 {
088 return m_map.get(arg0);
089 }
090
091 public Object put(Object key, Object value)
092 {
093 return m_map.put(key.toString(), value);
094 }
095
096 public void putAll(Map map)
097 {
098 for (Iterator it = map.entrySet().iterator(); it.hasNext(); )
099 {
100 Map.Entry entry = (Map.Entry) it.next();
101 put(entry.getKey(), entry.getValue());
102 }
103 }
104
105 public Object remove(Object arg0)
106 {
107 return m_map.remove(arg0);
108 }
109
110 public void clear()
111 {
112 m_map.clear();
113 }
114
115 public Set keySet()
116 {
117 return m_map.keySet();
118 }
119
120 public Collection values()
121 {
122 return m_map.values();
123 }
124
125 public Set entrySet()
126 {
127 return m_map.entrySet();
128 }
129
130 public String toString()
131 {
132 return m_map.toString();
133 }
134
135 private static class StringComparator implements Comparator
136 {
137 private final boolean m_isCaseSensitive;
138
139 public StringComparator(boolean b)
140 {
141 m_isCaseSensitive = b;
142 }
143
144 public int compare(Object o1, Object o2)
145 {
146 if (m_isCaseSensitive)
147 {
148 return o1.toString().compareTo(o2.toString());
149 }
150 else
151 {
152 return o1.toString().compareToIgnoreCase(o2.toString());
153 }
154 }
155
156 public boolean isCaseSensitive()
157 {
158 return m_isCaseSensitive;
159 }
160 }
161 }