Save This Page
Home » cglib-src-2.2 » net.sf.cglib.core » [javadoc | source]
    1   /*
    2    * Copyright 2003,2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    *  Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   package net.sf.cglib.core;
   17   
   18   import java.util;
   19   import java.lang.reflect.Array;
   20   
   21   /**
   22    * @author Chris Nokleberg
   23    * @version $Id: CollectionUtils.java,v 1.7 2004/06/24 21:15:21 herbyderby Exp $
   24    */
   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       

Save This Page
Home » cglib-src-2.2 » net.sf.cglib.core » [javadoc | source]