All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ehcache.impl.internal.classes.commonslang.ArrayUtils Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * 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]; /** *

ArrayUtils instances should NOT be constructed in standard programming. * Instead, the class should be used as ArrayUtils.clone(new int[] {2}). * *

This constructor is public to permit tools that require a JavaBean instance * to operate. */ public ArrayUtils() { super(); } // 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); } /** *

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; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy