Move to Commons Base
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / internal / springutil / CollectionUtils.java
diff --git a/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/CollectionUtils.java b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/CollectionUtils.java
deleted file mode 100644 (file)
index ad01542..0000000
+++ /dev/null
@@ -1,275 +0,0 @@
-/*\r
- * Copyright 2002-2008 the original author or authors.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.argeo.osgi.boot.internal.springutil;\r
-\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Enumeration;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Properties;\r
-\r
-/**\r
- * Miscellaneous collection utility methods.\r
- * Mainly for internal use within the framework.\r
- *\r
- * @author Juergen Hoeller\r
- * @author Rob Harrop\r
- * @since 1.1.3\r
- */\r
-public abstract class CollectionUtils {\r
-\r
-       /**\r
-        * Return <code>true</code> if the supplied Collection is <code>null</code>\r
-        * or empty. Otherwise, return <code>false</code>.\r
-        * @param collection the Collection to check\r
-        * @return whether the given Collection is empty\r
-        */\r
-       public static boolean isEmpty(Collection collection) {\r
-               return (collection == null || collection.isEmpty());\r
-       }\r
-\r
-       /**\r
-        * Return <code>true</code> if the supplied Map is <code>null</code>\r
-        * or empty. Otherwise, return <code>false</code>.\r
-        * @param map the Map to check\r
-        * @return whether the given Map is empty\r
-        */\r
-       public static boolean isEmpty(Map map) {\r
-               return (map == null || map.isEmpty());\r
-       }\r
-\r
-       /**\r
-        * Convert the supplied array into a List. A primitive array gets\r
-        * converted into a List of the appropriate wrapper type.\r
-        * <p>A <code>null</code> source value will be converted to an\r
-        * empty List.\r
-        * @param source the (potentially primitive) array\r
-        * @return the converted List result\r
-        * @see ObjectUtils#toObjectArray(Object)\r
-        */\r
-       public static List arrayToList(Object source) {\r
-               return Arrays.asList(ObjectUtils.toObjectArray(source));\r
-       }\r
-\r
-       /**\r
-        * Merge the given array into the given Collection.\r
-        * @param array the array to merge (may be <code>null</code>)\r
-        * @param collection the target Collection to merge the array into\r
-        */\r
-       public static void mergeArrayIntoCollection(Object array, Collection collection) {\r
-               if (collection == null) {\r
-                       throw new IllegalArgumentException("Collection must not be null");\r
-               }\r
-               Object[] arr = ObjectUtils.toObjectArray(array);\r
-               for (int i = 0; i < arr.length; i++) {\r
-                       collection.add(arr[i]);\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Merge the given Properties instance into the given Map,\r
-        * copying all properties (key-value pairs) over.\r
-        * <p>Uses <code>Properties.propertyNames()</code> to even catch\r
-        * default properties linked into the original Properties instance.\r
-        * @param props the Properties instance to merge (may be <code>null</code>)\r
-        * @param map the target Map to merge the properties into\r
-        */\r
-       public static void mergePropertiesIntoMap(Properties props, Map map) {\r
-               if (map == null) {\r
-                       throw new IllegalArgumentException("Map must not be null");\r
-               }\r
-               if (props != null) {\r
-                       for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {\r
-                               String key = (String) en.nextElement();\r
-                               map.put(key, props.getProperty(key));\r
-                       }\r
-               }\r
-       }\r
-\r
-\r
-       /**\r
-        * Check whether the given Iterator contains the given element.\r
-        * @param iterator the Iterator to check\r
-        * @param element the element to look for\r
-        * @return <code>true</code> if found, <code>false</code> else\r
-        */\r
-       public static boolean contains(Iterator iterator, Object element) {\r
-               if (iterator != null) {\r
-                       while (iterator.hasNext()) {\r
-                               Object candidate = iterator.next();\r
-                               if (ObjectUtils.nullSafeEquals(candidate, element)) {\r
-                                       return true;\r
-                               }\r
-                       }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Check whether the given Enumeration contains the given element.\r
-        * @param enumeration the Enumeration to check\r
-        * @param element the element to look for\r
-        * @return <code>true</code> if found, <code>false</code> else\r
-        */\r
-       public static boolean contains(Enumeration enumeration, Object element) {\r
-               if (enumeration != null) {\r
-                       while (enumeration.hasMoreElements()) {\r
-                               Object candidate = enumeration.nextElement();\r
-                               if (ObjectUtils.nullSafeEquals(candidate, element)) {\r
-                                       return true;\r
-                               }\r
-                       }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Check whether the given Collection contains the given element instance.\r
-        * <p>Enforces the given instance to be present, rather than returning\r
-        * <code>true</code> for an equal element as well.\r
-        * @param collection the Collection to check\r
-        * @param element the element to look for\r
-        * @return <code>true</code> if found, <code>false</code> else\r
-        */\r
-       public static boolean containsInstance(Collection collection, Object element) {\r
-               if (collection != null) {\r
-                       for (Iterator it = collection.iterator(); it.hasNext();) {\r
-                               Object candidate = it.next();\r
-                               if (candidate == element) {\r
-                                       return true;\r
-                               }\r
-                       }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Return <code>true</code> if any element in '<code>candidates</code>' is\r
-        * contained in '<code>source</code>'; otherwise returns <code>false</code>.\r
-        * @param source the source Collection\r
-        * @param candidates the candidates to search for\r
-        * @return whether any of the candidates has been found\r
-        */\r
-       public static boolean containsAny(Collection source, Collection candidates) {\r
-               if (isEmpty(source) || isEmpty(candidates)) {\r
-                       return false;\r
-               }\r
-               for (Iterator it = candidates.iterator(); it.hasNext();) {\r
-                       if (source.contains(it.next())) {\r
-                               return true;\r
-                       }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Return the first element in '<code>candidates</code>' that is contained in\r
-        * '<code>source</code>'. If no element in '<code>candidates</code>' is present in\r
-        * '<code>source</code>' returns <code>null</code>. Iteration order is\r
-        * {@link Collection} implementation specific.\r
-        * @param source the source Collection\r
-        * @param candidates the candidates to search for\r
-        * @return the first present object, or <code>null</code> if not found\r
-        */\r
-       public static Object findFirstMatch(Collection source, Collection candidates) {\r
-               if (isEmpty(source) || isEmpty(candidates)) {\r
-                       return null;\r
-               }\r
-               for (Iterator it = candidates.iterator(); it.hasNext();) {\r
-                       Object candidate = it.next();\r
-                       if (source.contains(candidate)) {\r
-                               return candidate;\r
-                       }\r
-               }\r
-               return null;\r
-       }\r
-\r
-       /**\r
-        * Find a single value of the given type in the given Collection.\r
-        * @param collection the Collection to search\r
-        * @param type the type to look for\r
-        * @return a value of the given type found if there is a clear match,\r
-        * or <code>null</code> if none or more than one such value found\r
-        */\r
-       public static Object findValueOfType(Collection collection, Class type) {\r
-               if (isEmpty(collection)) {\r
-                       return null;\r
-               }\r
-               Object value = null;\r
-               for (Iterator it = collection.iterator(); it.hasNext();) {\r
-                       Object obj = it.next();\r
-                       if (type == null || type.isInstance(obj)) {\r
-                               if (value != null) {\r
-                                       // More than one value found... no clear single value.\r
-                                       return null;\r
-                               }\r
-                               value = obj;\r
-                       }\r
-               }\r
-               return value;\r
-       }\r
-\r
-       /**\r
-        * Find a single value of one of the given types in the given Collection:\r
-        * searching the Collection for a value of the first type, then\r
-        * searching for a value of the second type, etc.\r
-        * @param collection the collection to search\r
-        * @param types the types to look for, in prioritized order\r
-        * @return a value of one of the given types found if there is a clear match,\r
-        * or <code>null</code> if none or more than one such value found\r
-        */\r
-       public static Object findValueOfType(Collection collection, Class[] types) {\r
-               if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {\r
-                       return null;\r
-               }\r
-               for (int i = 0; i < types.length; i++) {\r
-                       Object value = findValueOfType(collection, types[i]);\r
-                       if (value != null) {\r
-                               return value;\r
-                       }\r
-               }\r
-               return null;\r
-       }\r
-\r
-       /**\r
-        * Determine whether the given Collection only contains a single unique object.\r
-        * @param collection the Collection to check\r
-        * @return <code>true</code> if the collection contains a single reference or\r
-        * multiple references to the same instance, <code>false</code> else\r
-        */\r
-       public static boolean hasUniqueObject(Collection collection) {\r
-               if (isEmpty(collection)) {\r
-                       return false;\r
-               }\r
-               boolean hasCandidate = false;\r
-               Object candidate = null;\r
-               for (Iterator it = collection.iterator(); it.hasNext();) {\r
-                       Object elem = it.next();\r
-                       if (!hasCandidate) {\r
-                               hasCandidate = true;\r
-                               candidate = elem;\r
-                       }\r
-                       else if (candidate != elem) {\r
-                               return false;\r
-                       }\r
-               }\r
-               return true;\r
-       }\r
-\r
-}\r