⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderedmap.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description://  This class provides an ordered HashMap// ----------------------------------------------------------------------------// Change History://  2006/03/26  Martin D. Flynn//      Initial release//  2006/06/30  Martin D. Flynn//     -Repackaged//  2008/05/14  Martin D. Flynn//     -Replace method 'keys()' with 'keyIterator()'//     -Added method 'keyArray(...)'//     -Added methods 'valueArray(...)' and 'valueIterator()'//     -Added initial Java 5 'generics'// ----------------------------------------------------------------------------package org.opengts.util;import java.util.*;/***** <code>OrderedMap</code> provides a HashMap where values can also be retrieved in*** the order they were added**/public class OrderedMap<K,V>    extends HashMap<K,V>    implements Map<K,V>{        // ------------------------------------------------------------------------        private OrderedSet<K>       keyOrder = null;    private Map<String,String>  ignoredCaseMap = null;        /**    *** Constructor    **/    public OrderedMap()     {        super();        this.keyOrder = new OrderedSet<K>();    }        /**    *** Constructor    *** @param map  A map from which all contents will be copied    **/    public OrderedMap(Map<K,V> map)     {        this();        this.putAll(map);    }        // ------------------------------------------------------------------------    /**    *** Returns true if all lookups should be performed with case-insensitive keys     *** @return True if all lookups should be performed with case-insensitive keys    **/    public boolean isIgnoreCase()    {        return (this.ignoredCaseMap != null);    }        /**     *** Sets the case-insensitive key-lookup mode    *** @param ignoreCase  True to perform case-insensitive key lookups    **/    public void setIgnoreCase(boolean ignoreCase)    {        if (ignoreCase) {            if (this.ignoredCaseMap == null) {                this.ignoredCaseMap = new HashMap<String,String>();                for (Iterator<K> i = this.keyOrder.iterator(); i.hasNext();) {                    K key = (K)i.next();                    if (key instanceof String) {                        this.ignoredCaseMap.put(((String)key).toLowerCase(), (String)key);                    }                }            }        } else {            if (this.ignoredCaseMap != null) {                this.ignoredCaseMap = null;            }        }    }        /**    *** Maps the specified String key to its map lookup key.  If the specified key is not     *** a String, then the key argument is simple returned.    *** @param key  The key     *** @return The mapped lookup key    **/    public Object keyCaseFilter(Object key)    {        if ((this.ignoredCaseMap != null) && (key instanceof String)) {            String k = (String)this.ignoredCaseMap.get(((String)key).toLowerCase());            if (k != null) {                //if (!k.equals(key)) { Print.logStackTrace("Filtered key: " + key + " ==> " + k); }                return k;            }        }        return key;    }        // ------------------------------------------------------------------------        /**    *** Clears the contents of this map    **/    public void clear()     {        super.clear();        this.keyOrder.clear();        if (this.ignoredCaseMap != null) {            this.ignoredCaseMap.clear();         }    }        // ------------------------------------------------------------------------        /**    *** Return a set of Map.Entry elements    *** @return A set of Map.Entry elements    **/    public Set<Map.Entry<K,V>> entrySet()    {        // Attempting to return an ordered set of 'Map.Entry' entries.        // The effect this will have on calls to this method from HashMap itself        // isn't fully known.        /* Map.Entry map */        Set<Map.Entry<K,V>> es = super.entrySet(); // unordered        Map<K,Map.Entry<K,V>> meMap = new HashMap<K,Map.Entry<K,V>>();        for (Iterator<Map.Entry<K,V>> i = es.iterator(); i.hasNext();) {            Map.Entry<K,V> me = (Map.Entry<K,V>)i.next();            K key = me.getKey();            meMap.put(key, me);        }                /* place in keyOrder */        OrderedSet<Map.Entry<K,V>> entSet = new OrderedSet<Map.Entry<K,V>>();        for (Iterator<K> i = this.keyOrder.iterator(); i.hasNext();) {            K key = (K)i.next();            Map.Entry<K,V> me  = (Map.Entry<K,V>)meMap.get(key);            if (me == null) { Print.logError("Map.Entry is null!!!"); }            entSet.add(me);        }        return entSet;    }    // ------------------------------------------------------------------------    /**     *** Returns an ordered set of keys from this map    *** (Warning: The returned 'keySet' is not backed by the map, thus any Iterator 'remove()'     **  calls performed on the returned set will not remove the item from this map)    *** @return An ordered set of keys from this map    **/    public Set<K> keySet()    {        return new OrderedSet<K>(this.keyOrder);    }    /**    *** Returns an array of key elements from this map    *** @return An array of key elements from this map    **/    public K[] keyArray(Class<K> arrayType)    {        return ListTools.toArray(this.keyOrder, arrayType);    }    /**    *** Returns an Iterator over the keys in this map    *** @return An Iterator over the keys in this map    **/    public Iterator<K> keyIterator()    {        return new Iterator<K>() {            private Iterator<K> i = OrderedMap.this.keyOrder.iterator();            public boolean hasNext() {                return i.hasNext();            }            public K next() {                return i.next();            }            public void remove() {                throw new UnsupportedOperationException();            }        };    }    // ------------------------------------------------------------------------    /**     *** Returns an ordered set of values from this map    *** @return An ordered set of values from this map    **/    public Set<V> valueSet()    {        OrderedSet<V> valSet = new OrderedSet<V>();        for (Iterator<K> i = this.keyOrder.iterator(); i.hasNext();) {            valSet.add(super.get(i.next()));        }        return valSet;    }    /**    *** Returns an array of value elements from this map    *** @return An array of value elements from this map    **/    public V[] valueArray(Class<V> arrayType)    {        return ListTools.toArray(this.valueSet(), arrayType);    }    /**    *** Returns an Iterator over the values in this map    *** @return An Iterator over the values in this map    **/    public Iterator<V> valueIterator()    {        return new Iterator<V>() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -