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

net.dongliu.commons.collection.Arrays2 Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package net.dongliu.commons.collection;

import java.io.File;
import java.lang.reflect.Array;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Utils for Array.
 *
 * @author Liu Dong
 */
public final class Arrays2 {

    public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
    public static final int[] EMPTY_INT_ARRAY = {};
    public static final long[] EMPTY_LONG_ARRAY = {};
    public static final byte[] EMPTY_BYTE_ARRAY = {};
    public static final char[] EMPTY_CHAR_ARRAY = {};
    public static final short[] EMPTY_SHORT_ARRAY = {};
    public static final float[] EMPTY_FLOAT_ARRAY = {};
    public static final double[] EMPTY_DOUBLE_ARRAY = {};
    public static final Object[] EMPTY_OBJECT_ARRAY = {};
    public static final String[] EMPTY_STRING_ARRAY = {};
    public static final File[] EMPTY_FILE_ARRAY = {};
    public static final Class[] EMPTY_CLASS_ARRAY = {};

    /**
     * For each for array
     */
    public static  void forEach(T[] array, Consumer consumer) {
        for (T anArray : array) {
            consumer.accept(anArray);
        }
    }

    /**
     * Create new array with type
     */
    public static  T[] newArray(int size, Class cls) {
        @SuppressWarnings("unchecked")
        T[] array = (T[]) Array.newInstance(cls, size);
        return array;
    }

    /**
     * class for r type
     *
     * @param array array to be convert. not null
     * @param cls   class for r type
     */
    public static  R[] convert(T[] array, Class cls, Function converter) {
        R[] newArray = newArray(array.length, cls);
        for (int i = 0; i < array.length; i++) {
            newArray[i] = converter.apply(array[i]);
        }
        return newArray;
    }

    /**
     * Merge two arrays
     */
    public static  T[] merge(T[] a1, T[] a2) {
        Objects.requireNonNull(a1);
        Objects.requireNonNull(a2);
        T[] array = newArray(a1.length + a2.length, a1.getClass().getComponentType());
        System.arraycopy(a1, 0, array, 0, a1.length);
        System.arraycopy(a2, 0, array, a1.length, a2.length);
        return array;
    }

    public static  T[] merge(T value, T[] a) {
        Objects.requireNonNull(a);
        T[] array = newArray(a.length + 1, a.getClass().getComponentType());
        array[0] = value;
        System.arraycopy(a, 0, array, 1, a.length);
        return array;
    }

    public static  T[] merge(T[] a, T value) {
        Objects.requireNonNull(a);
        @SuppressWarnings("unchecked")
        T[] array = newArray(a.length + 1, a.getClass().getComponentType());
        System.arraycopy(a, 0, array, 0, a.length);
        array[a.length] = value;
        return array;
    }

    public static boolean[] merge(boolean[] a, boolean[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        boolean[] c = new boolean[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static char[] merge(char[] a, char[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        char[] c = new char[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static byte[] merge(byte[] a, byte[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static short[] merge(short[] a, short[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        short[] c = new short[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static int[] merge(int[] a, int[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        int[] c = new int[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static long[] merge(long[] a, long[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        long[] c = new long[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static float[] merge(float[] a, float[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        float[] c = new float[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static double[] merge(double[] a, double[] b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);
        double[] c = new double[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy