1 package com.diasparsoftware.java.util; 2 3 import java.util.*; 4 5 import org.apache.commons.collections.*; 6 7 public class CollectionUtil { 8 9 /*** 10 * Allows you to detect whether any item in a collection satisfies 11 * some acceptance criterion. 12 * 13 * @param collection 14 * The collection to search in 15 * @param predicate 16 * A predicate that should evaluate to <code>true</code> 17 * for the matching object. 18 * @return 19 */ 20 public static boolean detect( 21 Collection collection, 22 Predicate predicate) { 23 24 return (CollectionUtils.find(collection, predicate) != null); 25 } 26 27 /*** 28 * Searches a collection of <code>String</code> s for the 29 * specified search string, but ignoring case. 30 * 31 * @param stringCollection 32 * A collection of <code>String</code> s 33 * @param searchString 34 * The string to forEachDoIgnoreExceptionsearchString 35 * </code> in here if we ignore case?" 36 */ 37 public static boolean stringCollectionContainsIgnoreCase( 38 Collection stringCollection, 39 final String searchString) { 40 41 return CollectionUtil 42 .detect(stringCollection, new Predicate() { 43 public boolean evaluate(Object parameters) { 44 String eachString = (String) parameters; 45 return eachString.equalsIgnoreCase(searchString); 46 } 47 }); 48 } 49 50 /*** 51 * Executes <code>closure</code> for each element in the 52 * specified collection, ignoring any thrown exceptions. This 53 * method is designed to be used by the <code>JdbcResourceRegistry</code>: 54 * at cleanup time, you can't recover from exceptions anyway. 55 * 56 * @param collection 57 * @param closure 58 */ 59 public static void forEachDoIgnoreException( 60 Collection collection, 61 ExceptionalClosure closure) { 62 63 for (Iterator i = collection.iterator(); i.hasNext();) { 64 Object each = (Object) i.next(); 65 try { 66 closure.execute(each); 67 } 68 catch (Exception ignored) { 69 } 70 } 71 } 72 73 /*** 74 * The first exception stops the iteration. 75 * 76 * @param collection 77 * @param closure 78 * @throws Exception 79 */ 80 public static void forEachDo( 81 Collection collection, 82 ExceptionalClosure closure) 83 throws Exception { 84 85 for (Iterator i = collection.iterator(); i.hasNext();) { 86 Object each = (Object) i.next(); 87 closure.execute(each); 88 } 89 } 90 91 /*** 92 * Execute the specified closure for each item in the specified 93 * collection. 94 * 95 * @param collection 96 * @param closure 97 * @throws Exception 98 */ 99 public static void forEachDo( 100 Collection collection, 101 Closure closure) { 102 103 for (Iterator i = collection.iterator(); i.hasNext();) { 104 Object each = (Object) i.next(); 105 closure.execute(each); 106 } 107 } 108 109 /*** 110 * Execute the specified closure for each item in the specified 111 * collection. 112 * 113 * @param collection 114 * @param closure 115 * @throws Exception 116 */ 117 public static void forEachDo(Map map, MapEntryClosure closure) { 118 forEachDo(map.entrySet(), closure); 119 } 120 121 /*** 122 * Execute the specified closure for each item in the specified 123 * collection. 124 * 125 * @param collection 126 * @param closure 127 * @throws Exception 128 */ 129 public static void forEachDo( 130 Map map, 131 ExceptionalMapEntryClosure closure) 132 throws Exception { 133 134 forEachDo(map.entrySet(), closure); 135 } 136 137 /*** 138 * Selects the first object that the <code>selector</code> 139 * accepts, or <code>null</code> if the selector rejects 140 * them all. 141 * 142 * @param set 143 * @param selector 144 * @return 145 */ 146 public static Object select(Set set, Selector selector) { 147 for (Iterator i = set.iterator(); i.hasNext();) { 148 Object element = (Object) i.next(); 149 if (selector.accept(element)) 150 return element; 151 } 152 153 return null; 154 } 155 }