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

com.github.yt.commons.util.YtArrayUtils Maven / Gradle / Ivy

package com.github.yt.commons.util;

import java.lang.reflect.Array;

/**
 * copy from org.apache.commons
 * @author sheng
 */
public class YtArrayUtils {


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


    /**
     * 

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

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. * *

     * YtArrayUtils.getLength(null)            = 0
     * YtArrayUtils.getLength([])              = 0
     * YtArrayUtils.getLength([null])          = 1
     * YtArrayUtils.getLength([true, false])   = 2
     * YtArrayUtils.getLength([1, 2, 3])       = 3
     * YtArrayUtils.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 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; } /** *

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





© 2015 - 2025 Weber Informatics LLC | Privacy Policy