1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util; |
8 | |
|
9 | |
import java.lang.reflect.Array; |
10 | |
import java.util.Collection; |
11 | |
import java.util.Iterator; |
12 | |
import java.util.LinkedList; |
13 | |
import java.util.List; |
14 | |
|
15 | |
import org.apache.commons.collections.Predicate; |
16 | |
|
17 | |
|
18 | |
|
19 | 0 | public class CollectionUtils extends org.apache.commons.collections.CollectionUtils |
20 | |
{ |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public static <T>T[] toArrayOfComponentType(Collection objects, Class<T> clazz) |
39 | |
{ |
40 | 0 | if (objects == null) |
41 | |
{ |
42 | 0 | return null; |
43 | |
} |
44 | |
|
45 | 0 | if (clazz == null) |
46 | |
{ |
47 | 0 | throw new IllegalArgumentException("Array target class must not be null"); |
48 | |
} |
49 | |
|
50 | 0 | if (objects.isEmpty()) |
51 | |
{ |
52 | 0 | return (T[]) Array.newInstance(clazz, 0); |
53 | |
} |
54 | |
|
55 | 0 | int i = 0, size = objects.size(); |
56 | 0 | T[] result = (T[]) Array.newInstance(clazz, size); |
57 | 0 | Iterator iter = objects.iterator(); |
58 | |
|
59 | 0 | while (i < size && iter.hasNext()) |
60 | |
{ |
61 | 0 | result[i++] = (T)iter.next(); |
62 | |
} |
63 | |
|
64 | 0 | return result; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static String toString(Collection c, boolean newline) |
76 | |
{ |
77 | 0 | if (c == null || c.isEmpty()) |
78 | |
{ |
79 | 0 | return "[]"; |
80 | |
} |
81 | |
|
82 | 0 | return toString(c, c.size(), newline); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public static String toString(Collection c, int maxElements) |
90 | |
{ |
91 | 0 | return toString(c, maxElements, false); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public static String toString(Collection c, int maxElements, boolean newline) |
106 | |
{ |
107 | 0 | if (c == null || c.isEmpty()) |
108 | |
{ |
109 | 0 | return "[]"; |
110 | |
} |
111 | |
|
112 | 0 | int origNumElements = c.size(); |
113 | 0 | int numElements = Math.min(origNumElements, maxElements); |
114 | 0 | boolean tooManyElements = (origNumElements > maxElements); |
115 | |
|
116 | 0 | StringBuffer buf = new StringBuffer(numElements * 32); |
117 | 0 | buf.append('['); |
118 | |
|
119 | 0 | if (newline) |
120 | |
{ |
121 | 0 | buf.append(SystemUtils.LINE_SEPARATOR); |
122 | |
} |
123 | |
|
124 | 0 | Iterator items = c.iterator(); |
125 | 0 | for (int i = 0; i < numElements - 1; i++) |
126 | |
{ |
127 | 0 | Object item = items.next(); |
128 | |
|
129 | 0 | if (item instanceof Class) |
130 | |
{ |
131 | 0 | buf.append(((Class) item).getName()); |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | buf.append(item); |
136 | |
} |
137 | |
|
138 | 0 | if (newline) |
139 | |
{ |
140 | 0 | buf.append(SystemUtils.LINE_SEPARATOR); |
141 | |
} |
142 | |
else |
143 | |
{ |
144 | 0 | buf.append(',').append(' '); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
|
149 | 0 | Object lastItem = items.next(); |
150 | 0 | if (lastItem instanceof Class) |
151 | |
{ |
152 | 0 | buf.append(((Class) lastItem).getName()); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | 0 | buf.append(lastItem); |
157 | |
} |
158 | |
|
159 | 0 | if (newline) |
160 | |
{ |
161 | 0 | buf.append(SystemUtils.LINE_SEPARATOR); |
162 | |
} |
163 | |
|
164 | 0 | if (tooManyElements) |
165 | |
{ |
166 | 0 | buf.append(" [..]"); |
167 | |
} |
168 | |
|
169 | 0 | buf.append(']'); |
170 | 0 | return buf.toString(); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public static List addCreate(List list, Object value) |
177 | |
{ |
178 | 0 | if (null == list) |
179 | |
{ |
180 | 0 | return singletonList(value); |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 0 | list.add(value); |
185 | 0 | return list; |
186 | |
} |
187 | |
} |
188 | |
|
189 | |
public static List singletonList(Object value) |
190 | |
{ |
191 | 0 | List list = new LinkedList(); |
192 | 0 | list.add(value); |
193 | 0 | return list; |
194 | |
} |
195 | |
|
196 | |
public static boolean containsType(Collection<?> collection, final Class<?> type) |
197 | |
{ |
198 | 0 | if (type == null) |
199 | |
{ |
200 | 0 | return false; |
201 | |
} |
202 | 0 | return exists(collection, new Predicate() |
203 | 0 | { |
204 | |
public boolean evaluate(Object object) |
205 | |
{ |
206 | 0 | return object != null && type.isAssignableFrom(object.getClass()); |
207 | |
} |
208 | |
}); |
209 | |
} |
210 | |
|
211 | |
public static void removeType(Collection<?> collection, final Class<?> type) |
212 | |
{ |
213 | 0 | if (type == null) |
214 | |
{ |
215 | 0 | return; |
216 | |
} |
217 | 0 | filter(collection, new Predicate() |
218 | 0 | { |
219 | |
public boolean evaluate(Object object) |
220 | |
{ |
221 | 0 | return object != null && type.isAssignableFrom(object.getClass()); |
222 | |
} |
223 | |
}); |
224 | 0 | } |
225 | |
|
226 | |
} |