1 7 package com.ibm.icu.impl; 8 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.LinkedList ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 22 public class LinkedHashMap extends HashMap { 23 private static final long serialVersionUID = -2497823480436618075L; 24 25 private boolean accessOrder = false; 26 private LinkedList keyList = new LinkedList (); 27 28 public LinkedHashMap() { 29 super(); 30 } 31 32 public LinkedHashMap(int initialCapacity) { 33 super(initialCapacity); 34 } 35 36 public LinkedHashMap(int initialCapacity, float loadFactor) { 37 super(initialCapacity, loadFactor); 38 } 39 40 public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) { 41 super(initialCapacity, loadFactor); 42 this.accessOrder = accessOrder; 43 } 44 45 public LinkedHashMap(Map m) { 46 super(); 47 putAll(m); 48 } 49 50 public void clear() { 51 super.clear(); 52 keyList.clear(); 53 } 54 55 public Object remove(Object key) { 56 Object value = super.remove(key); 57 if (value == null) { 58 int index = getKeyIndex(key); 64 if (index >= 0) { 65 keyList.remove(index); 66 } 67 } 68 return value; 69 } 70 71 public Object get(Object key) { 72 Object value = super.get(key); 73 if (accessOrder) { 74 int index = getKeyIndex(key); 77 if (index >= 0) { 78 if (index != keyList.size() - 1) { 79 keyList.remove(index); 80 keyList.addLast(key); 81 } 82 } 83 } 84 return value; 85 } 86 87 public void putAll(Map m) { 88 Set keySet = m.keySet(); 89 Iterator it = keySet.iterator(); 90 while (it.hasNext()) { 91 Object key = it.next(); 92 Object value = m.get(key); 93 put(key, value); 94 } 95 } 96 97 public Object put(Object key, Object value) { 98 Object oldValue = super.put(key, value); 99 100 int index = getKeyIndex(key); 104 if (index >= 0) { 105 if (index != keyList.size() - 1) { 106 keyList.remove(index); 107 keyList.addLast(key); 108 } 109 } 110 else { 111 keyList.addLast(key); 112 } 113 114 Object eldestKey = keyList.getFirst(); 117 Object eldestValue = super.get(eldestKey); 118 MapEntry entry = new MapEntry(eldestKey, eldestValue); 119 if (removeEldestEntry(entry)) { 120 keyList.removeFirst(); 121 super.remove(eldestKey); 122 } 123 124 return oldValue; 125 } 126 127 protected boolean removeEldestEntry(Map.Entry eldest) { 128 return false; 129 } 130 131 private int getKeyIndex(Object key) { 132 int index = -1; 133 for (int i = 0; i < keyList.size(); i++) { 134 Object o = keyList.get(i); 135 if (o.equals(key)) { 136 index = i; 137 break; 138 } 139 } 140 return index; 141 } 142 143 protected static class MapEntry implements Map.Entry { 144 private Object key; 145 private Object value; 146 147 private MapEntry(Object key, Object value) { 148 this.key = key; 149 this.value = value; 150 } 151 152 public boolean equals(Object o) { 153 Object otherKey = ((Map.Entry )o).getKey(); 154 Object otherValue = ((Map.Entry )o).getValue(); 155 if (key.equals(otherKey) && value.equals(otherValue)) { 156 return true; 157 } 158 return false; 159 } 160 161 public Object getKey() { 162 return key; 163 } 164 165 public Object getValue() { 166 return value; 167 } 168 169 public Object setValue(Object value) { 170 Object oldValue = this.value; 171 this.value = value; 172 return oldValue; 173 } 174 } 175 } 176 | Popular Tags |