From 500d72d596381816c85cc35a552963eb86d339d8 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 8 May 2022 17:42:26 +0300 Subject: [PATCH] classlib: add methods to java.util.Objects --- .../teavm/classlib/java/util/TObjects.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java b/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java index fe405d927..f46935ab1 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java @@ -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 requireNonNull(T obj, Supplier messageSupplier) { + if (obj == null) { + throw new NullPointerException(messageSupplier.get()); + } + return obj; + } + + public static T requireNonNullElse(T obj, T defaultObj) { + return obj != null ? obj : requireNonNull(defaultObj); + } + + public static T requireNonNullElseGet(T obj, Supplier 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; + } }