1 package cz.cuni.amis.utils.collections;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Random;
9 import java.util.Set;
10
11 import cz.cuni.amis.utils.IFilter;
12 import cz.cuni.amis.utils.ObjectFilter;
13
14 public class MyCollections {
15
16 private static Random random = new Random(System.currentTimeMillis());
17
18
19
20
21
22
23
24
25 public static <T> List<T> getFiltered(Collection<T> col, IFilter filter) {
26 if (col == null) return null;
27 if (filter == null) return new ArrayList<T>(col);
28 ArrayList<T> result = new ArrayList<T>(col.size());
29 for (T obj : col) {
30 if (filter.isAccepted(obj)) result.add(obj);
31 }
32 return result;
33 }
34
35
36
37
38
39
40
41
42 public static <T> T[] getFiltered(T[] array, IFilter filter) {
43 if (array == null) return null;
44 if (filter == null) return Arrays.copyOf(array, array.length);
45 ArrayList<T> result = new ArrayList<T>(array.length);
46 for (T obj : array) {
47 if (filter.isAccepted(obj)) result.add(obj);
48 }
49 return (T[]) result.toArray(new Object[0]);
50 }
51
52
53
54
55
56
57
58
59
60
61 public static <T> T getRandom(Collection<T> col) {
62 if (col == null) return null;
63 if (col instanceof List) return getRandom((List<T>)col);
64 if (col.size() == 0) return null;
65 int rnd = random.nextInt(col.size());
66 Iterator<T> iter = col.iterator();
67 for (int i = 0; i < rnd-1; ++i) iter.next();
68 return iter.next();
69 }
70
71
72
73
74
75
76
77
78
79 public static <T> T getRandom(List<T> list) {
80 if (list == null) return null;
81 if (list.size() == 0) return null;
82 return list.get(random.nextInt(list.size()));
83 }
84
85
86
87
88
89
90
91 public static <T> T getRandom(T[] array) {
92 if (array == null) return null;
93 if (array.length == 0) return null;
94 return array[random.nextInt(array.length)];
95 }
96
97
98
99
100
101
102
103
104
105
106
107 public static <T> T getRandomFiltered(Collection<T> col, IFilter filter) {
108 if (col == null) return null;
109 if (filter == null) return getRandom(col);
110 List<T> filtered = getFiltered(col, filter);
111 return getRandom(filtered);
112 }
113
114
115
116
117
118
119
120 public static <T> T getRandomFiltered(T[] array, IFilter filter) {
121 if (array == null) return null;
122 if (filter == null) return getRandom(array);
123 T[] filtered = getFiltered(array, filter);
124 return getRandom(filtered);
125 }
126
127
128
129
130
131
132
133 public static <T> void toList(T[] objects, List<T> list) {
134 if (objects == null) return;
135 if (list == null) return;
136 for (T obj : objects) {
137 list.add(obj);
138 }
139 }
140
141
142
143
144
145
146
147
148 public static <T> void toList(T[] objects, List<T> list, ObjectFilter filter) {
149 if (filter == null) {
150 toList(objects, list);
151 return;
152 }
153 if (objects == null) return;
154 if (list == null) return;
155 for (T obj : objects) {
156 if (filter.accept(obj)) {
157 list.add(obj);
158 }
159 }
160 }
161
162
163
164
165
166
167
168
169 public static <T> void toList(T[] objects, List<T> list, IFilter filter) {
170 if (filter == null) {
171 toList(objects, list);
172 return;
173 }
174 if (objects == null) return;
175 if (list == null) return;
176 for (T obj : objects) {
177 if (filter.isAccepted(obj)) {
178 list.add(obj);
179 }
180 }
181 }
182
183 public static <T> List<T> asList(T[] objects) {
184 if (objects == null) return null;
185 List<T> list = new ArrayList<T>(objects.length);
186 for (T object : objects) {
187 list.add(object);
188 }
189 return list;
190 }
191
192 public static <T> List<T> asList(Collection<T> objects) {
193 if (objects == null) return null;
194 List<T> list = new ArrayList<T>(objects.size());
195 for (T object : objects) {
196 list.add(object);
197 }
198 return list;
199 }
200
201 public static <T> List<T> asList(T[] objects, ObjectFilter filter) {
202 if (filter == null) return asList(objects);
203 if (objects == null) return null;
204 List<T> list = new ArrayList<T>(objects.length);
205 for (T object : objects) {
206 if (filter.accept(object)) {
207 list.add(object);
208 }
209 }
210 return list;
211 }
212
213 public static <T> List<T> asList(Collection<T> objects, ObjectFilter filter) {
214 if (filter == null) return asList(objects);
215 if (objects == null) return null;
216 List<T> list = new ArrayList<T>(objects.size());
217 for (T object : objects) {
218 if (filter.accept(object)) {
219 list.add(object);
220 }
221 }
222 return list;
223 }
224
225 public static <T> List<T> asList(T[] objects, IFilter filter) {
226 if (filter == null) return asList(objects);
227 if (objects == null) return null;
228 List<T> list = new ArrayList<T>(objects.length);
229 for (T object : objects) {
230 if (filter.isAccepted(object)) {
231 list.add(object);
232 }
233 }
234 return list;
235 }
236
237 public static <T> List<T> asList(Collection<T> objects, IFilter filter) {
238 if (filter == null) return asList(objects);
239 if (objects == null) return null;
240 List<T> list = new ArrayList<T>(objects.size());
241 for (T object : objects) {
242 if (filter.isAccepted(object)) {
243 list.add(object);
244 }
245 }
246 return list;
247 }
248
249 public static String toString(Object objToString, String[] prefixes, String[] postfixes, String[] separators, IToString toString) {
250
251 StringBuffer sb = new StringBuffer(200);
252
253 String[] newPrefixes = null;
254 String[] newPostfixes = null;
255 String[] newSeparators = null;
256
257 boolean first = true;
258
259 sb.append(prefixes != null && prefixes.length > 0 ? prefixes[0] : "");
260
261 String separator = separators != null && separators.length > 0 ? separators[0] : "";
262
263 if (objToString.getClass().isArray()) {
264 objToString = asList((Object[])objToString);
265 }
266 if (objToString instanceof Collection) {
267
268 for (Object obj : (Collection)objToString) {
269
270 if (first) first = false;
271 else sb.append(separator);
272
273 if (obj instanceof Collection) {
274 if (newPrefixes == null) {
275 if (prefixes != null && prefixes.length > 1) {
276 newPrefixes = new String[prefixes.length-1];
277 System.arraycopy(prefixes, 1, newPrefixes, 0, prefixes.length-1);
278 } else {
279 newPrefixes = new String[0];
280 }
281 if (postfixes != null && postfixes.length > 1) {
282 newPostfixes = new String[postfixes.length-1];
283 System.arraycopy(postfixes, 1, newPostfixes, 0, postfixes.length-1);
284 } else {
285 newPostfixes = new String[0];
286 }
287 if (separators != null && separators.length > 1) {
288 newSeparators = new String[separators.length-1];
289 System.arraycopy(separators, 1, newSeparators, 0, separators.length-1);
290 } else {
291 newSeparators = new String[0];
292 }
293 }
294 sb.append(MyCollections.toString(obj, newPrefixes, newPostfixes, newSeparators, toString));
295 } else {
296 sb.append(toString.toString(obj));
297 }
298 }
299 } else {
300 sb.append(toString.toString(objToString));
301 }
302
303 sb.append(postfixes != null && postfixes.length > 0 ? postfixes[0] : "");
304
305 return sb.toString();
306 }
307
308 public static String toString(Object obj, String prefix, String postfix, String separator) {
309 return toString(obj, new String[]{prefix}, new String[]{postfix}, new String[]{separator}, TO_STRING );
310 }
311
312 public static String toString(Object obj, String prefix, String postfix, String separator, IToString toString) {
313 return toString(obj, new String[]{prefix}, new String[]{postfix}, new String[]{separator}, toString );
314 }
315
316 public static final IToString TO_STRING = new IToString() {
317
318 @Override
319 public String toString(Object obj) {
320 return obj.toString();
321 }
322
323 };
324
325 public static interface IToString {
326
327 public String toString(Object obj);
328
329 }
330
331
332
333
334
335
336
337
338
339 public static <T> void toCollection(T[] array, Collection<T> col) {
340 if (array == null) return;
341 if (col == null) return;
342 for (T t : array) {
343 col.add(t);
344 }
345 }
346
347 }