View Javadoc
1 /* 2 * Copyright (c) 2003, Henri Yandell 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the 7 * following conditions are met: 8 * 9 * + Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * + Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * + Neither the name of Genjava-Core nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.generationjava.collections; 34 35 import java.util.Arrays; 36 import java.util.ArrayList; 37 import java.util.Collection; 38 import java.util.Enumeration; 39 import java.util.Iterator; 40 import java.util.List; 41 import java.util.Map; 42 43 import com.generationjava.lang.ClassW; 44 45 /*** 46 * A wrapper around the Collections. Provides functionality above and 47 * beyond the duty of java.util's Collection API. 48 */ 49 final public class CollectionsW { 50 51 static public Collection slice(Collection coll, int start, int end) { 52 if(coll == null) { 53 return null; 54 } 55 Iterator iterator = coll.iterator(); 56 Collection sub = cloneNewEmptyCollection(coll); 57 end -= start; 58 while(iterator.hasNext()) { 59 if(start == 0) { 60 if(end == 0) { 61 break; 62 } else { 63 sub.add(iterator.next()); 64 end--; 65 } 66 } else { 67 iterator.next(); // ignore 68 start--; 69 } 70 } 71 return sub; 72 } 73 74 // unimplemented 75 static public Collection cloneNewEmptyCollection(Collection coll) { 76 return (Collection)ClassW.createObject(coll.getClass()); 77 } 78 79 static public Map cloneNewEmptyMap(Map map) { 80 return (Map)ClassW.createObject(map.getClass()); 81 } 82 83 /// TODO: Improve this, so it takes start and end and is a slice() 84 static public String[] getSubArray(String[] ob, int idx) { 85 if (idx > ob.length) { 86 return new String[0]; 87 } 88 String[] ob2 = new String[ob.length - idx]; 89 for (int i = idx; i < ob.length; i++) { 90 ob2[i - idx] = ob[i]; 91 } 92 return ob2; 93 } 94 95 static public Object[] iteratorToArray(Iterator iterator) { 96 return iteratorToArray(iterator, new Object[0]); 97 } 98 99 static public Object[] iteratorToArray(Iterator iterator, Object[] type) { 100 ArrayList list = new ArrayList(); 101 102 while(iterator.hasNext()) { 103 list.add(iterator.next()); 104 } 105 return list.toArray(type); 106 } 107 108 }

This page was automatically generated by Maven