1 54 package org.logicalcobwebs.cglib.core; 55 56 import java.util.*; 57 import java.lang.reflect.Array ; 58 59 63 public class CollectionUtils { 64 private CollectionUtils() { } 65 66 public static Map bucket(Collection c, Transformer t) { 67 Map buckets = new HashMap(); 68 for (Iterator it = c.iterator(); it.hasNext();) { 69 Object value = (Object )it.next(); 70 Object key = t.transform(value); 71 List bucket = (List)buckets.get(key); 72 if (bucket == null) { 73 buckets.put(key, bucket = new LinkedList()); 74 } 75 bucket.add(value); 76 } 77 return buckets; 78 } 79 80 public static void reverse(Map source, Map target) { 81 for (Iterator it = source.keySet().iterator(); it.hasNext();) { 82 Object key = it.next(); 83 target.put(source.get(key), key); 84 } 85 } 86 87 public static Object [] filter(Object [] a, Predicate p) { 88 List c = new ArrayList(Arrays.asList(a)); 89 filter(c, p); 90 return c.toArray((Object [])Array.newInstance(a.getClass().getComponentType(), c.size())); 91 } 92 93 public static Collection filter(Collection c, Predicate p) { 94 Iterator it = c.iterator(); 95 while (it.hasNext()) { 96 if (!p.evaluate(it.next())) { 97 it.remove(); 98 } 99 } 100 return c; 101 } 102 103 public static List transform(List c, Transformer t) { 104 List result = new ArrayList(c.size()); 105 for (Iterator it = c.iterator(); it.hasNext();) { 106 result.add(t.transform(it.next())); 107 } 108 return result; 109 } 110 } 111 112 | Popular Tags |