2021-07-24 02:37:17 +00:00
|
|
|
package androidx.core.util;
|
|
|
|
|
2021-09-15 06:33:54 +00:00
|
|
|
import android.text.TextUtils;
|
2021-07-24 02:37:17 +00:00
|
|
|
import androidx.annotation.IntRange;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.RestrictTo;
|
2021-09-15 06:33:54 +00:00
|
|
|
import c.d.b.a.a;
|
2021-07-24 02:37:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-15 06:33:54 +00:00
|
|
|
public static int checkFlagsArgument(int i, int i2) {
|
|
|
|
if ((i & i2) == i) {
|
|
|
|
return i;
|
|
|
|
}
|
2021-11-05 06:48:17 +00:00
|
|
|
StringBuilder R = a.R("Requested flags 0x");
|
|
|
|
R.append(Integer.toHexString(i));
|
|
|
|
R.append(", but only 0x");
|
|
|
|
R.append(Integer.toHexString(i2));
|
|
|
|
R.append(" are allowed");
|
|
|
|
throw new IllegalArgumentException(R.toString());
|
2021-09-15 06:33:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 02:37:17 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 06:33:54 +00:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static <T extends CharSequence> T checkStringNotEmpty(@Nullable T t) {
|
|
|
|
if (!TextUtils.isEmpty(t)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static <T extends CharSequence> T checkStringNotEmpty(@Nullable T t, @NonNull Object obj) {
|
|
|
|
if (!TextUtils.isEmpty(t)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException(String.valueOf(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static <T extends CharSequence> T checkStringNotEmpty(@Nullable T t, @NonNull String str, @NonNull Object... objArr) {
|
|
|
|
if (!TextUtils.isEmpty(t)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException(String.format(str, objArr));
|
|
|
|
}
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|