View Javadoc

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