KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > LinkedHashMap


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.axis.utils;
8
9 // $Id: LinkedHashMap.java,v 1.1.2.1 2005/03/02 14:30:12 tdiesler Exp $
10

11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /** A JDK-1.3 implementation of a LinkedHashMap
19  *
20  * @author Thomas.Diesler@jboss.org
21  * @since 12-Nov-2004
22  */

23 public class LinkedHashMap implements Map JavaDoc
24 {
25
26    private HashMap JavaDoc delegate = new HashMap JavaDoc();
27    private OrderedSet keys = new OrderedSet();
28
29    public int size()
30    {
31       return delegate.size();
32    }
33
34    public boolean isEmpty()
35    {
36       return delegate.isEmpty();
37    }
38
39    public boolean containsKey(Object JavaDoc key)
40    {
41       return delegate.containsKey(key);
42    }
43
44    public boolean containsValue(Object JavaDoc value)
45    {
46       return delegate.containsValue(value);
47    }
48
49    public Object JavaDoc get(Object JavaDoc key)
50    {
51       return delegate.get(key);
52    }
53
54    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
55    {
56       Object JavaDoc obj = delegate.put(key, value);
57       keys.add(key);
58       return obj;
59    }
60
61    public Object JavaDoc remove(Object JavaDoc key)
62    {
63       Object JavaDoc obj = delegate.remove(key);
64       keys.remove(key);
65       return obj;
66    }
67
68    public void putAll(Map JavaDoc t)
69    {
70       Iterator JavaDoc it = t.keySet().iterator();
71       while (it.hasNext())
72       {
73          Object JavaDoc key = it.next();
74          Object JavaDoc val = t.get(key);
75          put(key, val);
76       }
77    }
78
79    public void clear()
80    {
81       delegate.clear();
82       keys.clear();
83    }
84
85    public Set JavaDoc keySet()
86    {
87       OrderedSet set = new OrderedSet();
88       set.addAll(keys);
89       return set;
90    }
91
92    public Collection JavaDoc values()
93    {
94       ArrayList JavaDoc list = new ArrayList JavaDoc();
95       Iterator JavaDoc it = keySet().iterator();
96       while (it.hasNext())
97       {
98          Object JavaDoc key = it.next();
99          Object JavaDoc val = get(key);
100          list.add(val);
101       }
102       return list;
103    }
104
105    public Set JavaDoc entrySet()
106    {
107       OrderedSet set = new OrderedSet();
108       Iterator JavaDoc it = keySet().iterator();
109       while (it.hasNext())
110       {
111          Object JavaDoc key = it.next();
112          Object JavaDoc val = get(key);
113          set.add(new Entry(key, val));
114       }
115       return set;
116    }
117
118    public static class Entry implements Map.Entry JavaDoc
119    {
120       private Object JavaDoc key;
121       private Object JavaDoc val;
122
123       public Entry(Object JavaDoc key, Object JavaDoc val)
124       {
125          this.key = key;
126          this.val = val;
127       }
128
129       public Object JavaDoc getKey()
130       {
131          return key;
132       }
133
134       public Object JavaDoc getValue()
135       {
136          return val;
137       }
138
139       public Object JavaDoc setValue(Object JavaDoc value)
140       {
141          this.val = value;
142          return val;
143       }
144    }
145
146    public class OrderedSet implements Set JavaDoc
147    {
148
149       private ArrayList JavaDoc delegate = new ArrayList JavaDoc();
150
151       public int size()
152       {
153          return delegate.size();
154       }
155
156       public boolean isEmpty()
157       {
158          return delegate.isEmpty();
159       }
160
161       public boolean contains(Object JavaDoc o)
162       {
163          return delegate.contains(o);
164       }
165
166       public Iterator JavaDoc iterator()
167       {
168          return delegate.iterator();
169       }
170
171       public Object JavaDoc[] toArray()
172       {
173          return delegate.toArray();
174       }
175
176       public Object JavaDoc[] toArray(Object JavaDoc a[])
177       {
178          return delegate.toArray(a);
179       }
180
181       public boolean add(Object JavaDoc o)
182       {
183          return delegate.add(o);
184       }
185
186       public boolean remove(Object JavaDoc o)
187       {
188          return delegate.remove(o);
189       }
190
191       public boolean containsAll(Collection JavaDoc c)
192       {
193          return delegate.containsAll(c);
194       }
195
196       public boolean addAll(Collection JavaDoc c)
197       {
198          return delegate.addAll(c);
199       }
200
201       public boolean retainAll(Collection JavaDoc c)
202       {
203          return delegate.retainAll(c);
204       }
205
206       public boolean removeAll(Collection JavaDoc c)
207       {
208          return delegate.removeAll(c);
209       }
210
211       public void clear()
212       {
213          delegate.clear();
214       }
215    }
216 }
217
Popular Tags