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

com.sap.it.commons.tools.ArrayTools Maven / Gradle / Ivy

package com.sap.it.commons.tools;

import com.sap.it.commons.lang.Equals;

public class ArrayTools {
    private ArrayTools() {
    }

    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

    //
    // Null save length methods
    //
    public static int length(Object[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(byte[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(short[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(int[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(long[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(char[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(float[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(double[] array) {
        return array != null ? array.length : 0;
    }

    public static int length(boolean[] array) {
        return array != null ? array.length : 0;
    }

    //
    // Null save isEmpty methods
    //
    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(byte[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(short[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(int[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(long[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(char[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(float[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(double[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(boolean[] array) {
        return array == null || array.length == 0;
    }

    //
    // Null save clone methods
    //
    public static  T[] clone(T[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static byte[] clone(byte[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static short[] clone(short[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static int[] clone(int[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static long[] clone(long[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static char[] clone(char[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static float[] clone(float[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static double[] clone(double[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    public static boolean[] clone(boolean[] original) {
        return isEmpty(original) ? original : original.clone();
    }

    //
    public static  T firstOf(T[] data) {
        return isEmpty(data) ? null : data[0];
    }

    //
    public static String asString(Object[] array, String separator, String collectionEmptyValue) {
        if (isEmpty(array)) {
            return collectionEmptyValue;
        }

        final StringBuilder sb = new StringBuilder();
        for (Object o : array) {
            if (sb.length() > 0) {
                sb.append(separator);
            }
            sb.append(o);
        }
        return sb.toString();
    }

    public static String asString(Object[] array, String separator) {
        return asString(array, separator, StringTools.EMPTY_STRING);
    }

    // T11
    public static  int indexOf(T[] array, T value) {
        if (array == null) {
            return -1;
        }

        for (int idx = 0; idx < array.length; ++idx) {
            if (Equals.equals(value, array[idx])) {
                return idx;
            }
        }
        return -1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy