KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > LinkedHashMap


1 /*
2  * *****************************************************************************
3  * Copyright (C) 2006, International Business Machines Corporation and others.
4  * All Rights Reserved.
5  * *****************************************************************************
6  */

7 package com.ibm.icu.impl;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * JDK1.4 LinkedHashMap equivalent implementation for
17  * Java foundation profile support. This class is used
18  * by <code>com.ibm.icu.impl.LRUMap</code> on eclipse
19  * distributions, which require JDK1.3/Java Foundation
20  * profile support.
21  */

22 public class LinkedHashMap extends HashMap JavaDoc {
23     private static final long serialVersionUID = -2497823480436618075L;
24
25     private boolean accessOrder = false;
26     private LinkedList JavaDoc keyList = new LinkedList JavaDoc();
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 JavaDoc m) {
46         super();
47         putAll(m);
48     }
49
50     public void clear() {
51         super.clear();
52         keyList.clear();
53     }
54
55     public Object JavaDoc remove(Object JavaDoc key) {
56         Object JavaDoc value = super.remove(key);
57         if (value == null) {
58             // null might be returned for a map entry
59
// for null value. So we need to check if
60
// the key is actually available or not.
61
// If the key list contains the key, then
62
// remove the key from the list.
63
int index = getKeyIndex(key);
64             if (index >= 0) {
65                 keyList.remove(index);
66             }
67         }
68         return value;
69     }
70     
71     public Object JavaDoc get(Object JavaDoc key) {
72         Object JavaDoc value = super.get(key);
73         if (accessOrder) {
74             // When accessOrder is true, move the key
75
// to the end of the list
76
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 JavaDoc m) {
88         Set JavaDoc keySet = m.keySet();
89         Iterator JavaDoc it = keySet.iterator();
90         while (it.hasNext()) {
91             Object JavaDoc key = it.next();
92             Object JavaDoc value = m.get(key);
93             put(key, value);
94         }
95     }
96
97     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
98         Object JavaDoc oldValue = super.put(key, value);
99
100         // Move the key to the end of key list
101
// if it exists. If not, append the key
102
// to the end of key list
103
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         // Check if we need to remove the eldest
115
// entry.
116
Object JavaDoc eldestKey = keyList.getFirst();
117         Object JavaDoc 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 JavaDoc eldest) {
128         return false;
129     }
130
131     private int getKeyIndex(Object JavaDoc key) {
132         int index = -1;
133         for (int i = 0; i < keyList.size(); i++) {
134             Object JavaDoc 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 JavaDoc {
144         private Object JavaDoc key;
145         private Object JavaDoc value;
146
147         private MapEntry(Object JavaDoc key, Object JavaDoc value) {
148             this.key = key;
149             this.value = value;
150         }
151
152         public boolean equals(Object JavaDoc o) {
153             Object JavaDoc otherKey = ((Map.Entry JavaDoc)o).getKey();
154             Object JavaDoc otherValue = ((Map.Entry JavaDoc)o).getValue();
155             if (key.equals(otherKey) && value.equals(otherValue)) {
156                 return true;
157             }
158             return false;
159         }
160
161         public Object JavaDoc getKey() {
162             return key;
163         }
164
165         public Object JavaDoc getValue() {
166             return value;
167         }
168
169         public Object JavaDoc setValue(Object JavaDoc value) {
170             Object JavaDoc oldValue = this.value;
171             this.value = value;
172             return oldValue;
173         }
174     }
175 }
176
Popular Tags