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

org.apache.logging.log4j.core.util.ArrayUtils Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta2
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.
 */

package org.apache.logging.log4j.core.util;

import java.lang.reflect.Array;

/**
 * Copied from Apache Commons Lang (including the {@code @since} tags.)
 */
public class ArrayUtils {

    /**
     * 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 byte[] array) {
        return getLength(array) == 0;
    }
    
    /**
     * 

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); } /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (subtracts one from * their indices).

* *

This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.

* *

If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.

* * @param array the array to remove the element from, may not be {@code null} * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 2.1 */ private static Object remove(final Object array, final int index) { final int length = getLength(array); if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } final Object result = Array.newInstance(array.getClass().getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if (index < length - 1) { System.arraycopy(array, index + 1, result, index, length - index - 1); } return result; } /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (subtracts one from * their indices).

* *

This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.

* *

If the input array is {@code null}, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.

* *
     * ArrayUtils.remove(["a"], 0)           = []
     * ArrayUtils.remove(["a", "b"], 0)      = ["b"]
     * ArrayUtils.remove(["a", "b"], 1)      = ["a"]
     * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
     * 
* * @param the component type of the array * @param array the array to remove the element from, may not be {@code null} * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is {@code null}. * @since 2.1 */ @SuppressWarnings("unchecked") // remove() always creates an array of the same type as its input public static T[] remove(final T[] array, final int index) { return (T[]) remove((Object) array, index); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy