
com.flowyun.cornerstone.web.util.ArrayUtils Maven / Gradle / Ivy
package com.flowyun.cornerstone.web.util;
import java.lang.reflect.Array;
public class ArrayUtils {
public static int getLength(final Object array) {
if (array == null) {
return 0;
}
return Array.getLength(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;
}
/**
*
Checks if an array of primitive longs 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 long[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive ints 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 int[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive shorts 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 short[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive chars 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 char[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive bytes 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 byte[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive doubles 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 double[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive floats 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 float[] array) {
return getLength(array) == 0;
}
/**
*
Checks if an array of primitive booleans 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 boolean[] array) {
return getLength(array) == 0;
}
// ----------------------------------------------------------------------
/**
*
Checks if an array of Objects is not empty and not {@code null}.
*
* @param the component type of the array
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final T[] array) {
return !isEmpty(array);
}
/**
* Checks if an array of primitive longs is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final long[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive ints is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final int[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive shorts is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final short[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive chars is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final char[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive bytes is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final byte[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive doubles is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final double[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive floats is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final float[] array) {
return !isEmpty(array);
}
/**
*
Checks if an array of primitive booleans is not empty and not {@code null}.
*
* @param array the array to test
* @return {@code true} if the array is not empty and not {@code null}
* @since 2.5
*/
public static boolean isNotEmpty(final boolean[] array) {
return !isEmpty(array);
}
public static T[] subarray(final T[] array, int startIndexInclusive, int endIndexExclusive) {
if (array == null) {
return null;
}
if (startIndexInclusive < 0) {
startIndexInclusive = 0;
}
if (endIndexExclusive > array.length) {
endIndexExclusive = array.length;
}
final int newSize = endIndexExclusive - startIndexInclusive;
final Class> type = array.getClass().getComponentType();
if (newSize <= 0) {
@SuppressWarnings("unchecked") // OK, because array is of type T
final T[] emptyArray = (T[]) Array.newInstance(type, 0);
return emptyArray;
}
@SuppressWarnings("unchecked") // OK, because array is of type T
final
T[] subarray = (T[]) Array.newInstance(type, newSize);
System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
return subarray;
}
}