Added isInRange for Arrays and Lists.
Fixed getOrDefault
@@ -8,10 +8,7 @@ import net.minecraft.entity.Entity;
import net.minecraft.network.Packet;
import net.minecraft.util.math.Vec3d;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -96,12 +93,27 @@ public class Utils implements Globals {
return to_min + (to_max - to_min) * ((x - from_min) / (from_max - from_min));
}
+ public static <T> boolean isInRange(T[] array, int index) {
+ return array != null && index >= 0 && index < array.length;
+ }
+
+ public static <T> boolean isInRange(List<T> list, int index) {
+ return list != null && index >= 0 && index < list.size();
+ }
+
public static <T> T defaultTo(T value, T defaultTo) {
return value == null ? defaultTo : value;
}
- public static <T> T getOrDefault(Collection<T> collection, int index, T defaultTo) {
- return collection.stream().skip(index).findFirst().orElse(defaultTo);
+ public static <T> T getOrDefault(Iterable<T> iterable, int index, T defaultTo) {
+ Iterator<T> iterator = iterable.iterator();
+ T next;
+ int count = 0;
+ while(iterator.hasNext()) {
+ next = iterator.next();
+ if(count++ == index) return next;
+ }
+ return defaultTo;
}
@Deprecated
