com.fireflysource.common.collection.array.ArrayUtils Maven / Gradle / Ivy
package com.fireflysource.common.collection.array;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Utility methods for Array manipulation
*/
@SuppressWarnings("unused")
public class ArrayUtils {
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
@SuppressWarnings("rawtypes")
public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final long[] EMPTY_LONG_ARRAY = new long[0];
public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
public static final int[] EMPTY_INT_ARRAY = new int[0];
public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
public static final short[] EMPTY_SHORT_ARRAY = new short[0];
public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
public static T[] removeFromArray(T[] array, Object item) {
if (item == null || array == null)
return array;
for (int i = array.length; i-- > 0; ) {
if (item.equals(array[i])) {
Class> c = array.getClass().getComponentType();
@SuppressWarnings("unchecked")
T[] na = (T[]) Array.newInstance(c, Array.getLength(array) - 1);
if (i > 0)
System.arraycopy(array, 0, na, 0, i);
if (i + 1 < array.length)
System.arraycopy(array, i + 1, na, i, array.length - (i + 1));
return na;
}
}
return array;
}
/**
* Add element to an array
*
* @param array The array to add to (or null)
* @param item The item to add
* @param type The type of the array (in case of null array)
* @param the array entry type
* @return new array with contents of array plus item
*/
public static T[] addToArray(T[] array, T item, Class> type) {
if (array == null) {
if (type == null && item != null) {
type = item.getClass();
}
@SuppressWarnings("unchecked")
T[] na = (T[]) Array.newInstance(type, 1);
na[0] = item;
return na;
} else {
T[] na = Arrays.copyOf(array, array.length + 1);
na[array.length] = item;
return na;
}
}
/**
* Add element to the start of an array
*
* @param array The array to add to (or null)
* @param item The item to add
* @param type The type of the array (in case of null array)
* @param the array entry type
* @return new array with contents of array plus item
*/
public static T[] prependToArray(T item, T[] array, Class> type) {
if (array == null) {
if (type == null && item != null) {
type = item.getClass();
}
@SuppressWarnings("unchecked")
T[] na = (T[]) Array.newInstance(type, 1);
na[0] = item;
return na;
} else {
Class> c = array.getClass().getComponentType();
@SuppressWarnings("unchecked")
T[] na = (T[]) Array.newInstance(c, Array.getLength(array) + 1);
System.arraycopy(array, 0, na, 1, array.length);
na[0] = item;
return na;
}
}
/**
* @param array Any array of object
* @param the array entry type
* @return A new modifiable list initialised with the elements from
* array
.
*/
public static List asMutableList(E[] array) {
if (array == null || array.length == 0)
return new ArrayList<>();
return new ArrayList<>(Arrays.asList(array));
}
public static T[] removeNulls(T[] array) {
for (T t : array) {
if (t == null) {
List list = new ArrayList<>();
for (T t2 : array) {
if (t2 != null) {
list.add(t2);
}
}
return list.toArray(Arrays.copyOf(array, list.size()));
}
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Object[] nullToEmpty(Object[] array) {
if (array == null || array.length == 0) {
return EMPTY_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static String[] nullToEmpty(String[] array) {
if (array == null || array.length == 0) {
return EMPTY_STRING_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static long[] nullToEmpty(long[] array) {
if (array == null || array.length == 0) {
return EMPTY_LONG_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static int[] nullToEmpty(int[] array) {
if (array == null || array.length == 0) {
return EMPTY_INT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static short[] nullToEmpty(short[] array) {
if (array == null || array.length == 0) {
return EMPTY_SHORT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static char[] nullToEmpty(char[] array) {
if (array == null || array.length == 0) {
return EMPTY_CHAR_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static byte[] nullToEmpty(byte[] array) {
if (array == null || array.length == 0) {
return EMPTY_BYTE_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static double[] nullToEmpty(double[] array) {
if (array == null || array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static float[] nullToEmpty(float[] array) {
if (array == null || array.length == 0) {
return EMPTY_FLOAT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static boolean[] nullToEmpty(boolean[] array) {
if (array == null || array.length == 0) {
return EMPTY_BOOLEAN_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Long[] nullToEmpty(Long[] array) {
if (array == null || array.length == 0) {
return EMPTY_LONG_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Integer[] nullToEmpty(Integer[] array) {
if (array == null || array.length == 0) {
return EMPTY_INTEGER_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Short[] nullToEmpty(Short[] array) {
if (array == null || array.length == 0) {
return EMPTY_SHORT_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Character[] nullToEmpty(Character[] array) {
if (array == null || array.length == 0) {
return EMPTY_CHARACTER_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Byte[] nullToEmpty(Byte[] array) {
if (array == null || array.length == 0) {
return EMPTY_BYTE_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Double[] nullToEmpty(Double[] array) {
if (array == null || array.length == 0) {
return EMPTY_DOUBLE_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Float[] nullToEmpty(Float[] array) {
if (array == null || array.length == 0) {
return EMPTY_FLOAT_OBJECT_ARRAY;
}
return array;
}
/**
* Defensive programming technique to change a null
* reference to an empty one.
*
*
This method returns an empty array for a null
input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty public static
references in this class.
*
* @param array the array to check for null
or empty
* @return the same array, public static
empty array if null
or empty input
* @since 2.5
*/
public static Boolean[] nullToEmpty(Boolean[] array) {
if (array == null || array.length == 0) {
return EMPTY_BOOLEAN_OBJECT_ARRAY;
}
return array;
}
// Primitive/Object array converters
// ----------------------------------------------------------------------
// Character array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Characters to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Character
array, may be null
* @return a char
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static char[] toPrimitive(Character[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_CHAR_ARRAY;
}
final char[] result = new char[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Character to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Character
array, may be null
* @param valueForNull the value to insert if null
found
* @return a char
array, null
if null array input
*/
public static char[] toPrimitive(Character[] array, char valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_CHAR_ARRAY;
}
final char[] result = new char[array.length];
for (int i = 0; i < array.length; i++) {
Character b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive chars to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a char
array
* @return a Character
array, null
if null array input
*/
public static Character[] toObject(char[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_CHARACTER_OBJECT_ARRAY;
}
final Character[] result = new Character[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Long array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Longs to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Long
array, may be null
* @return a long
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static long[] toPrimitive(Long[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_LONG_ARRAY;
}
final long[] result = new long[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Long to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Long
array, may be null
* @param valueForNull the value to insert if null
found
* @return a long
array, null
if null array input
*/
public static long[] toPrimitive(Long[] array, long valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_LONG_ARRAY;
}
final long[] result = new long[array.length];
for (int i = 0; i < array.length; i++) {
Long b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive longs to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a long
array
* @return a Long
array, null
if null array input
*/
public static Long[] toObject(long[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_LONG_OBJECT_ARRAY;
}
final Long[] result = new Long[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Int array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Integers to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Integer
array, may be null
* @return an int
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static int[] toPrimitive(Integer[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_INT_ARRAY;
}
final int[] result = new int[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Integer to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Integer
array, may be null
* @param valueForNull the value to insert if null
found
* @return an int
array, null
if null array input
*/
public static int[] toPrimitive(Integer[] array, int valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_INT_ARRAY;
}
final int[] result = new int[array.length];
for (int i = 0; i < array.length; i++) {
Integer b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive ints to objects.
*
*
This method returns null
for a null
input array.
*
* @param array an int
array
* @return an Integer
array, null
if null array input
*/
public static Integer[] toObject(int[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_INTEGER_OBJECT_ARRAY;
}
final Integer[] result = new Integer[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Short array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Shorts to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Short
array, may be null
* @return a byte
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static short[] toPrimitive(Short[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_SHORT_ARRAY;
}
final short[] result = new short[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Short to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Short
array, may be null
* @param valueForNull the value to insert if null
found
* @return a byte
array, null
if null array input
*/
public static short[] toPrimitive(Short[] array, short valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_SHORT_ARRAY;
}
final short[] result = new short[array.length];
for (int i = 0; i < array.length; i++) {
Short b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive shorts to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a short
array
* @return a Short
array, null
if null array input
*/
public static Short[] toObject(short[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_SHORT_OBJECT_ARRAY;
}
final Short[] result = new Short[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Byte array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Bytes to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Byte
array, may be null
* @return a byte
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static byte[] toPrimitive(Byte[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BYTE_ARRAY;
}
final byte[] result = new byte[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Bytes to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Byte
array, may be null
* @param valueForNull the value to insert if null
found
* @return a byte
array, null
if null array input
*/
public static byte[] toPrimitive(Byte[] array, byte valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BYTE_ARRAY;
}
final byte[] result = new byte[array.length];
for (int i = 0; i < array.length; i++) {
Byte b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive bytes to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a byte
array
* @return a Byte
array, null
if null array input
*/
public static Byte[] toObject(byte[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BYTE_OBJECT_ARRAY;
}
final Byte[] result = new Byte[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Double array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Doubles to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Double
array, may be null
* @return a double
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static double[] toPrimitive(Double[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
final double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Doubles to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Double
array, may be null
* @param valueForNull the value to insert if null
found
* @return a double
array, null
if null array input
*/
public static double[] toPrimitive(Double[] array, double valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
final double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
Double b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive doubles to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a double
array
* @return a Double
array, null
if null array input
*/
public static Double[] toObject(double[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_DOUBLE_OBJECT_ARRAY;
}
final Double[] result = new Double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Float array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Floats to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Float
array, may be null
* @return a float
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static float[] toPrimitive(Float[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_FLOAT_ARRAY;
}
final float[] result = new float[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Floats to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Float
array, may be null
* @param valueForNull the value to insert if null
found
* @return a float
array, null
if null array input
*/
public static float[] toPrimitive(Float[] array, float valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_FLOAT_ARRAY;
}
final float[] result = new float[array.length];
for (int i = 0; i < array.length; i++) {
Float b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive floats to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a float
array
* @return a Float
array, null
if null array input
*/
public static Float[] toObject(float[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_FLOAT_OBJECT_ARRAY;
}
final Float[] result = new Float[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
// Boolean array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Booleans to primitives.
*
*
This method returns null
for a null
input array.
*
* @param array a Boolean
array, may be null
* @return a boolean
array, null
if null array input
* @throws NullPointerException if array content is null
*/
public static boolean[] toPrimitive(Boolean[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BOOLEAN_ARRAY;
}
final boolean[] result = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i];
}
return result;
}
/**
* Converts an array of object Booleans to primitives handling null
.
*
*
This method returns null
for a null
input array.
*
* @param array a Boolean
array, may be null
* @param valueForNull the value to insert if null
found
* @return a boolean
array, null
if null array input
*/
public static boolean[] toPrimitive(Boolean[] array, boolean valueForNull) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BOOLEAN_ARRAY;
}
final boolean[] result = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
Boolean b = array[i];
result[i] = (b == null ? valueForNull : b);
}
return result;
}
/**
* Converts an array of primitive booleans to objects.
*
*
This method returns null
for a null
input array.
*
* @param array a boolean
array
* @return a Boolean
array, null
if null array input
*/
public static Boolean[] toObject(boolean[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_BOOLEAN_OBJECT_ARRAY;
}
final Boolean[] result = new Boolean[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
}
return result;
}
}