org.ehcache.impl.internal.classes.commonslang.ArrayUtils Maven / Gradle / Ivy
Show all versions of ehcache-impl Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This is a modified version of the original Apache class. It has had unused
* members removed.
*/
package org.ehcache.impl.internal.classes.commonslang;
import java.lang.reflect.Array;
/**
* Operations on arrays, primitive arrays (like {@code int[]}) and
* primitive wrapper arrays (like {@code Integer[]}).
*
*
This class tries to handle {@code null} input gracefully.
* An exception will not be thrown for a {@code null}
* array input. However, an Object array that contains a {@code null}
* element may throw an exception. Each method documents its behaviour.
*
*
#ThreadSafe#
* @since 2.0
*/
public class ArrayUtils {
/**
* An empty immutable {@code Object} array.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
/**
* An empty immutable {@code Class} array.
*/
public static final Class>[] EMPTY_CLASS_ARRAY = new Class>[0];
/**
* An empty immutable {@code long} array.
*/
public static final long[] EMPTY_LONG_ARRAY = new long[0];
/**
* An empty immutable {@code int} array.
*/
public static final int[] EMPTY_INT_ARRAY = new int[0];
/**
* An empty immutable {@code short} array.
*/
public static final short[] EMPTY_SHORT_ARRAY = new short[0];
/**
* An empty immutable {@code double} array.
*/
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
/**
* An empty immutable {@code float} array.
*/
public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
// NOTE: Cannot use {@code} to enclose text which includes {}, but
is OK
// nullToEmpty
//-----------------------------------------------------------------------
/**
*
Defensive programming technique to change a {@code null}
* reference to an empty one.
*
*
This method returns an empty array for a {@code null} input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty {@code public static} references in this class.
*
* @param array the array to check for {@code null} or empty
* @return the same array, {@code public static} empty array if {@code null} or empty input
* @since 2.5
*/
public static Object[] nullToEmpty(final Object[] array) {
if (isEmpty(array)) {
return EMPTY_OBJECT_ARRAY;
}
return array;
}
/**
*
Defensive programming technique to change a {@code null}
* reference to an empty one.
*
*
This method returns an empty array for a {@code null} input array.
*
*
As a memory optimizing technique an empty array passed in will be overridden with
* the empty {@code public static} references in this class.
*
* @param array the array to check for {@code null} or empty
* @return the same array, {@code public static} empty array if {@code null} or empty input
* @since 3.2
*/
public static Class>[] nullToEmpty(final Class>[] array) {
if (isEmpty(array)) {
return EMPTY_CLASS_ARRAY;
}
return array;
}
// Is same length
//-----------------------------------------------------------------------
/**
*
Checks whether two arrays are the same length, treating
* {@code null} arrays as length {@code 0}.
*
*
Any multi-dimensional aspects of the arrays are ignored.
*
* @param array1 the first array, may be {@code null}
* @param array2 the second array, may be {@code null}
* @return {@code true} if length of arrays matches, treating
* {@code null} as an empty array
*/
public static boolean isSameLength(final Object[] array1, final Object[] array2) {
return getLength(array1) == getLength(array2);
}
//-----------------------------------------------------------------------
/**
*
Returns the length of the specified array.
* This method can deal with {@code Object} arrays and with primitive arrays.
*
*
If the input array is {@code null}, {@code 0} is returned.
*
*
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
*
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or {@code 0} if the array is {@code null}
* @throws IllegalArgumentException if the object argument is not an array.
* @since 2.1
*/
public static int getLength(final Object array) {
if (array == null) {
return 0;
}
return Array.getLength(array);
}
// Long array converters
// ----------------------------------------------------------------------
/**
* Converts an array of object Longs to primitives.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array a {@code Long} array, may be {@code null}
* @return a {@code long} array, {@code null} if null array input
* @throws NullPointerException if array content is {@code null}
*/
public static long[] toPrimitive(final 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].longValue();
}
return result;
}
// Int array converters
// ----------------------------------------------------------------------
/**
*
Converts an array of object Integers to primitives.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array a {@code Integer} array, may be {@code null}
* @return an {@code int} array, {@code null} if null array input
* @throws NullPointerException if array content is {@code null}
*/
public static int[] toPrimitive(final 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].intValue();
}
return result;
}
// Short array converters
// ----------------------------------------------------------------------
/**
*
Converts an array of object Shorts to primitives.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array a {@code Short} array, may be {@code null}
* @return a {@code byte} array, {@code null} if null array input
* @throws NullPointerException if array content is {@code null}
*/
public static short[] toPrimitive(final 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].shortValue();
}
return result;
}
// Byte array converters
// ----------------------------------------------------------------------
// Double array converters
// ----------------------------------------------------------------------
/**
*
Converts an array of object Doubles to primitives.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array a {@code Double} array, may be {@code null}
* @return a {@code double} array, {@code null} if null array input
* @throws NullPointerException if array content is {@code null}
*/
public static double[] toPrimitive(final 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].doubleValue();
}
return result;
}
// Float array converters
// ----------------------------------------------------------------------
/**
*
Converts an array of object Floats to primitives.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array a {@code Float} array, may be {@code null}
* @return a {@code float} array, {@code null} if null array input
* @throws NullPointerException if array content is {@code null}
*/
public static float[] toPrimitive(final 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].floatValue();
}
return result;
}
/**
*
Create an array of primitive type from an array of wrapper types.
*
*
This method returns {@code null} for a {@code null} input array.
*
* @param array an array of wrapper object
* @return an array of the corresponding primitive type, or the original array
* @since 3.5
*/
public static Object toPrimitive(final Object array) {
if (array == null) {
return null;
}
final Class> ct = array.getClass().getComponentType();
final Class> pt = ClassUtils.wrapperToPrimitive(ct);
if(Integer.TYPE.equals(pt)) {
return toPrimitive((Integer[]) array);
}
if(Long.TYPE.equals(pt)) {
return toPrimitive((Long[]) array);
}
if(Short.TYPE.equals(pt)) {
return toPrimitive((Short[]) array);
}
if(Double.TYPE.equals(pt)) {
return toPrimitive((Double[]) array);
}
if(Float.TYPE.equals(pt)) {
return toPrimitive((Float[]) array);
}
return array;
}
// ----------------------------------------------------------------------
/**
*
Checks if an array of Objects is empty or {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is empty or {@code null}
* @since 2.1
*/
public static boolean isEmpty(final Object[] array) {
return getLength(array) == 0;
}
}