KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > collections > OrderedMap


1 /*
2  * $Id: OrderedMap.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * 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.collections;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33
34 /**
35  * OrderedMap - HashMap backed by a linked list.
36  *
37  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class OrderedMap extends HashMap JavaDoc {
42
43     private List JavaDoc orderedKeys = new LinkedList JavaDoc();
44
45     /**
46      * @see java.util.Map#keySet()
47      */

48     public Set JavaDoc keySet() {
49         return new OrderedSet(orderedKeys);
50     }
51
52     /**
53      * @return List a copy of the ordered keys list which backs this map
54      */

55     public List JavaDoc getOrderedKeys() {
56         return new LinkedList JavaDoc(this.orderedKeys);
57     }
58
59     /**
60      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
61      */

62     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
63         if (!orderedKeys.contains(key))
64             orderedKeys.add(key);
65         return super.put(key, value);
66     }
67
68     /**
69      * @see java.util.Map#clear()
70      */

71     public void clear() {
72         super.clear();
73         orderedKeys.clear();
74     }
75    
76     /**
77      * @see java.util.Map#remove(java.lang.Object)
78      */

79     public Object JavaDoc remove(Object JavaDoc key) {
80         if (orderedKeys.contains(key))
81             orderedKeys.remove(key);
82         return super.remove(key);
83     }
84     
85     /**
86      * @see java.util.Map#values()
87      */

88     public Collection JavaDoc values() {
89         Iterator JavaDoc i = orderedKeys.iterator();
90         if (!i.hasNext()) {
91             return null;
92         }
93         
94         List JavaDoc values = new ArrayList JavaDoc();
95         while (i.hasNext()) {
96             values.add(this.get(i.next()));
97         }
98         return (Collection JavaDoc) values;
99     }
100
101     public int indexOf(Object JavaDoc key) {
102         return orderedKeys.indexOf(key);
103     }
104 }
105
Popular Tags