1 16 17 package org.springframework.util; 18 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 ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 27 35 public abstract class CollectionUtils { 36 37 43 public static boolean isEmpty(Collection collection) { 44 return (collection == null || collection.isEmpty()); 45 } 46 47 53 public static boolean isEmpty(Map map) { 54 return (map == null || map.isEmpty()); 55 } 56 57 66 public static List arrayToList(Object source) { 67 return Arrays.asList(ObjectUtils.toObjectArray(source)); 68 } 69 70 75 public static void mergeArrayIntoCollection(Object array, Collection collection) { 76 if (collection == null) { 77 throw new IllegalArgumentException ("Collection must not be null"); 78 } 79 Object [] arr = ObjectUtils.toObjectArray(array); 80 for (int i = 0; i < arr.length; i++) { 81 collection.add(arr[i]); 82 } 83 } 84 85 93 public static void mergePropertiesIntoMap(Properties props, Map map) { 94 if (map == null) { 95 throw new IllegalArgumentException ("Map must not be null"); 96 } 97 if (props != null) { 98 for (Enumeration en = props.propertyNames(); en.hasMoreElements();) { 99 String key = (String ) en.nextElement(); 100 map.put(key, props.getProperty(key)); 101 } 102 } 103 } 104 105 106 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)) { 117 return true; 118 } 119 } 120 } 121 return false; 122 } 123 124 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)) { 135 return true; 136 } 137 } 138 } 139 return false; 140 } 141 142 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) { 155 return true; 156 } 157 } 158 } 159 return false; 160 } 161 162 169 public static boolean containsAny(Collection source, Collection candidates) { 170 if (isEmpty(source) || isEmpty(candidates)) { 171 return false; 172 } 173 for (Iterator it = candidates.iterator(); it.hasNext();) { 174 if (source.contains(it.next())) { 175 return true; 176 } 177 } 178 return false; 179 } 180 181 190 public static Object findFirstMatch(Collection source, Collection candidates) { 191 if (isEmpty(source) || isEmpty(candidates)) { 192 return null; 193 } 194 for (Iterator it = candidates.iterator(); it.hasNext();) { 195 Object candidate = it.next(); 196 if (source.contains(candidate)) { 197 return candidate; 198 } 199 } 200 return null; 201 } 202 203 210 public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException { 211 if (isEmpty(collection)) { 212 return null; 213 } 214 Class typeToUse = (type != null ? type : Object .class); 215 Object value = null; 216 for (Iterator it = collection.iterator(); it.hasNext();) { 217 Object obj = it.next(); 218 if (typeToUse.isInstance(obj)) { 219 if (value != null) { 220 throw new IllegalArgumentException ("More than one value of type [" + typeToUse.getName() + "] found"); 221 } 222 value = obj; 223 } 224 } 225 return value; 226 } 227 228 237 public static Object findValueOfType(Collection collection, Class [] types) throws IllegalArgumentException { 238 if (isEmpty(collection) || ObjectUtils.isEmpty(types)) { 239 return null; 240 } 241 for (int i = 0; i < types.length; i++) { 242 Object value = findValueOfType(collection, types[i]); 243 if (value != null) { 244 return value; 245 } 246 } 247 return null; 248 } 249 250 256 public static boolean hasUniqueObject(Collection collection) { 257 if (isEmpty(collection)) { 258 return false; 259 } 260 boolean hasCandidate = false; 261 Object candidate = null; 262 for (Iterator it = collection.iterator(); it.hasNext();) { 263 Object elem = it.next(); 264 if (!hasCandidate) { 265 hasCandidate = true; 266 candidate = elem; 267 } 268 else if (candidate != elem) { 269 return false; 270 } 271 } 272 return true; 273 } 274 275 } 276 | Popular Tags |