net.sourceforge.stripes.util
Class CollectionUtil

java.lang.Object
  extended by net.sourceforge.stripes.util.CollectionUtil

public class CollectionUtil
extends Object

Utility methods for working with Collections and Arrays.

Since:
Stripes 1.4
Author:
Tim Fennell

Constructor Summary
CollectionUtil()
           
 
Method Summary
static boolean applies(String[] events, String event)
          Checks to see if an event is applicable given an array of event names.
static
<T> List<T>
asList(Iterable<T> in)
          Converts an Iterable into a List that can be navigated in ways other than simple iteration.
static List<Object> asList(Object in)
          Converts an Object reference that is known to be an array into a List.
static Object[] asObjectArray(Object in)
          Converts an Object reference that is known to be an array into an Object[].
static boolean contains(Object[] arr, Object item)
          Checks to see if an array contains an item.
static boolean empty(String[] arr)
          Checks to see if the array contains any values that are non-null non empty-string values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtil

public CollectionUtil()
Method Detail

contains

public static boolean contains(Object[] arr,
                               Object item)
Checks to see if an array contains an item. Works on unsorted arrays. If the array is null this method will always return false. If the item is null, will return true if the array contains a null entry, false otherwise. In all other cases, item.equals() is used to determine equality.

Parameters:
arr - the array to scan for the item.
item - the item to be looked for
Returns:
true if item is contained in the array, false otherwise

empty

public static boolean empty(String[] arr)
Checks to see if the array contains any values that are non-null non empty-string values. If it does, returns false. Returns true for null arrays and zero length arrays, as well as for arrays consisting only of nulls and empty strings.


applies

public static boolean applies(String[] events,
                              String event)

Checks to see if an event is applicable given an array of event names. The array is usually derived from the on attribute of one of the Stripes annotations (e.g. ValidationMethod). The array can be composed of positive event names (e.g. {"foo", "bar"}) in which case the event must be contained in the array, or negative event names (e.g. {"!splat", "!whee"}) in which case the event must not be contained in the array.

Calling this method with a null or zero length array will always return true.

Parameters:
events - an array containing event names or event names prefixed with bangs
event - the event name to check for applicability given the array
Returns:
true if the array indicates the event is applicable, false otherwise

asObjectArray

public static Object[] asObjectArray(Object in)
Converts an Object reference that is known to be an array into an Object[]. If the array is assignable to Object[], the array passed in is simply cast and returned. Otherwise a new Object[] of equal size is constructed and the elements are wrapped and inserted into the new array before being returned.

Parameters:
in - an array of Objects or primitives
Returns:
an Object[], either the array passed in, or in the case of primitives, a new Object[] containing a wrapper for each element in the input array
Throws:
IllegalArgumentException - thrown if the in parameter is null or not an array

asList

public static List<Object> asList(Object in)

Converts an Object reference that is known to be an array into a List. Semantically very similar to Arrays.asList(Object[]) except that this method can deal with arrays of primitives in the same manner as arrays as objects.

A new List is created of the same size as the array, and elements are copied from the array into the List. If elements are primitives then they are converted to the appropriate wrapper types in order to return a List.

Parameters:
in - an array of Objects or primitives (null values are not allowed)
Returns:
a List containing an element for each element in the input array
Throws:
IllegalArgumentException - thrown if the in parameter is null or not an array

asList

public static <T> List<T> asList(Iterable<T> in)
Converts an Iterable into a List that can be navigated in ways other than simple iteration. If the underlying implementation of the Iterable is a List, it is cast to List and returned. Otherwise it is iterated and the items placed, in order, into a new List.

Parameters:
in - an Iterable to serve as the source for a List
Returns:
either the Iterable itself if it is a List, or a new List with the same elements


? Copyright 2005-2006, Stripes Development Team.