com.greenpepper.util.CollectionUtil Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2006 Pyxis Technologies inc.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
* or see the FSF site: http://www.fsf.org.
*/
package com.greenpepper.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
/**
* CollectionUtil class.
*
* @author oaouattara
* @version $Id: $Id
*/
public final class CollectionUtil
{
private CollectionUtil()
{
}
/**
* isEmpty.
*
* @param objects a T object.
* @param a T object.
* @return a boolean.
*/
public static boolean isEmpty( T... objects )
{
return objects.length == 0;
}
/**
* toArray.
*
* @param objects a T object.
* @param a T object.
* @return an array of T objects.
*/
public static T[] toArray( T... objects )
{
return objects;
}
/**
* toVector.
*
* @param objects a T object.
* @param a T object.
* @return a {@link java.util.Vector} object.
*/
public static Vector toVector( T... objects )
{
return new Vector( Arrays.asList( objects ) );
}
/**
* toList.
*
* @param objects a T object.
* @param a T object.
* @return a {@link java.util.List} object.
*/
public static List toList( T... objects )
{
List list = new ArrayList( objects.length );
for (T t : objects)
{
list.add( t );
}
return list;
}
/**
* toArray.
*
* @param list a {@link java.util.List} object.
* @param type a {@link java.lang.Class} object.
* @param a T object.
* @return an array of T objects.
*/
@SuppressWarnings("unchecked")
public static T[] toArray( List> list, Class type )
{
T[] array = (T[])Array.newInstance( type, list.size() );
return list.toArray( array );
}
/**
* toArray.
*
* @param list a {@link java.util.List} object.
* @return an array of {@link java.lang.String} objects.
*/
public static String[] toArray( List list )
{
return toArray( list, String.class );
}
/**
* even.
*
* @param objects a {@link java.lang.Iterable} object.
* @param a T object.
* @return a {@link java.util.List} object.
*/
public static List even( Iterable objects )
{
return split( objects, true );
}
/**
* odd.
*
* @param objects a {@link java.lang.Iterable} object.
* @param a T object.
* @return a {@link java.util.List} object.
*/
public static List odd( Iterable objects )
{
return split( objects, false );
}
/**
* joinAsString.
*
* @param objects an array of {@link java.lang.Object} objects.
* @param separator a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public static String joinAsString( Object[] objects, String separator )
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < objects.length; i++)
{
Object element = objects[i];
result.append( String.valueOf( element ) );
if (i < objects.length - 1) result.append( separator );
}
return result.toString();
}
/**
* filter.
*
* @param collection a {@link java.lang.Iterable} object.
* @param predicate a {@link com.greenpepper.util.CollectionUtil.Predicate} object.
* @param a T object.
* @return a {@link java.util.List} object.
*/
public static List filter( Iterable collection, Predicate predicate)
{
List filtered = new ArrayList();
for (T element : collection)
{
if (predicate.isVerifiedBy(element))
{
filtered.add(element);
}
}
return filtered;
}
public interface Predicate
{
boolean isVerifiedBy(T element);
}
/**
* split.
*
* @param objects a {@link java.lang.Iterable} object.
* @param even a boolean.
* @param a T object.
* @return a {@link java.util.List} object.
*/
public static List split( Iterable objects, boolean even )
{
List parts = new ArrayList();
for (T element : objects)
{
if (even) parts.add( element );
even = !even;
}
return parts;
}
/**
* shift.
*
* @param list a {@link java.util.List} object.
* @param a T object.
* @return a T object.
*/
public static T shift( List list )
{
return remove( list, 0 );
}
/**
* removeLast.
*
* @param list a {@link java.util.List} object.
* @param a T object.
* @return a T object.
*/
public static T removeLast( List list )
{
return remove( list, list.size() - 1 );
}
private static T remove( List list, int position )
{
if (list.isEmpty()) return null;
return list.remove( position );
}
/**
* first.
*
* @param list a {@link java.util.List} object.
* @param a T object.
* @return a T object.
*/
public static T first( List list )
{
return get( list, 0 );
}
/**
* last.
*
* @param list a {@link java.util.List} object.
* @param a T object.
* @return a T object.
*/
public static T last( List list )
{
return get( list, list.size() - 1 );
}
private static T get( List list, int index )
{
if (list.size() <= index) return null;
return list.get( index );
}
/**
* toPrimitiveIntArray.
*
* @param values a {@link java.util.List} object.
* @return a {@link java.lang.Object} object.
*/
public static Object toPrimitiveIntArray(List> values)
{
int[] array = new int[values.size()];
int cursor = 0;
for(Object o : values)
{
array[cursor] = (Integer) o;
cursor ++;
}
return array;
}
/**
* toPrimitiveFloatArray.
*
* @param values a {@link java.util.List} object.
* @return a {@link java.lang.Object} object.
*/
public static Object toPrimitiveFloatArray(List> values)
{
float[] array = new float[values.size()];
int cursor = 0;
for(Object o : values)
{
array[cursor] = (Float) o;
cursor ++;
}
return array;
}
/**
* toPrimitiveLongArray.
*
* @param values a {@link java.util.List} object.
* @return a {@link java.lang.Object} object.
*/
public static Object toPrimitiveLongArray(List> values)
{
long[] array = new long[values.size()];
int cursor = 0;
for(Object o : values)
{
array[cursor] = (Long) o;
cursor ++;
}
return array;
}
/**
* toPrimitiveDoubleArray.
*
* @param values a {@link java.util.List} object.
* @return a {@link java.lang.Object} object.
*/
public static Object toPrimitiveDoubleArray(List> values)
{
double[] array = new double[values.size()];
int cursor = 0;
for(Object o : values)
{
array[cursor] = (Double) o;
cursor ++;
}
return array;
}
/**
* toPrimitiveBoolArray.
*
* @param values a {@link java.util.List} object.
* @return a {@link java.lang.Object} object.
*/
public static Object toPrimitiveBoolArray(List> values)
{
boolean[] array = new boolean[values.size()];
int cursor = 0;
for(Object o : values)
{
array[cursor] = (Boolean) o;
cursor ++;
}
return array;
}
}