1 16 package net.sf.cglib.core; 17 18 import java.util.*; 19 import java.lang.reflect.Array ; 20 21 25 public class CollectionUtils { 26 private CollectionUtils() { } 27 28 public static Map bucket(Collection c, Transformer t) { 29 Map buckets = new HashMap(); 30 for (Iterator it = c.iterator(); it.hasNext();) { 31 Object value = (Object )it.next(); 32 Object key = t.transform(value); 33 List bucket = (List)buckets.get(key); 34 if (bucket == null) { 35 buckets.put(key, bucket = new LinkedList()); 36 } 37 bucket.add(value); 38 } 39 return buckets; 40 } 41 42 public static void reverse(Map source, Map target) { 43 for (Iterator it = source.keySet().iterator(); it.hasNext();) { 44 Object key = it.next(); 45 target.put(source.get(key), key); 46 } 47 } 48 49 public static Collection filter(Collection c, Predicate p) { 50 Iterator it = c.iterator(); 51 while (it.hasNext()) { 52 if (!p.evaluate(it.next())) { 53 it.remove(); 54 } 55 } 56 return c; 57 } 58 59 public static List transform(Collection c, Transformer t) { 60 List result = new ArrayList(c.size()); 61 for (Iterator it = c.iterator(); it.hasNext();) { 62 result.add(t.transform(it.next())); 63 } 64 return result; 65 } 66 67 public static Map getIndexMap(List list) { 68 Map indexes = new HashMap(); 69 int index = 0; 70 for (Iterator it = list.iterator(); it.hasNext();) { 71 indexes.put(it.next(), new Integer (index++)); 72 } 73 return indexes; 74 } 75 } 76 77 | Popular Tags |