Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionUtils |
|
| 4.333333333333333;4.333 |
1 | /* | |
2 | * $Id: CollectionUtils.java 10529 2008-01-25 05:58:36Z dfeist $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
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 | ||
20 | // @ThreadSafe | |
21 | 0 | public class CollectionUtils extends org.apache.commons.collections.CollectionUtils |
22 | { | |
23 | ||
24 | /** | |
25 | * Creates an array of the given Collection's elements, but with the given | |
26 | * <code>Class</code> as element type. Useful for arrays of objects that | |
27 | * implement multiple interfaces and a "typed view" onto these objects is | |
28 | * required. | |
29 | * | |
30 | * @param objects a Collection of objects | |
31 | * @param clazz the desired service type of the new array | |
32 | * @return <code>null</code> when objects is <code>null</code>, or a new | |
33 | * array containing the elements of the source array which is typed to | |
34 | * the given <code>clazz</code> parameter. | |
35 | * @throws IllegalArgumentException if the <code>clazz</code> argument is | |
36 | * <code>null</code>. | |
37 | * @throws ArrayStoreException if the elements in <code>objects</code> cannot | |
38 | * be cast to <code>clazz</code>. | |
39 | */ | |
40 | public static Object[] toArrayOfComponentType(Collection objects, Class clazz) | |
41 | { | |
42 | 10 | if (objects == null) |
43 | { | |
44 | 2 | return null; |
45 | } | |
46 | ||
47 | 8 | if (clazz == null) |
48 | { | |
49 | 2 | throw new IllegalArgumentException("Array target class must not be null"); |
50 | } | |
51 | ||
52 | 6 | if (objects.isEmpty()) |
53 | { | |
54 | 2 | return (Object[]) Array.newInstance(clazz, 0); |
55 | } | |
56 | ||
57 | 4 | int i = 0, size = objects.size(); |
58 | 4 | Object[] result = (Object[]) Array.newInstance(clazz, size); |
59 | 4 | Iterator iter = objects.iterator(); |
60 | ||
61 | 10 | while (i < size && iter.hasNext()) |
62 | { | |
63 | 8 | result[i++] = iter.next(); |
64 | } | |
65 | ||
66 | 2 | return result; |
67 | } | |
68 | ||
69 | /** | |
70 | * Creates a String representation of the given Collection, with optional | |
71 | * newlines between elements. Class objects are represented by their full names. | |
72 | * | |
73 | * @param c the Collection to format | |
74 | * @param newline indicates whether elements are to be split across lines | |
75 | * @return the formatted String | |
76 | */ | |
77 | public static String toString(Collection c, boolean newline) | |
78 | { | |
79 | 22 | if (c == null || c.isEmpty()) |
80 | { | |
81 | 8 | return "[]"; |
82 | } | |
83 | ||
84 | 14 | return toString(c, c.size(), newline); |
85 | } | |
86 | ||
87 | /** | |
88 | * Calls {@link #toString(Collection, int, boolean)} with <code>false</code> | |
89 | * for newline. | |
90 | */ | |
91 | public static String toString(Collection c, int maxElements) | |
92 | { | |
93 | 48 | return toString(c, maxElements, false); |
94 | } | |
95 | ||
96 | /** | |
97 | * Creates a String representation of the given Collection, with optional | |
98 | * newlines between elements. Class objects are represented by their full names. | |
99 | * Considers at most <code>maxElements</code> values; overflow is indicated by | |
100 | * an appended "[..]" ellipsis. | |
101 | * | |
102 | * @param c the Collection to format | |
103 | * @param maxElements the maximum number of elements to take into account | |
104 | * @param newline indicates whether elements are to be split across lines | |
105 | * @return the formatted String | |
106 | */ | |
107 | public static String toString(Collection c, int maxElements, boolean newline) | |
108 | { | |
109 | 62 | if (c == null || c.isEmpty()) |
110 | { | |
111 | 8 | return "[]"; |
112 | } | |
113 | ||
114 | 54 | int origNumElements = c.size(); |
115 | 54 | int numElements = Math.min(origNumElements, maxElements); |
116 | 54 | boolean tooManyElements = (origNumElements > maxElements); |
117 | ||
118 | 54 | StringBuffer buf = new StringBuffer(numElements * 32); |
119 | 54 | buf.append('['); |
120 | ||
121 | 54 | if (newline) |
122 | { | |
123 | 10 | buf.append(SystemUtils.LINE_SEPARATOR); |
124 | } | |
125 | ||
126 | 54 | Iterator items = c.iterator(); |
127 | 436 | for (int i = 0; i < numElements - 1; i++) |
128 | { | |
129 | 382 | Object item = items.next(); |
130 | ||
131 | 382 | if (item instanceof Class) |
132 | { | |
133 | 0 | buf.append(((Class) item).getName()); |
134 | } | |
135 | else | |
136 | { | |
137 | 382 | buf.append(item); |
138 | } | |
139 | ||
140 | 382 | if (newline) |
141 | { | |
142 | 20 | buf.append(SystemUtils.LINE_SEPARATOR); |
143 | } | |
144 | else | |
145 | { | |
146 | 362 | buf.append(',').append(' '); |
147 | } | |
148 | } | |
149 | ||
150 | // don't forget the last one | |
151 | 54 | Object lastItem = items.next(); |
152 | 54 | if (lastItem instanceof Class) |
153 | { | |
154 | 4 | buf.append(((Class) lastItem).getName()); |
155 | } | |
156 | else | |
157 | { | |
158 | 50 | buf.append(lastItem); |
159 | } | |
160 | ||
161 | 54 | if (newline) |
162 | { | |
163 | 10 | buf.append(SystemUtils.LINE_SEPARATOR); |
164 | } | |
165 | ||
166 | 54 | if (tooManyElements) |
167 | { | |
168 | 4 | buf.append(" [..]"); |
169 | } | |
170 | ||
171 | 54 | buf.append(']'); |
172 | 54 | return buf.toString(); |
173 | } | |
174 | ||
175 | /** | |
176 | * Some code uses null to indicate "unset", which makes appending items complex. | |
177 | */ | |
178 | public static List addCreate(List list, Object value) | |
179 | { | |
180 | 0 | if (null == list) |
181 | { | |
182 | 0 | return singletonList(value); |
183 | } | |
184 | else | |
185 | { | |
186 | 0 | list.add(value); |
187 | 0 | return list; |
188 | } | |
189 | } | |
190 | ||
191 | public static List singletonList(Object value) | |
192 | { | |
193 | 558 | List list = new LinkedList(); |
194 | 558 | list.add(value); |
195 | 558 | return list; |
196 | } | |
197 | ||
198 | } |