01: /*
02: * $Id: OrderedMap.java,v 1.2 2003/08/17 03:11:29 ajzeneski Exp $
03: *
04: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: */
24: package org.ofbiz.base.util;
25:
26: import java.util.ArrayList;
27: import java.util.Collection;
28: import java.util.HashMap;
29: import java.util.Iterator;
30: import java.util.LinkedList;
31: import java.util.List;
32: import java.util.Set;
33:
34: /**
35: * OrderedMap - HashMap backed by a linked list.
36: *
37: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
38: * @version $Revision: 1.2 $
39: * @since 2.0
40: */
41: public class OrderedMap extends HashMap {
42:
43: private List orderedKeys = new LinkedList();
44:
45: /**
46: * @see java.util.Map#keySet()
47: */
48: public Set keySet() {
49: return new OrderedSet(orderedKeys);
50: }
51:
52: /**
53: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
54: */
55: public Object put(Object key, Object value) {
56: if (!orderedKeys.contains(key))
57: orderedKeys.add(key);
58: return super .put(key, value);
59: }
60:
61: /**
62: * @see java.util.Map#clear()
63: */
64: public void clear() {
65: super .clear();
66: orderedKeys.clear();
67: }
68:
69: /**
70: * @see java.util.Map#remove(java.lang.Object)
71: */
72: public Object remove(Object key) {
73: if (orderedKeys.contains(key))
74: orderedKeys.remove(key);
75: return super .remove(key);
76: }
77:
78: /**
79: * @see java.util.Map#values()
80: */
81: public Collection values() {
82: Iterator i = orderedKeys.iterator();
83: if (!i.hasNext()) {
84: return null;
85: }
86:
87: List values = new ArrayList();
88: while (i.hasNext()) {
89: values.add(this .get(i.next()));
90: }
91: return (Collection) values;
92: }
93: }
|