Package: CollectionUtils

CollectionUtils

nameinstructionbranchcomplexitylinemethod
CollectionUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
arrayToList(Object[])
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
contains(Enumeration, Object)
M: 0 C: 17
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 6
100%
M: 0 C: 1
100%
contains(Iterator, Object)
M: 0 C: 17
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 6
100%
M: 0 C: 1
100%
containsAny(Collection, Collection)
M: 2 C: 22
92%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 5
83%
M: 0 C: 1
100%
containsInstance(Collection, Object)
M: 0 C: 19
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
findFirstMatch(Collection, Collection)
M: 4 C: 22
85%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 5
71%
M: 0 C: 1
100%
findValueOfType(Collection, Class)
M: 31 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
findValueOfType(Collection, Class[])
M: 28 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
hasUniqueObject(Collection)
M: 0 C: 33
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
isEmpty(Collection)
M: 0 C: 9
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isEmpty(Map)
M: 0 C: 9
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
mergeArrayIntoCollection(Object[], Collection)
M: 5 C: 17
77%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
mergePropertiesIntoMap(Properties, Map)
M: 5 C: 23
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 6
86%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.util.common;
13:
14: import java.util.Arrays;
15: import java.util.Collection;
16: import java.util.Enumeration;
17: import java.util.Iterator;
18: import java.util.List;
19: import java.util.Map;
20: import java.util.Properties;
21:
22: /**
23: * Miscellaneous collection utility methods.
24: *
25: */
26: public abstract class CollectionUtils {
27:
28:         /**
29:          * Return <code>true</code> if the supplied Collection is <code>null</code>
30:          * or empty. Otherwise, return <code>false</code>.
31:          * @param collection the Collection to check
32:          * @return whether the given Collection is empty
33:          */
34:         public static boolean isEmpty(Collection<?> collection) {
35:•                return (collection == null || collection.isEmpty());
36:         }
37:
38:         /**
39:          * Return <code>true</code> if the supplied Map is <code>null</code>
40:          * or empty. Otherwise, return <code>false</code>.
41:          * @param map the Map to check
42:          * @return whether the given Map is empty
43:          */
44:         public static boolean isEmpty(Map<?,?> map) {
45:•                return (map == null || map.isEmpty());
46:         }
47:
48:         /**
49:          * Convert the supplied array into a List.
50:          * <p>A <code>null</code> source value will be converted to an
51:          * empty List.
52:          * @param <T> the type of elements of the list
53:          * @param source the array
54:          * @return the converted List result
55:          * @see ObjectUtils#toObjectArray(Object)
56:          */
57:         public static <T> List<T> arrayToList(T[] source) {
58:                 return Arrays.asList(source);
59:         }
60:
61:         /**
62:          * Merge the given array into the given Collection.
63:          * @param <T> type of elements of array
64:          * @param array the array to merge (may be <code>null</code>)
65:          * @param collection the target Collection to merge the array into
66:          */
67:         public static <T> void mergeArrayIntoCollection(T[] array, Collection<T> collection) {
68:•                if (collection == null) {
69:                         throw new IllegalArgumentException("Collection must not be null");
70:                 }
71:•                for (int i = 0; i < array.length; i++) {
72:                         collection.add(array[i]);
73:                 }
74:         }
75:
76:         /**
77:          * Merge the given Properties instance into the given Map,
78:          * copying all properties (key-value pairs) over.
79:          * <p>Uses <code>Properties.propertyNames()</code> to even catch
80:          * default properties linked into the original Properties instance.
81:          * @param props the Properties instance to merge (may be <code>null</code>)
82:          * @param map the target Map to merge the properties into
83:          */
84:         public static void mergePropertiesIntoMap(Properties props, Map<? super String, ? super String> map) {
85:•                if (map == null) {
86:                         throw new IllegalArgumentException("Map must not be null");
87:                 }
88:•                if (props != null) {
89:•                        for (Enumeration<?> en = props.propertyNames(); en.hasMoreElements();) {
90:                                 String key = (String) en.nextElement();
91:                                 map.put(key, props.getProperty(key));
92:                         }
93:                 }
94:         }
95:
96:
97:         /**
98:          * Check whether the given Iterator contains the given element.
99:          * @param iterator the Iterator to check
100:          * @param element the element to look for
101:          * @return <code>true</code> if found, <code>false</code> else
102:          */
103:         public static boolean contains(Iterator<?> iterator, Object element) {
104:•                if (iterator != null) {
105:•                        while (iterator.hasNext()) {
106:                                 Object candidate = iterator.next();
107:•                                if (ObjectUtils.nullSafeEquals(candidate, element)) {
108:                                         return true;
109:                                 }
110:                         }
111:                 }
112:                 return false;
113:         }
114:
115:         /**
116:          * Check whether the given Enumeration contains the given element.
117:          * @param enumeration the Enumeration to check
118:          * @param element the element to look for
119:          * @return <code>true</code> if found, <code>false</code> else
120:          */
121:         public static boolean contains(Enumeration<?> enumeration, Object element) {
122:•                if (enumeration != null) {
123:•                        while (enumeration.hasMoreElements()) {
124:                                 Object candidate = enumeration.nextElement();
125:•                                if (ObjectUtils.nullSafeEquals(candidate, element)) {
126:                                         return true;
127:                                 }
128:                         }
129:                 }
130:                 return false;
131:         }
132:
133:         /**
134:          * Check whether the given Collection contains the given element instance.
135:          * <p>Enforces the given instance to be present, rather than returning
136:          * <code>true</code> for an equal element as well.
137:          * @param collection the Collection to check
138:          * @param element the element to look for
139:          * @return <code>true</code> if found, <code>false</code> else
140:          */
141:         public static boolean containsInstance(Collection<?> collection, Object element) {
142:•                if (collection != null) {
143:•                        for (Iterator<?> it = collection.iterator(); it.hasNext();) {
144:                                 Object candidate = it.next();
145:•                                if (candidate == element) {
146:                                         return true;
147:                                 }
148:                         }
149:                 }
150:                 return false;
151:         }
152:
153:         /**
154:          * Return <code>true</code> if any element in '<code>candidates</code>' is
155:          * contained in '<code>source</code>'; otherwise returns <code>false</code>.
156:          * @param source the source Collection
157:          * @param candidates the candidates to search for
158:          * @return whether any of the candidates has been found
159:          */
160:         public static boolean containsAny(Collection<?> source, Collection<?> candidates) {
161:•                if (isEmpty(source) || isEmpty(candidates)) {
162:                         return false;
163:                 }
164:•                for (Iterator<?> it = candidates.iterator(); it.hasNext();) {
165:•                        if (source.contains(it.next())) {
166:                                 return true;
167:                         }
168:                 }
169:                 return false;
170:         }
171:
172:         /**
173:          * Return the first element in '<code>candidates</code>' that is contained in
174:          * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
175:          * '<code>source</code>' returns <code>null</code>. Iteration order is
176:          * {@link Collection} implementation specific.
177:          * @param source the source Collection
178:          * @param candidates the candidates to search for
179:          * @return the first present object, or <code>null</code> if not found
180:          */
181:         public static Object findFirstMatch(Collection<?> source, Collection<?> candidates) {
182:•                if (isEmpty(source) || isEmpty(candidates)) {
183:                         return null;
184:                 }
185:•                for (Iterator<?> it = candidates.iterator(); it.hasNext();) {
186:                         Object candidate = it.next();
187:•                        if (source.contains(candidate)) {
188:                                 return candidate;
189:                         }
190:                 }
191:                 return null;
192:         }
193:
194:         /**
195:          * Find a single value of the given type in the given Collection.
196:          * @param collection the Collection to search
197:          * @param type the type to look for
198:          * @return a value of the given type found if there is a clear match,
199:          * or <code>null</code> if none or more than one such value found
200:          */
201:         public static Object findValueOfType(Collection<?> collection, Class<?> type) {
202:•                if (isEmpty(collection)) {
203:                         return null;
204:                 }
205:                 Object value = null;
206:•                for (Iterator<?> it = collection.iterator(); it.hasNext();) {
207:                         Object obj = it.next();
208:•                        if (type == null || type.isInstance(obj)) {
209:•                                if (value != null) {
210:                                         // More than one value found... no clear single value.
211:                                         return null;
212:                                 }
213:                                 value = obj;
214:                         }
215:                 }
216:                 return value;
217:         }
218:
219:         /**
220:          * Find a single value of one of the given types in the given Collection:
221:          * searching the Collection for a value of the first type, then
222:          * searching for a value of the second type, etc.
223:          * @param collection the collection to search
224:          * @param types the types to look for, in prioritized order
225:          * @return a value of one of the given types found if there is a clear match,
226:          * or <code>null</code> if none or more than one such value found
227:          */
228:         public static Object findValueOfType(Collection<?> collection, Class<?>[] types) {
229:•                if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
230:                         return null;
231:                 }
232:•                for (int i = 0; i < types.length; i++) {
233:                         Object value = findValueOfType(collection, types[i]);
234:•                        if (value != null) {
235:                                 return value;
236:                         }
237:                 }
238:                 return null;
239:         }
240:
241:         /**
242:          * Determine whether the given Collection only contains a single unique object.
243:          * @param collection the Collection to check
244:          * @return <code>true</code> if the collection contains a single reference or
245:          * multiple references to the same instance, <code>false</code> else
246:          */
247:         public static boolean hasUniqueObject(Collection<?> collection) {
248:•                if (isEmpty(collection)) {
249:                         return false;
250:                 }
251:                 boolean hasCandidate = false;
252:                 Object candidate = null;
253:•                for (Iterator<?> it = collection.iterator(); it.hasNext();) {
254:                         Object elem = it.next();
255:•                        if (!hasCandidate) {
256:                                 hasCandidate = true;
257:                                 candidate = elem;
258:                         }
259:•                        else if (candidate != elem) {
260:                                 return false;
261:                         }
262:                 }
263:                 return true;
264:         }
265:
266: }