mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: add methods to java.util.Objects
This commit is contained in:
parent
a9035a25f0
commit
500d72d596
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package org.teavm.classlib.java.util;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import org.teavm.classlib.java.lang.TObject;
|
||||
|
||||
public final class TObjects extends TObject {
|
||||
|
@ -52,6 +53,25 @@ public final class TObjects extends TObject {
|
|||
return obj;
|
||||
}
|
||||
|
||||
public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
|
||||
if (obj == null) {
|
||||
throw new NullPointerException(messageSupplier.get());
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static <T> T requireNonNullElse(T obj, T defaultObj) {
|
||||
return obj != null ? obj : requireNonNull(defaultObj);
|
||||
}
|
||||
|
||||
public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {
|
||||
return obj != null ? obj : requireNonNull(supplier.get());
|
||||
}
|
||||
|
||||
public static boolean isNull(Object obj) {
|
||||
return obj == null;
|
||||
}
|
||||
|
||||
public static boolean nonNull(Object obj) {
|
||||
return obj != null;
|
||||
}
|
||||
|
@ -103,4 +123,25 @@ public final class TObjects extends TObject {
|
|||
public static int hash(Object... values) {
|
||||
return TArrays.hashCode(values);
|
||||
}
|
||||
|
||||
public static int checkIndex(int index, int length) {
|
||||
if (index < 0 || index >= length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public static int checkFromToIndex(int fromIndex, int toIndex, int length) {
|
||||
if (fromIndex < 0 || fromIndex > toIndex || toIndex > length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return fromIndex;
|
||||
}
|
||||
|
||||
public static int checkFromIndexSize(int fromIndex, int size, int length) {
|
||||
if (fromIndex < 0 || size < 0 || fromIndex + size > length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return fromIndex;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user