svn-gvsig-desktop / tags / v1_0_2_RELEASE / applications / appgvSIG / src / com / vividsolutions / jump / util / CollectionUtil.java @ 11432
History | View | Annotate | Download (9.59 KB)
1 |
/*
|
---|---|
2 |
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
3 |
* for visualizing and manipulating spatial features with geometry and attributes.
|
4 |
*
|
5 |
* Copyright (C) 2003 Vivid Solutions
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
20 |
*
|
21 |
* For more information, contact:
|
22 |
*
|
23 |
* Vivid Solutions
|
24 |
* Suite #1A
|
25 |
* 2328 Government Street
|
26 |
* Victoria BC V8T 5G5
|
27 |
* Canada
|
28 |
*
|
29 |
* (250)385-6040
|
30 |
* www.vividsolutions.com
|
31 |
*/
|
32 |
package com.vividsolutions.jump.util; |
33 |
|
34 |
import java.util.ArrayList; |
35 |
import java.util.Collection; |
36 |
import java.util.Collections; |
37 |
import java.util.HashMap; |
38 |
import java.util.Iterator; |
39 |
import java.util.List; |
40 |
import java.util.Map; |
41 |
import java.util.SortedSet; |
42 |
import java.util.TreeSet; |
43 |
|
44 |
import com.vividsolutions.jts.util.Assert; |
45 |
|
46 |
|
47 |
public class CollectionUtil { |
48 |
public CollectionUtil() {
|
49 |
} |
50 |
|
51 |
/**
|
52 |
* Returns a List of Lists: all combinations of the elements of the given List.
|
53 |
* @param maxCombinationSize combinations larger than this value are discarded
|
54 |
*/
|
55 |
public static List combinations(List original, int maxCombinationSize) { |
56 |
return combinations(original, maxCombinationSize, null); |
57 |
} |
58 |
|
59 |
public static Map inverse(Map map) { |
60 |
Map inverse;
|
61 |
try {
|
62 |
inverse = (Map) map.getClass().newInstance();
|
63 |
} catch (InstantiationException e) { |
64 |
Assert.shouldNeverReachHere(e.toString()); |
65 |
|
66 |
return null; |
67 |
} catch (IllegalAccessException e) { |
68 |
Assert.shouldNeverReachHere(e.toString()); |
69 |
|
70 |
return null; |
71 |
} |
72 |
for (Iterator i = map.keySet().iterator(); i.hasNext();) { |
73 |
Object key = i.next();
|
74 |
Object value = map.get(key);
|
75 |
inverse.put(value, key); |
76 |
} |
77 |
|
78 |
return inverse;
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* Returns a List of Lists: all combinations of the elements of the given List.
|
83 |
* @param maxCombinationSize combinations larger than this value are discarded
|
84 |
* @param mandatoryItem an item that all returned combinations must contain,
|
85 |
* or null to leave unspecified
|
86 |
*/
|
87 |
public static List combinations(List original, int maxCombinationSize, |
88 |
Object mandatoryItem) {
|
89 |
ArrayList combinations = new ArrayList(); |
90 |
|
91 |
//Combinations are given by the bits of each binary number from 1 to 2^N
|
92 |
for (int i = 1; i <= ((int) Math.pow(2, original.size()) - 1); i++) { |
93 |
ArrayList combination = new ArrayList(); |
94 |
for (int j = 0; j < original.size(); j++) { |
95 |
if ((i & (int) Math.pow(2, j)) > 0) { |
96 |
combination.add(original.get(j)); |
97 |
} |
98 |
} |
99 |
if (combination.size() > maxCombinationSize) {
|
100 |
continue;
|
101 |
} |
102 |
if ((mandatoryItem != null) && |
103 |
!combination.contains(mandatoryItem)) { |
104 |
continue;
|
105 |
} |
106 |
combinations.add(combination); |
107 |
} |
108 |
|
109 |
return combinations;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Returns a List of Lists: all combinations of the elements of the given List.
|
114 |
*/
|
115 |
public static List combinations(List original) { |
116 |
return combinations(original, original.size(), null); |
117 |
} |
118 |
|
119 |
public static void removeKeys(Collection keys, Map map) { |
120 |
for (Iterator i = keys.iterator(); i.hasNext();) { |
121 |
Object key = (Object) i.next(); |
122 |
map.remove(key); |
123 |
} |
124 |
} |
125 |
|
126 |
/**
|
127 |
* The nth key corresponds to the nth value
|
128 |
*/
|
129 |
public static List[] keysAndCorrespondingValues(Map map) { |
130 |
ArrayList keys = new ArrayList(map.keySet()); |
131 |
ArrayList values = new ArrayList(); |
132 |
for (Iterator i = keys.iterator(); i.hasNext();) { |
133 |
Object key = i.next();
|
134 |
values.add(map.get(key)); |
135 |
} |
136 |
|
137 |
return new List[] { keys, values }; |
138 |
} |
139 |
|
140 |
public static Collection concatenate(Collection collections) { |
141 |
ArrayList concatenation = new ArrayList(); |
142 |
for (Iterator i = collections.iterator(); i.hasNext();) { |
143 |
Collection collection = (Collection) i.next(); |
144 |
concatenation.addAll(collection); |
145 |
} |
146 |
|
147 |
return concatenation;
|
148 |
} |
149 |
|
150 |
public static Object randomElement(List list) { |
151 |
return list.get((int) Math.floor(Math.random() * list.size())); |
152 |
} |
153 |
|
154 |
public static SortedSet reverseSortedSet(int[] ints) { |
155 |
TreeSet sortedSet = new TreeSet(Collections.reverseOrder()); |
156 |
for (int i = 0; i < ints.length; i++) { |
157 |
sortedSet.add(new Integer(ints[i])); |
158 |
} |
159 |
|
160 |
return sortedSet;
|
161 |
} |
162 |
|
163 |
public static List reverse(List list) { |
164 |
Collections.reverse(list);
|
165 |
|
166 |
return list;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Data is evenly discarded or duplicated to attain the new size
|
171 |
*/
|
172 |
public static Collection stretch(Collection source, Collection destination, |
173 |
int destinationSize) {
|
174 |
Assert.isTrue(destination.isEmpty()); |
175 |
|
176 |
List originalList = source instanceof List ? (List) source |
177 |
: new ArrayList(source); |
178 |
for (int i = 0; i < destinationSize; i++) { |
179 |
destination.add(originalList.get( |
180 |
(int) Math.round( |
181 |
i * originalList.size() / (double) destinationSize)));
|
182 |
} |
183 |
|
184 |
return destination;
|
185 |
} |
186 |
|
187 |
public static Object ifNotIn(Object o, Collection c, Object alternative) { |
188 |
return c.contains(o) ? o : alternative;
|
189 |
} |
190 |
|
191 |
public static void setIfNull(int i, List list, String value) { |
192 |
if (i >= list.size()) {
|
193 |
resize(list, i + 1);
|
194 |
} |
195 |
if (list.get(i) != null) { |
196 |
return;
|
197 |
} |
198 |
list.set(i, value); |
199 |
} |
200 |
|
201 |
public static void resize(List list, int newSize) { |
202 |
if (newSize < list.size()) {
|
203 |
list.subList(newSize, list.size()).clear(); |
204 |
} else {
|
205 |
list.addAll(Collections.nCopies(newSize - list.size(), null)); |
206 |
} |
207 |
} |
208 |
|
209 |
public static boolean containsReference(Object[] objects, Object o) { |
210 |
return indexOf(o, objects) > -1; |
211 |
} |
212 |
|
213 |
public static int indexOf(Object o, Object[] objects) { |
214 |
for (int i = 0; i < objects.length; i++) { |
215 |
if (objects[i] == o) {
|
216 |
return i;
|
217 |
} |
218 |
} |
219 |
|
220 |
return -1; |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Brute force, for when HashSet and TreeSet won't work (e.g. #hashCode
|
225 |
* implementation isn't appropriate). The original Collection is not modified.
|
226 |
*/
|
227 |
public static Collection removeDuplicates(Collection original) { |
228 |
ArrayList result = new ArrayList(); |
229 |
for (Iterator i = original.iterator(); i.hasNext();) { |
230 |
Object item = i.next();
|
231 |
if (!result.contains(item)) {
|
232 |
result.add(item); |
233 |
} |
234 |
} |
235 |
|
236 |
return result;
|
237 |
} |
238 |
|
239 |
public static void addIfNotNull(Object item, Collection collection) { |
240 |
if (item != null) { |
241 |
collection.add(item); |
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Modifies and returns the collection.
|
247 |
*/
|
248 |
public static Collection filterByClass(Collection collection, Class c) { |
249 |
for (Iterator i = collection.iterator(); i.hasNext();) { |
250 |
Object item = i.next();
|
251 |
if (!c.isInstance(item)) {
|
252 |
i.remove(); |
253 |
} |
254 |
} |
255 |
|
256 |
return collection;
|
257 |
} |
258 |
|
259 |
public static Map createMap(Object[] alternatingKeysAndValues) { |
260 |
return createMap(HashMap.class, alternatingKeysAndValues); |
261 |
} |
262 |
|
263 |
public static Map createMap(Class mapClass, |
264 |
Object[] alternatingKeysAndValues) { |
265 |
Map map = null; |
266 |
try {
|
267 |
map = (Map) mapClass.newInstance();
|
268 |
} catch (Exception e) { |
269 |
Assert.shouldNeverReachHere(e.toString()); |
270 |
} |
271 |
for (int i = 0; i < alternatingKeysAndValues.length; i += 2) { |
272 |
map.put(alternatingKeysAndValues[i], alternatingKeysAndValues[i + |
273 |
1]);
|
274 |
} |
275 |
|
276 |
return map;
|
277 |
} |
278 |
|
279 |
/**
|
280 |
* The Smalltalk #collect method.
|
281 |
*/
|
282 |
public static Collection collect(Collection collection, Block block) { |
283 |
ArrayList result = new ArrayList(); |
284 |
for (Iterator i = collection.iterator(); i.hasNext();) { |
285 |
Object item = i.next();
|
286 |
result.add(block.yield(item)); |
287 |
} |
288 |
|
289 |
return result;
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* The Smalltalk #select method.
|
294 |
*/
|
295 |
public static Collection select(Collection collection, Block block) { |
296 |
ArrayList result = new ArrayList(); |
297 |
for (Iterator i = collection.iterator(); i.hasNext();) { |
298 |
Object item = i.next();
|
299 |
if (Boolean.TRUE.equals(block.yield(item))) { |
300 |
result.add(item); |
301 |
} |
302 |
; |
303 |
} |
304 |
|
305 |
return result;
|
306 |
} |
307 |
|
308 |
public static Object get(Class c, Map map) { |
309 |
if (map.keySet().contains(c)) {
|
310 |
return map.get(c);
|
311 |
} |
312 |
for (Iterator i = map.keySet().iterator(); i.hasNext();) { |
313 |
Class candidateClass = (Class) i.next(); |
314 |
if (candidateClass.isAssignableFrom(c)) {
|
315 |
return map.get(candidateClass);
|
316 |
} |
317 |
} |
318 |
|
319 |
return null; |
320 |
} |
321 |
} |