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.Collection;
36 import java.util.Comparator;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40
41 import com.generationjava.lang.ClassW;
42
43 import org.apache.commons.collections.comparators.ComparableComparator;
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 public static 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 public static Collection cloneNewEmptyCollection(Collection coll) {
76 return (Collection)ClassW.createObject(coll.getClass());
77 }
78
79 public static 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 public static 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 // A search that doesn't involve the list needing to be sorted
96 public static int simpleSearch(List list, Object obj) {
97 return simpleSearch(list, obj, new ComparableComparator() );
98 }
99 public static int simpleSearch(List list, Object obj, Comparator cmp) {
100 int sz = list.size();
101 for( int i=0; i<sz; i++ ) {
102 Object tmp = list.get(i);
103 if(cmp.compare(obj, tmp) == 0) {
104 return i;
105 }
106 }
107 return -1; // ?
108 }
109
110 }