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