
org.rx.core.Arrays Maven / Gradle / Ivy
package org.rx.core;
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;
import io.netty.util.internal.ThreadLocalRandom;
/**
* 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 - 2025 Weber Informatics LLC | Privacy Policy