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

org.rx.core.Arrays Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import io.netty.util.internal.ThreadLocalRandom;
import lombok.NonNull;
import org.apache.commons.collections4.EnumerationUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.rx.exception.InvalidException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 * System.arraycopy();
 * Arrays.copyOf();
 */
public class Arrays extends ArrayUtils {
    public static  List toList(Enumeration enumeration) {
        return EnumerationUtils.toList(enumeration);
    }

    public static  List toList(@NonNull T one) {
        List list = new ArrayList<>(1);
        list.add(one);
        return list;
    }

    @SafeVarargs
    public static  List toList(T... items) {
        if (items == null) {
            return new ArrayList<>();
        }

        List list = new ArrayList<>(items.length);
        Collections.addAll(list, items);
        return list;
    }

    public static int randomNext(int[] array) {
        if (isEmpty(array)) {
            throw new InvalidException("Empty array");
        }

        return array[ThreadLocalRandom.current().nextInt(0, array.length)];
    }

    public static  T randomNext(T[] array) {
        if (isEmpty(array)) {
            throw new InvalidException("Empty array");
        }

        return array[ThreadLocalRandom.current().nextInt(0, array.length)];
    }

    public static boolean equals(byte[] a, byte[] b) {
        return java.util.Arrays.equals(a, b);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy