root / tags / v1_0_2_Build_914 / applications / appgvSIG / src / com / vividsolutions / jump / util / OrderedMap.java @ 12200
History | View | Annotate | Download (3.55 KB)
1 |
|
---|---|
2 |
/*
|
3 |
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
4 |
* for visualizing and manipulating spatial features with geometry and attributes.
|
5 |
*
|
6 |
* Copyright (C) 2003 Vivid Solutions
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Vivid Solutions
|
25 |
* Suite #1A
|
26 |
* 2328 Government Street
|
27 |
* Victoria BC V8T 5G5
|
28 |
* Canada
|
29 |
*
|
30 |
* (250)385-6040
|
31 |
* www.vividsolutions.com
|
32 |
*/
|
33 |
|
34 |
package com.vividsolutions.jump.util; |
35 |
|
36 |
import java.util.ArrayList; |
37 |
import java.util.Collection; |
38 |
import java.util.HashMap; |
39 |
import java.util.Iterator; |
40 |
import java.util.List; |
41 |
import java.util.Map; |
42 |
import java.util.Set; |
43 |
|
44 |
|
45 |
/**
|
46 |
* A Map that preserves the order of its keys.
|
47 |
*/
|
48 |
public class OrderedMap implements Map { |
49 |
private Map map; |
50 |
private List keyList; |
51 |
|
52 |
/**
|
53 |
* Creates an OrderedMap backed by the given map.
|
54 |
* @param map a Map that will be this OrderedMap's underlying Map
|
55 |
*/
|
56 |
public OrderedMap(List keyList, Map map) { |
57 |
this.keyList = keyList;
|
58 |
this.map = map;
|
59 |
} |
60 |
|
61 |
/**
|
62 |
* Creates an OrderedMap.
|
63 |
*/
|
64 |
public OrderedMap() {
|
65 |
this(new HashMap()); |
66 |
} |
67 |
|
68 |
public OrderedMap(Map map) { |
69 |
this(new UniqueList(), map); |
70 |
} |
71 |
|
72 |
public int size() { |
73 |
return map.size();
|
74 |
} |
75 |
|
76 |
public boolean isEmpty() { |
77 |
return map.isEmpty();
|
78 |
} |
79 |
|
80 |
public boolean containsKey(Object key) { |
81 |
return map.containsKey(key);
|
82 |
} |
83 |
|
84 |
public boolean containsValue(Object value) { |
85 |
return map.containsValue(value);
|
86 |
} |
87 |
|
88 |
public Object get(Object key) { |
89 |
return map.get(key);
|
90 |
} |
91 |
|
92 |
public Object put(Object key, Object value) { |
93 |
keyList.add(key); |
94 |
|
95 |
return map.put(key, value);
|
96 |
} |
97 |
|
98 |
public Object remove(Object key) { |
99 |
keyList.remove(key); |
100 |
|
101 |
return map.remove(key);
|
102 |
} |
103 |
|
104 |
public void putAll(Map t) { |
105 |
keyList.addAll(t.keySet()); |
106 |
map.putAll(t); |
107 |
} |
108 |
|
109 |
public void clear() { |
110 |
keyList.clear(); |
111 |
map.clear(); |
112 |
} |
113 |
|
114 |
public Set keySet() { |
115 |
return map.keySet();
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Returns the keys, in order.
|
120 |
* @return the keys in the order they were (first) added
|
121 |
*/
|
122 |
public List keyList() { |
123 |
return keyList;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Returns the values.
|
128 |
* @return the values in the same order as the keys
|
129 |
* @see #keyList()
|
130 |
*/
|
131 |
public List valueList() { |
132 |
return (List) values(); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Returns the values.
|
137 |
* @return the values in the same order as the keys
|
138 |
* @see #keyList()
|
139 |
*/
|
140 |
public Collection values() { |
141 |
ArrayList values = new ArrayList(); |
142 |
|
143 |
for (Iterator i = keyList.iterator(); i.hasNext();) { |
144 |
Object key = i.next();
|
145 |
values.add(map.get(key)); |
146 |
} |
147 |
|
148 |
return values;
|
149 |
} |
150 |
|
151 |
public Set entrySet() { |
152 |
return map.entrySet();
|
153 |
} |
154 |
|
155 |
public boolean equals(Object o) { |
156 |
return map.equals(o);
|
157 |
} |
158 |
} |