org.rx.core.Arrays Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
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);
}
}