KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > InsertionSortedMap


1 package org.jahia.utils;
2
3 import java.util.*;
4 import java.io.Serializable JavaDoc;
5
6
7 /**
8  * <p>Title: Map implementation that respects the insertion order.</p>
9  * <p>Description: This map implementation actually uses an ArrayList to
10  * store the entry pairs.</p>
11  * <p>Copyright: Copyright (c) 2002</p>
12  * <p>Company: Jahia Ltd</p>
13  * @author Serge Huber
14  * @version 1.0
15  *
16  */

17
18 public class InsertionSortedMap extends AbstractMap implements Serializable JavaDoc {
19
20     private ArrayList internalList = new ArrayList();
21
22     private class Entry implements Map.Entry, Serializable JavaDoc {
23
24         private Object JavaDoc key;
25         private Object JavaDoc value;
26
27         public Entry(Object JavaDoc key, Object JavaDoc value) {
28             this.key = key;
29             this.value = value;
30         }
31
32         public boolean equals (Object JavaDoc o) {
33             if (! (o instanceof Entry)) {
34                 return false;
35             }
36             Entry e2 = (Entry) o;
37             if ( (this.getKey() == null ?
38                   e2.getKey() == null : this.getKey().equals(e2.getKey())) &&
39                 (this.getValue() == null ?
40                  e2.getValue() == null : this.getValue().equals(e2.getValue()))) {
41                 return true;
42             } else {
43                 return false;
44             }
45
46         }
47
48         public int hashCode() {
49            return ((this.getKey()==null ? 0 : this.getKey().hashCode()) ^
50                     (this.getValue()==null ? 0 : this.getValue().hashCode()));
51         }
52
53         public Object JavaDoc getKey() {
54             return key;
55         }
56
57         public Object JavaDoc getValue() {
58             return value;
59         }
60
61         public Object JavaDoc setValue(Object JavaDoc value) {
62             Object JavaDoc oldValue = this.value;
63             this.value = value;
64             return oldValue;
65         }
66
67     }
68
69     public InsertionSortedMap() {
70     }
71
72     public InsertionSortedMap(Map t) {
73         // we must now build the key order based on the map we were passed.
74
Iterator sourceEntryIter = t.entrySet().iterator();
75         while (sourceEntryIter.hasNext()) {
76             Map.Entry curEntry = (Map.Entry) sourceEntryIter.next();
77             internalList.add(curEntry.getKey());
78         }
79     }
80
81     public Set entrySet() {
82         InsertionSortedSet insertionSortedSet = new InsertionSortedSet();
83         insertionSortedSet.setInternalList(internalList);
84         return insertionSortedSet;
85     }
86
87     public Object JavaDoc put(Object JavaDoc key,
88                       Object JavaDoc value)
89         throws UnsupportedOperationException JavaDoc ,
90         ClassCastException JavaDoc ,
91         IllegalArgumentException JavaDoc ,
92         NullPointerException JavaDoc {
93         int pos = findKey(key);
94         if (pos == -1) {
95             Entry newEntry = new Entry(key, value);
96             internalList.add(newEntry);
97             return null;
98         } else {
99             Entry existingEntry = (Entry) internalList.get(pos);
100             Object JavaDoc oldValue = existingEntry.getValue();
101             existingEntry.setValue(value);
102             return oldValue;
103         }
104     }
105
106     private int findKey(Object JavaDoc key) {
107         int pos = -1;
108         Iterator listIter = internalList.iterator();
109         while (listIter.hasNext()) {
110             Map.Entry curEntry = (Map.Entry) listIter.next();
111             pos++;
112             if (curEntry.getKey().equals(key)) {
113                 return pos;
114             }
115         }
116         return -1;
117     }
118
119 }
120
Popular Tags