01: package com.mockrunner.util.common;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: /**
07: * Util class for collections
08: */
09: public class CollectionUtil {
10: /**
11: * Fills a <code>List</code> with <code>null</code>
12: * by calling {@link #fillList(List, int, Object)}
13: * with a <code>null</code> object.
14: * @param list the <code>List</code> that should be filled
15: * @param size the resulting size of the <code>List</code>
16: */
17: public static void fillList(List list, int size) {
18: fillList(list, size, null);
19: }
20:
21: /**
22: * Fills a <code>List</code> with with the specified object
23: * until it has the specified size. If the specified size is
24: * equal or lower the <code>List</code> size, nothing happens.
25: * @param list the <code>List</code> that should be filled
26: * @param size the resulting size of the <code>List</code>
27: */
28: public static void fillList(List list, int size, Object object) {
29: for (int ii = list.size(); ii < size; ii++) {
30: list.add(object);
31: }
32: }
33:
34: /**
35: * Returns a truncated version of the specified <code>List</code>.
36: * @param list the <code>List</code>
37: * @param len the truncate length
38: * @return the truncated <code>List</code>
39: */
40: public static List truncateList(List list, int len) {
41: if (len >= list.size())
42: return list;
43: ArrayList newList = new ArrayList(len);
44: for (int ii = 0; ii < len; ii++) {
45: newList.add(list.get(ii));
46: }
47: return newList;
48: }
49: }
|