]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/CollectionUtils.java
2 * Copyright 2002-2008 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.osgi
.boot
.internal
.springutil
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collection
;
21 import java
.util
.Enumeration
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import java
.util
.Properties
;
28 * Miscellaneous collection utility methods.
29 * Mainly for internal use within the framework.
31 * @author Juergen Hoeller
35 public abstract class CollectionUtils
{
38 * Return <code>true</code> if the supplied Collection is <code>null</code>
39 * or empty. Otherwise, return <code>false</code>.
40 * @param collection the Collection to check
41 * @return whether the given Collection is empty
43 public static boolean isEmpty(Collection collection
) {
44 return (collection
== null || collection
.isEmpty());
48 * Return <code>true</code> if the supplied Map is <code>null</code>
49 * or empty. Otherwise, return <code>false</code>.
50 * @param map the Map to check
51 * @return whether the given Map is empty
53 public static boolean isEmpty(Map map
) {
54 return (map
== null || map
.isEmpty());
58 * Convert the supplied array into a List. A primitive array gets
59 * converted into a List of the appropriate wrapper type.
60 * <p>A <code>null</code> source value will be converted to an
62 * @param source the (potentially primitive) array
63 * @return the converted List result
64 * @see ObjectUtils#toObjectArray(Object)
66 public static List
arrayToList(Object source
) {
67 return Arrays
.asList(ObjectUtils
.toObjectArray(source
));
71 * Merge the given array into the given Collection.
72 * @param array the array to merge (may be <code>null</code>)
73 * @param collection the target Collection to merge the array into
75 public static void mergeArrayIntoCollection(Object array
, Collection collection
) {
76 if (collection
== null) {
77 throw new IllegalArgumentException("Collection must not be null");
79 Object
[] arr
= ObjectUtils
.toObjectArray(array
);
80 for (int i
= 0; i
< arr
.length
; i
++) {
81 collection
.add(arr
[i
]);
86 * Merge the given Properties instance into the given Map,
87 * copying all properties (key-value pairs) over.
88 * <p>Uses <code>Properties.propertyNames()</code> to even catch
89 * default properties linked into the original Properties instance.
90 * @param props the Properties instance to merge (may be <code>null</code>)
91 * @param map the target Map to merge the properties into
93 public static void mergePropertiesIntoMap(Properties props
, Map map
) {
95 throw new IllegalArgumentException("Map must not be null");
98 for (Enumeration en
= props
.propertyNames(); en
.hasMoreElements();) {
99 String key
= (String
) en
.nextElement();
100 map
.put(key
, props
.getProperty(key
));
107 * Check whether the given Iterator contains the given element.
108 * @param iterator the Iterator to check
109 * @param element the element to look for
110 * @return <code>true</code> if found, <code>false</code> else
112 public static boolean contains(Iterator iterator
, Object element
) {
113 if (iterator
!= null) {
114 while (iterator
.hasNext()) {
115 Object candidate
= iterator
.next();
116 if (ObjectUtils
.nullSafeEquals(candidate
, element
)) {
125 * Check whether the given Enumeration contains the given element.
126 * @param enumeration the Enumeration to check
127 * @param element the element to look for
128 * @return <code>true</code> if found, <code>false</code> else
130 public static boolean contains(Enumeration enumeration
, Object element
) {
131 if (enumeration
!= null) {
132 while (enumeration
.hasMoreElements()) {
133 Object candidate
= enumeration
.nextElement();
134 if (ObjectUtils
.nullSafeEquals(candidate
, element
)) {
143 * Check whether the given Collection contains the given element instance.
144 * <p>Enforces the given instance to be present, rather than returning
145 * <code>true</code> for an equal element as well.
146 * @param collection the Collection to check
147 * @param element the element to look for
148 * @return <code>true</code> if found, <code>false</code> else
150 public static boolean containsInstance(Collection collection
, Object element
) {
151 if (collection
!= null) {
152 for (Iterator it
= collection
.iterator(); it
.hasNext();) {
153 Object candidate
= it
.next();
154 if (candidate
== element
) {
163 * Return <code>true</code> if any element in '<code>candidates</code>' is
164 * contained in '<code>source</code>'; otherwise returns <code>false</code>.
165 * @param source the source Collection
166 * @param candidates the candidates to search for
167 * @return whether any of the candidates has been found
169 public static boolean containsAny(Collection source
, Collection candidates
) {
170 if (isEmpty(source
) || isEmpty(candidates
)) {
173 for (Iterator it
= candidates
.iterator(); it
.hasNext();) {
174 if (source
.contains(it
.next())) {
182 * Return the first element in '<code>candidates</code>' that is contained in
183 * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
184 * '<code>source</code>' returns <code>null</code>. Iteration order is
185 * {@link Collection} implementation specific.
186 * @param source the source Collection
187 * @param candidates the candidates to search for
188 * @return the first present object, or <code>null</code> if not found
190 public static Object
findFirstMatch(Collection source
, Collection candidates
) {
191 if (isEmpty(source
) || isEmpty(candidates
)) {
194 for (Iterator it
= candidates
.iterator(); it
.hasNext();) {
195 Object candidate
= it
.next();
196 if (source
.contains(candidate
)) {
204 * Find a single value of the given type in the given Collection.
205 * @param collection the Collection to search
206 * @param type the type to look for
207 * @return a value of the given type found if there is a clear match,
208 * or <code>null</code> if none or more than one such value found
210 public static Object
findValueOfType(Collection collection
, Class type
) {
211 if (isEmpty(collection
)) {
215 for (Iterator it
= collection
.iterator(); it
.hasNext();) {
216 Object obj
= it
.next();
217 if (type
== null || type
.isInstance(obj
)) {
219 // More than one value found... no clear single value.
229 * Find a single value of one of the given types in the given Collection:
230 * searching the Collection for a value of the first type, then
231 * searching for a value of the second type, etc.
232 * @param collection the collection to search
233 * @param types the types to look for, in prioritized order
234 * @return a value of one of the given types found if there is a clear match,
235 * or <code>null</code> if none or more than one such value found
237 public static Object
findValueOfType(Collection collection
, Class
[] types
) {
238 if (isEmpty(collection
) || ObjectUtils
.isEmpty(types
)) {
241 for (int i
= 0; i
< types
.length
; i
++) {
242 Object value
= findValueOfType(collection
, types
[i
]);
251 * Determine whether the given Collection only contains a single unique object.
252 * @param collection the Collection to check
253 * @return <code>true</code> if the collection contains a single reference or
254 * multiple references to the same instance, <code>false</code> else
256 public static boolean hasUniqueObject(Collection collection
) {
257 if (isEmpty(collection
)) {
260 boolean hasCandidate
= false;
261 Object candidate
= null;
262 for (Iterator it
= collection
.iterator(); it
.hasNext();) {
263 Object elem
= it
.next();
268 else if (candidate
!= elem
) {