/* * 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; /** * <p>Operations on arrays, primitive arrays (like {@code int[]}) and * primitive wrapper arrays (like {@code Integer[]}). * * <p>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. * * <p>#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]; /** * <p>ArrayUtils instances should NOT be constructed in standard programming. * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>. * * <p>This constructor is public to permit tools that require a JavaBean instance * to operate. */ public ArrayUtils() { super(); } // nullToEmpty //----------------------------------------------------------------------- /** * <p>Defensive programming technique to change a {@code null} * reference to an empty one. * * <p>This method returns an empty array for a {@code null} input array.</li> * * <p>As a memory optimizing technique an empty array passed in will be overridden with * the empty {@code public static} references in this class.</li> * * @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; } /** * <p>Defensive programming technique to change a {@code null} * reference to an empty one.</li> * * <p>This method returns an empty array for a {@code null} input array.</li> * * <p>As a memory optimizing technique an empty array passed in will be overridden with * the empty {@code public static} references in this class.</li> * * @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 //----------------------------------------------------------------------- /** * <p>Checks whether two arrays are the same length, treating * {@code null} arrays as length {@code 0}. * * <p>Any multi-dimensional aspects of the arrays are ignored.</li> * * @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); } /** * <p>Returns the length of the specified array. * This method can deal with {@code Object} arrays and with primitive arrays.</li> * * <p>If the input array is {@code null}, {@code 0} is returned.</li> * * <pre> * 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 * </pre> * * @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); } /** * <p>Checks if an array of Objects is empty or {@code null}.</li> * * @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; } }