Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]

    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.util;
   13   
   14   import java.util.ArrayList;
   15   import java.util.Iterator;
   16   import java.util.List;
   17   
   18   import org.apache.commons.collections.IteratorUtils;
   19   
   20   
   21   /**
   22    * Utility methods for collection handling.
   23    * @author Fabrizio Giustina
   24    * @version $Revision: 1081 $ ($Author: fgiust $)
   25    */
   26   public final class CollectionUtil
   27   {
   28   
   29       /**
   30        * Don't instantiate a CollectionUtil.
   31        */
   32       private CollectionUtil()
   33       {
   34           // unused
   35       }
   36   
   37       /**
   38        * Create a list of objects taken from the given iterator and crop the resulting list according to the startIndex
   39        * and numberOfItems parameters.
   40        * @param iterator Iterator
   41        * @param startIndex int starting index
   42        * @param numberOfItems int number of items to keep in the list
   43        * @return List with values taken from the given object, cropped according to startIndex and numberOfItems
   44        * parameters
   45        */
   46       private static List getSubList(Iterator iterator, int startIndex, int numberOfItems)
   47       {
   48   
   49           List croppedList = new ArrayList(numberOfItems);
   50   
   51           int skippedRecordCount = 0;
   52           int copiedRecordCount = 0;
   53           while (iterator.hasNext())
   54           {
   55   
   56               Object object = iterator.next();
   57   
   58               if (++skippedRecordCount <= startIndex)
   59               {
   60                   continue;
   61               }
   62   
   63               croppedList.add(object);
   64   
   65               if ((numberOfItems != 0) && (++copiedRecordCount >= numberOfItems))
   66               {
   67                   break;
   68               }
   69           }
   70   
   71           return croppedList;
   72   
   73       }
   74   
   75       /**
   76        * create an iterator on a given object (Collection, Enumeration, array, single Object) and crop the resulting list
   77        * according to the startIndex and numberOfItems parameters.
   78        * @param iterableObject Collection, Enumeration or array to crop
   79        * @param startIndex int starting index
   80        * @param numberOfItems int number of items to keep in the list
   81        * @return List with values taken from the given object, cropped according the startIndex and numberOfItems
   82        * parameters
   83        */
   84       public static List getListFromObject(Object iterableObject, int startIndex, int numberOfItems)
   85       {
   86           if (iterableObject instanceof List)
   87           {
   88               // easier, use sublist
   89               List list = ((List) iterableObject);
   90   
   91               // check for partial lists
   92               int lastRecordExclusive = numberOfItems <= 0 ? list.size() : startIndex + numberOfItems;
   93               if (lastRecordExclusive > list.size())
   94               {
   95                   lastRecordExclusive = list.size();
   96               }
   97   
   98               if (startIndex < list.size())
   99               {
  100                   return list.subList(startIndex, lastRecordExclusive);
  101               }
  102           }
  103   
  104           // use an iterator
  105           Iterator iterator = IteratorUtils.getIterator(iterableObject);
  106           return getSubList(iterator, startIndex, numberOfItems);
  107       }
  108   }

Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]