svn-gvsig-desktop / tags / v1_0_2_RELEASE / applications / appgvSIG / src / com / vividsolutions / jump / util / CollectionMap.java @ 11432
History | View | Annotate | Download (5.65 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.Collections; |
39 |
import java.util.HashMap; |
40 |
import java.util.Iterator; |
41 |
import java.util.Map; |
42 |
import java.util.Set; |
43 |
|
44 |
import com.vividsolutions.jts.util.Assert; |
45 |
|
46 |
|
47 |
/**
|
48 |
* A Map whose values are Collections.
|
49 |
*/
|
50 |
public class CollectionMap implements Map { |
51 |
private Map map; |
52 |
private Class collectionClass = ArrayList.class; |
53 |
|
54 |
/**
|
55 |
* Creates a CollectionMap backed by the given Map class.
|
56 |
* @param mapClass a Class that implements Map
|
57 |
*/
|
58 |
public CollectionMap(Class mapClass) { |
59 |
try {
|
60 |
map = (Map) mapClass.newInstance();
|
61 |
} catch (InstantiationException e) { |
62 |
Assert.shouldNeverReachHere(); |
63 |
} catch (IllegalAccessException e) { |
64 |
Assert.shouldNeverReachHere(); |
65 |
} |
66 |
} |
67 |
|
68 |
public CollectionMap(Class mapClass, Class collectionClass) { |
69 |
this.collectionClass = collectionClass;
|
70 |
try {
|
71 |
map = (Map) mapClass.newInstance();
|
72 |
} catch (InstantiationException e) { |
73 |
Assert.shouldNeverReachHere(); |
74 |
} catch (IllegalAccessException e) { |
75 |
Assert.shouldNeverReachHere(); |
76 |
} |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Creates a CollectionMap.
|
81 |
*/
|
82 |
public CollectionMap() {
|
83 |
this(HashMap.class); |
84 |
} |
85 |
|
86 |
private Collection getItemsInternal(Object key) { |
87 |
Collection collection = (Collection) map.get(key); |
88 |
if (collection == null) { |
89 |
try {
|
90 |
collection = (Collection) collectionClass.newInstance();
|
91 |
} catch (InstantiationException e) { |
92 |
Assert.shouldNeverReachHere(); |
93 |
} catch (IllegalAccessException e) { |
94 |
Assert.shouldNeverReachHere(); |
95 |
} |
96 |
map.put(key, collection); |
97 |
} |
98 |
return collection;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Adds the item to the Collection at the given key, creating a new Collection if
|
103 |
* necessary.
|
104 |
* @param key the key to the Collection to which the item should be added
|
105 |
* @param item the item to add
|
106 |
*/
|
107 |
public void addItem(Object key, Object item) { |
108 |
getItemsInternal(key).add(item); |
109 |
} |
110 |
|
111 |
public void removeItem(Object key, Object item) { |
112 |
getItemsInternal(key).remove(item); |
113 |
} |
114 |
|
115 |
public void clear() { |
116 |
map.clear(); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Adds the items to the Collection at the given key, creating a new Collection if
|
121 |
* necessary.
|
122 |
* @param key the key to the Collection to which the items should be added
|
123 |
* @param items the items to add
|
124 |
*/
|
125 |
public void addItems(Object key, Collection items) { |
126 |
for (Iterator i = items.iterator(); i.hasNext();) { |
127 |
addItem(key, i.next()); |
128 |
} |
129 |
} |
130 |
|
131 |
public void addItems(CollectionMap other) { |
132 |
for (Iterator i = other.keySet().iterator(); i.hasNext(); ) { |
133 |
Object key = i.next();
|
134 |
addItems(key, other.getItems(key)); |
135 |
} |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Returns the values.
|
140 |
* @return a view of the values, backed by this CollectionMap
|
141 |
*/
|
142 |
public Collection values() { |
143 |
return map.values();
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Returns the keys.
|
148 |
* @return a view of the keys, backed by this CollectionMap
|
149 |
*/
|
150 |
public Set keySet() { |
151 |
return map.keySet();
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* Returns the number of mappings.
|
156 |
* @return the number of key-value pairs
|
157 |
*/
|
158 |
public int size() { |
159 |
return map.size();
|
160 |
} |
161 |
|
162 |
public Object get(Object key) { |
163 |
return getItems(key);
|
164 |
} |
165 |
|
166 |
public Collection getItems(Object key) { |
167 |
return Collections.unmodifiableCollection(getItemsInternal(key)); |
168 |
} |
169 |
|
170 |
public Object remove(Object key) { |
171 |
return map.remove(key);
|
172 |
} |
173 |
|
174 |
public boolean containsKey(Object key) { |
175 |
return map.containsKey(key);
|
176 |
} |
177 |
|
178 |
public boolean containsValue(Object value) { |
179 |
return map.containsValue(value);
|
180 |
} |
181 |
|
182 |
public Set entrySet() { |
183 |
return map.entrySet();
|
184 |
} |
185 |
|
186 |
public boolean isEmpty() { |
187 |
return map.isEmpty();
|
188 |
} |
189 |
|
190 |
public Object put(Object key, Object value) { |
191 |
Assert.isTrue(value instanceof Collection); |
192 |
|
193 |
return map.put(key, value);
|
194 |
} |
195 |
|
196 |
public void putAll(Map map) { |
197 |
for (Iterator i = map.keySet().iterator(); i.hasNext();) { |
198 |
Object key = i.next();
|
199 |
//Delegate to #put so that the assertion is made. [Jon Aquino]
|
200 |
put(key, map.get(key)); |
201 |
} |
202 |
} |
203 |
public void removeItems(Object key, Collection items) { |
204 |
getItemsInternal(key).removeAll(items); |
205 |
} |
206 |
|
207 |
public Map getMap() { |
208 |
return map;
|
209 |
} |
210 |
|
211 |
} |