org.omnaest.utils.structure.array
Class ArrayUtils

java.lang.Object
  extended by org.omnaest.utils.structure.array.ArrayUtils

public class ArrayUtils
extends Object

Helper methods for arrays.

Author:
Omnaest
See Also:
CollectionUtils

Constructor Summary
ArrayUtils()
           
 
Method Summary
static
<E> Class<E[]>
arrayType(Class<E> componentType)
          Returns an array type based on the element type
static Class<?> componentType(Class<?> arrayType)
          Returns the component type of a given array type.
static
<TO,FROM> TO[]
convertArray(FROM[] arrayFrom, Class<TO> arrayToType, ElementConverter<FROM,? extends TO> elementConverter)
          Converts a given array into a new array with a new element type.
static
<TO,FROM> TO[]
convertArray(FROM[] arrayFrom, TO[] arrayTo, ElementConverter<FROM,? extends TO> elementConverter)
          Similar to convertArray(Object[], Class, ElementConverter) but uses the given target array if the size fits otherwise it will construct a new array with the same component type
static
<TO,FROM> TO[]
convertArrayExcludingNullElements(FROM[] arrayFrom, TO[] arrayTo, ElementConverter<FROM,TO> elementConverter)
          Converts a given array into a new list with a new element type, whereby all elements which convert to null will be excluded in the target list.
static
<E> BitSet
differenceBitSet(E[] elements1, E[] elements2)
          Calculates a BitSet which has its bits set to true at the same index where the related element from one array to the other array is not Object.equals(Object)
static boolean isArray(Object object)
          Returns true if the object is an Array
static boolean isArrayType(Class<?> type)
          Returns true if the given type is not null and Class.isArray() is true
static int length(Object arrayObject)
          Returns the length of a given array instance or -1 if the given instance is not an array instance.
static
<E> E[]
merge(E[]... elementArrays)
          Returns an Array containing all elements of each of the given Arrays.
static int sumOf(int... values)
          Return the sum of all given Integer values
static Object[] toObject(Object primitiveArray)
          Returns a non primitive Array for a primitive one
static String[] trimStringArrayTokens(String[] stringArray)
          Returns a new array with all tokens of the given string array being trimmed
static
<E> E[]
valueOf(Class<E> type, E... elements)
           
static
<E> E[]
valueOf(Collection<? extends E> collection, Class<E> type)
          Returns an typed Array based on the given Collection and the given array component type

If null is given as Collection null is returned, too.
static
<E> E[]
valueOf(E... elements)
          Returns a new Array instance based on the Class types of the given elements.
static
<E> E[]
valueOf(Iterable<? extends E> iterable, Class<E> type)
          Returns an typed Array based on the given Iterable and the given array component type

If null is given as Iterable null is returned, too.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtils

public ArrayUtils()
Method Detail

merge

public static <E> E[] merge(E[]... elementArrays)
Returns an Array containing all elements of each of the given Arrays. If any given Array is null, it will be ignored.

As long as at least one Array instance is given this function returns an Array instance itself. If no Array instance is available null will be returned.

Parameters:
elementArrays -
Returns:

convertArray

public static <TO,FROM> TO[] convertArray(FROM[] arrayFrom,
                                          Class<TO> arrayToType,
                                          ElementConverter<FROM,? extends TO> elementConverter)
Converts a given array into a new array with a new element type.

Parameters:
arrayFrom -
arrayToType -
elementConverter - ElementConverter
Returns:

convertArray

public static <TO,FROM> TO[] convertArray(FROM[] arrayFrom,
                                          TO[] arrayTo,
                                          ElementConverter<FROM,? extends TO> elementConverter)
Similar to convertArray(Object[], Class, ElementConverter) but uses the given target array if the size fits otherwise it will construct a new array with the same component type

Parameters:
arrayFrom -
arrayTo - : instance of the array later returned. Use the return value to set this list. The array will not be necessarily changed only by the call as parameter.
elementConverter - ElementConverter
Returns:

convertArrayExcludingNullElements

public static <TO,FROM> TO[] convertArrayExcludingNullElements(FROM[] arrayFrom,
                                                               TO[] arrayTo,
                                                               ElementConverter<FROM,TO> elementConverter)
Converts a given array into a new list with a new element type, whereby all elements which convert to null will be excluded in the target list.

Parameters:
arrayFrom -
arrayTo -
elementConverter -
Returns:

trimStringArrayTokens

public static String[] trimStringArrayTokens(String[] stringArray)
Returns a new array with all tokens of the given string array being trimmed

Parameters:
stringArray -
Returns:
new array with all trimmed tokens

toObject

public static Object[] toObject(Object primitiveArray)
Returns a non primitive Array for a primitive one

Parameters:
primitiveArray -
Returns:

isArrayType

public static boolean isArrayType(Class<?> type)
Returns true if the given type is not null and Class.isArray() is true

Parameters:
type -
Returns:

isArray

public static boolean isArray(Object object)
Returns true if the object is an Array

Parameters:
object -
Returns:

componentType

public static Class<?> componentType(Class<?> arrayType)
Returns the component type of a given array type. Returns null if the given type is no array type or there can no component type be detected.

Parameters:
arrayType -
Returns:
See Also:
arrayType(Class)

length

public static int length(Object arrayObject)
Returns the length of a given array instance or -1 if the given instance is not an array instance.

Parameters:
arrayObject -
Returns:

valueOf

public static <E> E[] valueOf(Collection<? extends E> collection,
                              Class<E> type)
Returns an typed Array based on the given Collection and the given array component type

If null is given as Collection null is returned, too. If the given type is null, Object is used as component type.

Parameters:
collection - Collection
type - Class
Returns:
new Array instance
See Also:
Collection.toArray(Object[])

valueOf

public static <E> E[] valueOf(Iterable<? extends E> iterable,
                              Class<E> type)
Returns an typed Array based on the given Iterable and the given array component type

If null is given as Iterable null is returned, too. If the given type is null, Object is used as component type.

Parameters:
iterable - Collection
type - Class
Returns:
new Array instance
See Also:
Collection.toArray(Object[])

valueOf

public static <E> E[] valueOf(E... elements)
Returns a new Array instance based on the Class types of the given elements. The first shared type which can be cast to the expected return type is used.

The automatic detection of element types is based on reflection and has a high impact on performance if the given element types have large inherited type graphs.

Parameters:
elements -
Returns:
See Also:
valueOf(Class, Object...)

valueOf

public static <E> E[] valueOf(Class<E> type,
                              E... elements)
Parameters:
elements -
Returns:

sumOf

public static int sumOf(int... values)
Return the sum of all given Integer values

Parameters:
values -
Returns:

differenceBitSet

public static <E> BitSet differenceBitSet(E[] elements1,
                                          E[] elements2)
Calculates a BitSet which has its bits set to true at the same index where the related element from one array to the other array is not Object.equals(Object)

Parameters:
elements1 -
elements2 -
Returns:
BitSet with modified index bits set to true

arrayType

public static <E> Class<E[]> arrayType(Class<E> componentType)
Returns an array type based on the element type

Parameters:
componentType -
Returns:
See Also:
componentType(Class)


Copyright © 2013. All Rights Reserved.