一、前言
关于felix源码包中org.apache.felix.framework.util.StringMap自定义字符串Map,通过依赖java.util.TreeMap并实现了java.util.Map接口,具体参见源码说明。
二、源码说明
package org.apache.felix.framework.util;@b@@b@import java.util.Collection;@b@import java.util.Comparator;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import java.util.TreeMap;@b@@b@public class StringMap@b@ implements Map@b@{@b@ private TreeMap m_map;@b@@b@ public StringMap()@b@ {@b@ this(true);@b@ }@b@@b@ public StringMap(boolean caseSensitive)@b@ {@b@ this.m_map = new TreeMap(new StringComparator(caseSensitive));@b@ }@b@@b@ public StringMap(Map map, boolean caseSensitive)@b@ {@b@ this(caseSensitive);@b@ putAll(map);@b@ }@b@@b@ public boolean isCaseSensitive()@b@ {@b@ return ((StringComparator)this.m_map.comparator()).isCaseSensitive();@b@ }@b@@b@ public void setCaseSensitive(boolean b)@b@ {@b@ if (isCaseSensitive() != b)@b@ {@b@ TreeMap map = new TreeMap(new StringComparator(b));@b@ map.putAll(this.m_map);@b@ this.m_map = map;@b@ }@b@ }@b@@b@ public int size()@b@ {@b@ return this.m_map.size();@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return this.m_map.isEmpty();@b@ }@b@@b@ public boolean containsKey(Object arg0)@b@ {@b@ return this.m_map.containsKey(arg0);@b@ }@b@@b@ public boolean containsValue(Object arg0)@b@ {@b@ return this.m_map.containsValue(arg0);@b@ }@b@@b@ public Object get(Object arg0)@b@ {@b@ return this.m_map.get(arg0);@b@ }@b@@b@ public Object put(Object key, Object value)@b@ {@b@ return this.m_map.put(key.toString(), value);@b@ }@b@@b@ public void putAll(Map map)@b@ {@b@ for (Iterator it = map.entrySet().iterator(); it.hasNext(); )@b@ {@b@ Map.Entry entry = (Map.Entry)it.next();@b@ put(entry.getKey(), entry.getValue());@b@ }@b@ }@b@@b@ public Object remove(Object arg0)@b@ {@b@ return this.m_map.remove(arg0);@b@ }@b@@b@ public void clear()@b@ {@b@ this.m_map.clear();@b@ }@b@@b@ public Set keySet()@b@ {@b@ return this.m_map.keySet();@b@ }@b@@b@ public Collection values()@b@ {@b@ return this.m_map.values();@b@ }@b@@b@ public Set entrySet()@b@ {@b@ return this.m_map.entrySet();@b@ }@b@@b@ public String toString()@b@ {@b@ return this.m_map.toString();@b@ }@b@@b@ private static class StringComparator implements Comparator@b@ {@b@ private final boolean m_isCaseSensitive;@b@@b@ public StringComparator(boolean b)@b@ {@b@ this.m_isCaseSensitive = b;@b@ }@b@@b@ public int compare(Object o1, Object o2)@b@ {@b@ if (this.m_isCaseSensitive)@b@ {@b@ return o1.toString().compareTo(o2.toString());@b@ }@b@@b@ return o1.toString().compareToIgnoreCase(o2.toString());@b@ }@b@@b@ public boolean isCaseSensitive()@b@ {@b@ return this.m_isCaseSensitive;@b@ }@b@ }@b@}