001    package com.mockrunner.util.common;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    /**
007     * Util class for collections
008     */
009    public class CollectionUtil
010    {
011        /**
012         * Fills a <code>List</code> with <code>null</code>
013         * by calling {@link #fillList(List, int, Object)}
014         * with a <code>null</code> object.
015         * @param list the <code>List</code> that should be filled
016         * @param size the resulting size of the <code>List</code>
017         */
018        public static void fillList(List list, int size)
019        {
020            fillList(list, size, null);
021        }
022        
023        /**
024         * Fills a <code>List</code> with with the specified object
025         * until it has the specified size. If the specified size is
026         * equal or lower the <code>List</code> size, nothing happens.
027         * @param list the <code>List</code> that should be filled
028         * @param size the resulting size of the <code>List</code>
029         */
030        public static void fillList(List list, int size, Object object)
031        {
032            for(int ii = list.size(); ii <  size; ii++)
033            {
034                list.add(object);
035            }
036        }
037        
038        /**
039         * Returns a truncated version of the specified <code>List</code>.
040         * @param list the <code>List</code>
041         * @param len the truncate length
042         * @return the truncated <code>List</code>
043         */
044        public static List truncateList(List list, int len)
045        {
046            if(len >= list.size()) return list;
047            ArrayList newList = new ArrayList(len);
048            for(int ii = 0; ii < len; ii++)
049            {
050                newList.add(list.get(ii));
051            }
052            return newList;
053        }
054    }