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

com.azure.cosmos.implementation.apachecommons.lang.ArrayUtils Maven / Gradle / Ivy

Go to download

This Package contains Microsoft Azure Cosmos SDK (with Reactive Extension Reactor support) for Azure Cosmos DB SQL API

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

/*
 * Portions Copyright (c) Microsoft Corporation
 */

package com.azure.cosmos.implementation.apachecommons.lang;

import java.lang.reflect.Array;

public class ArrayUtils {
    /**
     * An empty immutable {@code String} array.
     */
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * The index value when an element is not found in a list or array: {@code -1}.
     * This value is returned by methods in this class and can also be used in comparisons with values returned by
     * various method from {@link java.util.List}.
     */
    public static final int INDEX_NOT_FOUND = -1;

    private ArrayUtils() {
        super();
    }

    /**
     * 

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. * * @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. */ public static int getLength(final Object array) { if (array == null) { return 0; } return Array.getLength(array); } /** *

Finds the index of the given object in the array. * *

This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param objectToFind the object to find, may be {@code null} * @return the index of the object within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final Object[] array, final Object objectToFind) { return indexOf(array, objectToFind, 0); } /** *

Finds the index of the given object in the array starting at the given index. * *

This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). * * @param array the array to search through for the object, may be {@code null} * @param objectToFind the object to find, may be {@code null} * @param startIndex the index to start searching at * @return the index of the object within the array starting at the index, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) { if (array == null) { return INDEX_NOT_FOUND; } if (startIndex < 0) { startIndex = 0; } if (objectToFind == null) { for (int i = startIndex; i < array.length; i++) { if (array[i] == null) { return i; } } } else { for (int i = startIndex; i < array.length; i++) { if (objectToFind.equals(array[i])) { return i; } } } return INDEX_NOT_FOUND; } /** *

Checks if the object is in the given array. * *

The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param objectToFind the object to find * @return {@code true} if the array contains the object */ public static boolean contains(final Object[] array, final Object objectToFind) { return indexOf(array, objectToFind) != INDEX_NOT_FOUND; } // ---------------------------------------------------------------------- /** *

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} */ public static boolean isEmpty(final Object[] 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} */ public static boolean isEmpty(final char[] array) { return getLength(array) == 0; } /** *

Copies the given array and adds the given element at the end of the new array. * * @param the component type of the array * @param array the array to "add" the element to, may be {@code null} * @param element the object to add, may be {@code null} * @return A new array containing the existing elements plus the new element * @throws IllegalArgumentException if both arguments are null */ public static T[] add(final T[] array, final T element) { Class type; if (array != null) { type = array.getClass().getComponentType(); } else if (element != null) { type = element.getClass(); } else { throw new IllegalArgumentException("Arguments cannot both be null"); } @SuppressWarnings("unchecked") // type must be T final T[] newArray = (T[]) copyArrayGrow1(array, type); newArray[newArray.length - 1] = element; return newArray; } /** * Returns a copy of the given array of size 1 greater than the argument. * The last value of the array is left to the default value. * * @param array The array to copy, must not be {@code null}. * @param newArrayComponentType If {@code array} is {@code null}, create a * size 1 array of this type. * @return A new copy of the array of size 1 greater than the input. */ private static Object copyArrayGrow1(final Object array, final Class newArrayComponentType) { if (array != null) { final int arrayLength = Array.getLength(array); final Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1); System.arraycopy(array, 0, newArray, 0, arrayLength); return newArray; } return Array.newInstance(newArrayComponentType, 1); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy