source: osm/applications/editors/josm/plugins/conflation/src/com/vividsolutions/jump/util/CollectionUtil.java@ 28163

Last change on this file since 28163 was 28163, checked in by joshdoe, 12 years ago

conflation: now uses Java Conflation Suite and depends on JTS plugin

Currently depends on a few JUMP classes and the JCS files are directly included, this will change in the future.

File size: 9.8 KB
Line 
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
34package com.vividsolutions.jump.util;
35
36import java.util.ArrayList;
37import java.util.Collection;
38import java.util.Collections;
39import java.util.HashMap;
40import java.util.Iterator;
41import java.util.List;
42import java.util.Map;
43import java.util.SortedSet;
44import java.util.TreeSet;
45
46import com.vividsolutions.jts.util.Assert;
47
48public class CollectionUtil {
49 public CollectionUtil() {}
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 return null;
66 } catch (IllegalAccessException e) {
67 Assert.shouldNeverReachHere(e.toString());
68 return null;
69 }
70 for (Iterator i = map.keySet().iterator(); i.hasNext();) {
71 Object key = i.next();
72 Object value = map.get(key);
73 inverse.put(value, key);
74 }
75 return inverse;
76 }
77
78 /**
79 * Returns a List of Lists: all combinations of the elements of the given List.
80 * @param maxCombinationSize combinations larger than this value are discarded
81 * @param mandatoryItem an item that all returned combinations must contain,
82 * or null to leave unspecified
83 */
84 public static List combinations(
85 List original,
86 int maxCombinationSize,
87 Object mandatoryItem) {
88 ArrayList combinations = new ArrayList();
89
90 //Combinations are given by the bits of each binary number from 1 to 2^N
91 for (int i = 1; i <= ((int) Math.pow(2, original.size()) - 1); i++) {
92 ArrayList combination = new ArrayList();
93
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
100 if (combination.size() > maxCombinationSize) {
101 continue;
102 }
103
104 if ((mandatoryItem != null) && !combination.contains(mandatoryItem)) {
105 continue;
106 }
107
108 combinations.add(combination);
109 }
110
111 return combinations;
112 }
113
114 /**
115 * Returns a List of Lists: all combinations of the elements of the given List.
116 */
117 public static List combinations(List original) {
118 return combinations(original, original.size(), null);
119 }
120
121 public static void removeKeys(Collection keys, Map map) {
122 for (Iterator i = keys.iterator(); i.hasNext();) {
123 Object key = (Object) i.next();
124 map.remove(key);
125 }
126 }
127
128 /**
129 * The nth key corresponds to the nth value
130 */
131 public static List[] keysAndCorrespondingValues(Map map) {
132 ArrayList keys = new ArrayList(map.keySet());
133 ArrayList values = new ArrayList();
134 for (Iterator i = keys.iterator(); i.hasNext();) {
135 Object key = i.next();
136 values.add(map.get(key));
137 }
138 return new List[] { keys, values };
139 }
140
141 public static Collection concatenate(Collection collections) {
142 ArrayList concatenation = new ArrayList();
143 for (Iterator i = collections.iterator(); i.hasNext();) {
144 Collection collection = (Collection) i.next();
145 concatenation.addAll(collection);
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 return sortedSet;
160 }
161
162 public static List reverse(List list) {
163 Collections.reverse(list);
164 return list;
165 }
166
167 /**
168 * Data is evenly discarded or duplicated to attain the new size
169 */
170 public static Collection stretch(
171 Collection source,
172 Collection destination,
173 int destinationSize) {
174 try {
175 Assert.isTrue(destination.isEmpty());
176 List originalList =
177 source instanceof List ? (List) source : new ArrayList(source);
178 for (int i = 0; i < destinationSize; i++) {
179 destination.add(
180 originalList.get(
181 (int) Math.round(
182 i * originalList.size() / (double) destinationSize)));
183 }
184 } finally {
185 return destination;
186 }
187 }
188
189 public static Object ifNotIn(Object o, Collection c, Object alternative) {
190 return c.contains(o) ? o : alternative;
191 }
192
193 public static void setIfNull(int i, List list, String value) {
194 if (i >= list.size()) {
195 resize(list, i + 1);
196 }
197 if (list.get(i) != null) {
198 return;
199 }
200 list.set(i, value);
201 }
202
203 public static void resize(List list, int newSize) {
204 if (newSize < list.size()) {
205 list.subList(newSize, list.size()).clear();
206 } else {
207 list.addAll(Collections.nCopies(newSize - list.size(), null));
208 }
209 }
210
211 public static boolean containsReference(Object[] objects, Object o) {
212 for (int i = 0; i < objects.length; i++) {
213 if (objects[i] == o) {
214 return true;
215 }
216 }
217 return false;
218 }
219 /**
220 * Brute force, for when HashSet and TreeSet won't work (e.g. #hashCode
221 * implementation isn't appropriate). The original Collection is not modified.
222 */
223 public static Collection removeDuplicates(Collection original) {
224 ArrayList result = new ArrayList();
225 for (Iterator i = original.iterator(); i.hasNext();) {
226 Object item = i.next();
227 if (!result.contains(item)) {
228 result.add(item);
229 }
230 }
231 return result;
232 }
233
234 public static void addIfNotNull(Object item, Collection collection) {
235 if (item != null) {
236 collection.add(item);
237 }
238 }
239
240 /**
241 * Modifies and returns the collection.
242 */
243 public static Collection filterByClass(Collection collection, Class c) {
244 for (Iterator i = collection.iterator(); i.hasNext();) {
245 Object item = i.next();
246 if (!c.isInstance(item)) {
247 i.remove();
248 }
249 }
250 return collection;
251 }
252
253 public static Map createMap(Object[] alternatingKeysAndValues) {
254 return createMap(HashMap.class, alternatingKeysAndValues);
255 }
256
257 public static Map createMap(Class mapClass, Object[] alternatingKeysAndValues) {
258 Map map = null;
259 try {
260 map = (Map) mapClass.newInstance();
261 } catch (Exception e) {
262 Assert.shouldNeverReachHere(e.toString());
263 }
264 for (int i = 0; i < alternatingKeysAndValues.length; i += 2) {
265 map.put(alternatingKeysAndValues[i], alternatingKeysAndValues[i + 1]);
266 }
267 return map;
268 }
269
270 /**
271 * The Smalltalk #collect method.
272 */
273 public static Collection collect(Collection collection, Block block) {
274 ArrayList result = new ArrayList();
275 for (Iterator i = collection.iterator(); i.hasNext();) {
276 Object item = i.next();
277 result.add(block.yield(item));
278 }
279 return result;
280 }
281
282 /**
283 * The Smalltalk #select method.
284 */
285 public static Collection select(Collection collection, Block block) {
286 ArrayList result = new ArrayList();
287 for (Iterator i = collection.iterator(); i.hasNext();) {
288 Object item = i.next();
289 if(Boolean.TRUE.equals(block.yield(item))) {
290 result.add(item);
291 };
292 }
293 return result;
294 }
295
296 public static Object get(Class c, Map map) {
297 if (map.keySet().contains(c)) {
298 return map.get(c);
299 }
300 for (Iterator i = map.keySet().iterator(); i.hasNext();) {
301 Class candidateClass = (Class) i.next();
302 if (candidateClass.isAssignableFrom(c)) {
303 return map.get(candidateClass);
304 }
305 }
306 return null;
307 }
308}
Note: See TracBrowser for help on using the repository browser.