1 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 26 public final class CollectionUtil 27 { 28 29 32 private CollectionUtil() 33 { 34 } 36 37 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 84 public static List getListFromObject(Object iterableObject, int startIndex, int numberOfItems) 85 { 86 if (iterableObject instanceof List ) 87 { 88 List list = ((List ) iterableObject); 90 91 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 Iterator iterator = IteratorUtils.getIterator(iterableObject); 106 return getSubList(iterator, startIndex, numberOfItems); 107 } 108 } | Popular Tags |