Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionUtils |
|
| 5.5;5.5 |
1 | /* | |
2 | * $Id: CollectionUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $ | |
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 | ||
17 | ||
18 | // @ThreadSafe | |
19 | 0 | public class CollectionUtils extends org.apache.commons.collections.CollectionUtils |
20 | { | |
21 | ||
22 | /** | |
23 | * Creates an array of the given Collection's elements, but with the given | |
24 | * <code>Class</code> as element type. Useful for arrays of objects that | |
25 | * implement multiple interfaces and a "typed view" onto these objects is | |
26 | * required. | |
27 | * | |
28 | * @param objects a Collection of objects | |
29 | * @param clazz the desired component type of the new array | |
30 | * @return <code>null</code> when objects is <code>null</code>, or a new | |
31 | * array containing the elements of the source array which is typed to | |
32 | * the given <code>clazz</code> parameter. | |
33 | * @throws IllegalArgumentException if the <code>clazz</code> argument is | |
34 | * <code>null</code>. | |
35 | * @throws ArrayStoreException if the elements in <code>objects</code> cannot | |
36 | * be cast to <code>clazz</code>. | |
37 | */ | |
38 | public static Object[] toArrayOfComponentType(Collection objects, Class clazz) | |
39 | { | |
40 | 10 | if (objects == null) |
41 | { | |
42 | 2 | return null; |
43 | } | |
44 | ||
45 | 8 | if (clazz == null) |
46 | { | |
47 | 2 | throw new IllegalArgumentException("Array target class must not be null"); |
48 | } | |
49 | ||
50 | 6 | if (objects.isEmpty()) |
51 | { | |
52 | 2 | return (Object[]) Array.newInstance(clazz, 0); |
53 | } | |
54 | ||
55 | 4 | int i = 0, size = objects.size(); |
56 | 4 | Object[] result = (Object[]) Array.newInstance(clazz, size); |
57 | 4 | Iterator iter = objects.iterator(); |
58 | ||
59 | 10 | while (i < size && iter.hasNext()) |
60 | { | |
61 | 8 | result[i++] = iter.next(); |
62 | } | |
63 | ||
64 | 2 | return result; |
65 | } | |
66 | ||
67 | /** | |
68 | * Creates a String representation of the given Collection, with optional | |
69 | * newlines between elements. Class objects are represented by their full names. | |
70 | * | |
71 | * @param c the Collection to format | |
72 | * @param newline indicates whether elements are to be split across lines | |
73 | * @return the formatted String | |
74 | */ | |
75 | public static String toString(Collection c, boolean newline) | |
76 | { | |
77 | 16 | if (c == null || c.isEmpty()) |
78 | { | |
79 | 8 | return "[]"; |
80 | } | |
81 | ||
82 | 8 | return toString(c, c.size(), newline); |
83 | } | |
84 | ||
85 | /** | |
86 | * Calls {@link #toString(Collection, int, boolean)} with <code>false</code> | |
87 | * for newline. | |
88 | */ | |
89 | public static String toString(Collection c, int maxElements) | |
90 | { | |
91 | 8 | return toString(c, maxElements, false); |
92 | } | |
93 | ||
94 | /** | |
95 | * Creates a String representation of the given Collection, with optional | |
96 | * newlines between elements. Class objects are represented by their full names. | |
97 | * Considers at most <code>maxElements</code> values; overflow is indicated by | |
98 | * an appended "[..]" ellipsis. | |
99 | * | |
100 | * @param c the Collection to format | |
101 | * @param maxElements the maximum number of elements to take into account | |
102 | * @param newline indicates whether elements are to be split across lines | |
103 | * @return the formatted String | |
104 | */ | |
105 | public static String toString(Collection c, int maxElements, boolean newline) | |
106 | { | |
107 | 16 | if (c == null || c.isEmpty()) |
108 | { | |
109 | 0 | return "[]"; |
110 | } | |
111 | ||
112 | 16 | int origNumElements = c.size(); |
113 | 16 | int numElements = Math.min(origNumElements, maxElements); |
114 | 16 | boolean tooManyElements = (origNumElements > maxElements); |
115 | ||
116 | 16 | StringBuffer buf = new StringBuffer(numElements * 32); |
117 | 16 | buf.append('['); |
118 | ||
119 | 16 | if (newline) |
120 | { | |
121 | 4 | buf.append(SystemUtils.LINE_SEPARATOR); |
122 | } | |
123 | ||
124 | 16 | Iterator items = c.iterator(); |
125 | 140 | for (int i = 0; i < numElements - 1; i++) |
126 | { | |
127 | 124 | Object item = items.next(); |
128 | ||
129 | 124 | if (item instanceof Class) |
130 | { | |
131 | 0 | buf.append(((Class) item).getName()); |
132 | } | |
133 | else | |
134 | { | |
135 | 124 | buf.append(item); |
136 | } | |
137 | ||
138 | 124 | if (newline) |
139 | { | |
140 | 2 | buf.append(SystemUtils.LINE_SEPARATOR); |
141 | } | |
142 | else | |
143 | { | |
144 | 122 | buf.append(',').append(' '); |
145 | } | |
146 | } | |
147 | ||
148 | // don't forget the last one | |
149 | 16 | Object lastItem = items.next(); |
150 | 16 | if (lastItem instanceof Class) |
151 | { | |
152 | 4 | buf.append(((Class) lastItem).getName()); |
153 | } | |
154 | else | |
155 | { | |
156 | 12 | buf.append(lastItem); |
157 | } | |
158 | ||
159 | 16 | if (newline) |
160 | { | |
161 | 4 | buf.append(SystemUtils.LINE_SEPARATOR); |
162 | } | |
163 | ||
164 | 16 | if (tooManyElements) |
165 | { | |
166 | 4 | buf.append(" [..]"); |
167 | } | |
168 | ||
169 | 16 | buf.append(']'); |
170 | 16 | return buf.toString(); |
171 | } | |
172 | ||
173 | } |