001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base.util;
017:
018: import java.util.AbstractCollection;
019: import java.util.AbstractSet;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: /**
028: *
029: * List ordered map.
030: * The iterators returned by <code>keySet()</code>, <code>values()</code>
031: * and <code>entrySet()</code> methods reflect the order in which keys have
032: * been added to the map.
033: *
034: * @author Christoph Beck
035: */
036: public class ListOrderedMap implements Map {
037:
038: private final Map map;
039: private final List lst;
040:
041: public static Map decorate(Map map) {
042: return new ListOrderedMap(map, new ArrayList());
043: }
044:
045: protected ListOrderedMap(Map map, List lst) {
046: super ();
047:
048: this .map = map;
049: this .lst = lst;
050:
051: lst.addAll(map.keySet());
052: }
053:
054: public boolean containsKey(Object key) {
055: return map.containsKey(key);
056: }
057:
058: public boolean containsValue(Object value) {
059: return map.containsValue(value);
060: }
061:
062: public Object get(Object key) {
063: return map.get(key);
064: }
065:
066: public boolean isEmpty() {
067: return map.isEmpty();
068: }
069:
070: public int size() {
071: return map.size();
072: }
073:
074: public boolean equals(Object object) {
075: return object == this ? true : map.equals(object);
076: }
077:
078: public int hashCode() {
079: return map.hashCode();
080: }
081:
082: public Object put(Object key, Object value) {
083: if (!map.containsKey(key)) {
084: lst.add(key);
085: }
086: return map.put(key, value);
087: }
088:
089: public void putAll(Map map) {
090: Iterator it = map.entrySet().iterator();
091: while (it.hasNext()) {
092: Map.Entry entry = (Map.Entry) it.next();
093: put(entry.getKey(), entry.getValue());
094: }
095: }
096:
097: public Object remove(Object key) {
098: if (map.containsKey(key)) {
099: lst.remove(key);
100: return map.remove(key);
101: }
102: return null;
103: }
104:
105: public void clear() {
106: map.clear();
107: lst.clear();
108: }
109:
110: public Collection values() {
111: return new AbstractCollection() {
112: public int size() {
113: return map.size();
114: }
115:
116: public boolean contains(Object value) {
117: return map.containsValue(value);
118: }
119:
120: public void clear() {
121: ListOrderedMap.this .clear();
122: }
123:
124: public Iterator iterator() {
125: return new Iterator() {
126: Object last = null;
127: Iterator keys = lst.iterator();
128:
129: public Object next() {
130: return map.get(last = keys.next());
131: }
132:
133: public boolean hasNext() {
134: return keys.hasNext();
135: }
136:
137: public void remove() {
138: keys.remove();
139: map.remove(last);
140: }
141: };
142: }
143: };
144: }
145:
146: public Set keySet() {
147: return new AbstractSet() {
148: public int size() {
149: return map.size();
150: }
151:
152: public boolean contains(Object value) {
153: return map.containsKey(value);
154: }
155:
156: public void clear() {
157: ListOrderedMap.this .clear();
158: }
159:
160: public Iterator iterator() {
161: return new Iterator() {
162: Object last = null;
163: Iterator keys = lst.iterator();
164:
165: public Object next() {
166: return last = keys.next();
167: }
168:
169: public boolean hasNext() {
170: return keys.hasNext();
171: }
172:
173: public void remove() {
174: keys.remove();
175: map.remove(last);
176: }
177: };
178: }
179: };
180: }
181:
182: public Set entrySet() {
183: return new AbstractSet() {
184: Set delegate = ListOrderedMap.this .map.entrySet();
185:
186: public int size() {
187: return ListOrderedMap.this .size();
188: }
189:
190: public boolean contains(Object obj) {
191: return delegate.contains(obj);
192: }
193:
194: public boolean remove(Object obj) {
195: boolean result = contains(obj);
196: if (result) {
197: ListOrderedMap.this .remove(((Map.Entry) obj)
198: .getKey());
199: }
200: return result;
201: }
202:
203: public void clear() {
204: ListOrderedMap.this .clear();
205: }
206:
207: public boolean equals(Object obj) {
208: return obj == this ? true : delegate.equals(obj);
209: }
210:
211: public int hashCode() {
212: return delegate.hashCode();
213: }
214:
215: public String toString() {
216: return delegate.toString();
217: }
218:
219: public Iterator iterator() {
220: return new Iterator() {
221: Iterator keys = lst.iterator();
222: Object last = null;
223:
224: public Object next() {
225: last = keys.next();
226: return new Map.Entry() {
227: Object key = last;
228:
229: public Object getKey() {
230: return key;
231: }
232:
233: public Object getValue() {
234: return map.get(key);
235: }
236:
237: public Object setValue(Object value) {
238: return map.put(key, value);
239: }
240: };
241: }
242:
243: public boolean hasNext() {
244: return keys.hasNext();
245: }
246:
247: public void remove() {
248: keys.remove();
249: map.remove(last);
250: }
251: };
252: }
253: };
254: }
255:
256: public String toString() {
257: StringBuffer buf = new StringBuffer();
258: buf.append('{');
259: Iterator keys = keySet().iterator();
260: while (keys.hasNext()) {
261: Object key = keys.next();
262: buf.append(key);
263: buf.append('=');
264: buf.append(get(key));
265: if (keys.hasNext()) {
266: buf.append(", ");
267: }
268: }
269: buf.append('}');
270: return buf.toString();
271: }
272: }
|