> Sounds fine with me. Could anyone give me a brief outline of what the
> methods do - just in case there's some overlap with whats already there.
> Even just a quick cut'n'paste of Bay's current CollectionUtils class as is
> would do...
>
Compiled from CollectionsUtils.java
public final class org.apache.commons.util.CollectionsUtils {
// does its best to index an Object and return the element at
// that index. needs to handle primitives.
public static java.lang.Object index(java.lang.Object, int);
public static java.lang.Object index(java.lang.Object,
java.lang.Object);
// does its best to make an iterator for an object.
//
public static java.util.Iterator getIterator(java.lang.Object);
// get sub-collection of a collection. need to add an array version too
public static java.util.Collection slice(java.util.Collection, int,
int);
// reverses an array. nothing fancy.
public static void reverseArray(java.lang.Object[]);
}
package org.apache.commons.util;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Commons" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* A wrapper around the Collections. Provides functionality above and
* beyond the duty of java.util's Collection API.
*
* @author [EMAIL PROTECTED]
* @version $Id: CollectionsUtils.java,v 1.2 2001/08/24 19:17:40 dlr Exp $
*/
final public class CollectionsUtils {
/**
* Given an Object, and an index, it will get the nth value in the
* object.
*/
static public Object index(Object obj, int idx) {
return index(obj, new Integer(idx));
}
static public Object index(Object obj, Object index) {
if(obj instanceof Map) {
Map map = (Map)obj;
if(map.containsKey(index)) {
return map.get(index);
}
}
int idx = -1;
if(index instanceof Integer) {
idx = ((Integer)index).intValue();
}
if(idx < 0) {
return obj;
} else
if(obj instanceof Map) {
Map map = (Map)obj;
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext()) {
idx--;
if(idx == -1) {
return iterator.next();
} else {
iterator.next();
}
}
} else
if(obj instanceof List) {
return ((List)obj).get(idx);
} else
if(obj instanceof Object[]) {
return ((Object[])obj)[idx];
} else
if(obj instanceof Enumeration) {
Enumeration enum = (Enumeration)obj;
while(enum.hasMoreElements()) {
idx--;
if(idx == -1) {
return enum.nextElement();
} else {
enum.nextElement();
}
}
} else
if(obj instanceof Iterator) {
Iterator iterator = (Iterator)obj;
while(iterator.hasNext()) {
idx--;
if(idx == -1) {
return iterator.next();
} else {
iterator.next();
}
}
}
return obj;
}
static public Iterator getIterator(Object obj) {
if(obj instanceof Iterator) {
return (Iterator)obj;
} else
if(obj instanceof Collection) {
return ((Collection)obj).iterator();
} else
if(obj instanceof Object[]) {
return Arrays.asList( (Object[])obj ).iterator();
} else
if(obj instanceof Enumeration) {
return new EnumerationIterator( (Enumeration)obj );
} else
if(obj instanceof Map) {
return ((Map)obj).values().iterator();
} else {
return null;
}
}
static public Collection slice(Collection coll, int start, int end) {
if(coll == null) {
return null;
}
Iterator iterator = coll.iterator();
Collection sub = (Collection)ClassUtils.createObject(coll.getClass());
end -= start;
while(iterator.hasNext()) {
if(start == 0) {
if(end == 0) {
break;
} else {
sub.add(iterator.next());
}
} else {
iterator.next(); // ignore
start--;
}
}
return sub;
}
static public void reverseArray(Object[] array) {
int i = 0;
int j = array.length - 1;
Object tmp;
while(j>i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}