76 lines
2.1 KiB
Java
76 lines
2.1 KiB
Java
|
package androidx.core.util;
|
||
|
|
||
|
import androidx.annotation.IntRange;
|
||
|
import androidx.annotation.NonNull;
|
||
|
import androidx.annotation.Nullable;
|
||
|
import androidx.annotation.RestrictTo;
|
||
|
import java.util.Locale;
|
||
|
import java.util.Objects;
|
||
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
||
|
public final class Preconditions {
|
||
|
private Preconditions() {
|
||
|
}
|
||
|
|
||
|
public static void checkArgument(boolean z2) {
|
||
|
if (!z2) {
|
||
|
throw new IllegalArgumentException();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void checkArgument(boolean z2, @NonNull Object obj) {
|
||
|
if (!z2) {
|
||
|
throw new IllegalArgumentException(String.valueOf(obj));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int checkArgumentInRange(int i, int i2, int i3, @NonNull String str) {
|
||
|
if (i < i2) {
|
||
|
throw new IllegalArgumentException(String.format(Locale.US, "%s is out of range of [%d, %d] (too low)", str, Integer.valueOf(i2), Integer.valueOf(i3)));
|
||
|
} else if (i <= i3) {
|
||
|
return i;
|
||
|
} else {
|
||
|
throw new IllegalArgumentException(String.format(Locale.US, "%s is out of range of [%d, %d] (too high)", str, Integer.valueOf(i2), Integer.valueOf(i3)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@IntRange(from = 0)
|
||
|
public static int checkArgumentNonnegative(int i) {
|
||
|
if (i >= 0) {
|
||
|
return i;
|
||
|
}
|
||
|
throw new IllegalArgumentException();
|
||
|
}
|
||
|
|
||
|
@IntRange(from = 0)
|
||
|
public static int checkArgumentNonnegative(int i, @Nullable String str) {
|
||
|
if (i >= 0) {
|
||
|
return i;
|
||
|
}
|
||
|
throw new IllegalArgumentException(str);
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
public static <T> T checkNotNull(@Nullable T t) {
|
||
|
Objects.requireNonNull(t);
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
public static <T> T checkNotNull(@Nullable T t, @NonNull Object obj) {
|
||
|
if (t != null) {
|
||
|
return t;
|
||
|
}
|
||
|
throw new NullPointerException(String.valueOf(obj));
|
||
|
}
|
||
|
|
||
|
public static void checkState(boolean z2) {
|
||
|
checkState(z2, null);
|
||
|
}
|
||
|
|
||
|
public static void checkState(boolean z2, @Nullable String str) {
|
||
|
if (!z2) {
|
||
|
throw new IllegalStateException(str);
|
||
|
}
|
||
|
}
|
||
|
}
|