public final class

Preconditions

extends java.lang.Object

 java.lang.Object

↳androidx.core.util.Preconditions

Gradle dependencies

compile group: 'androidx.core', name: 'core', version: '1.9.0-alpha04'

  • groupId: androidx.core
  • artifactId: core
  • version: 1.9.0-alpha04

Artifact androidx.core:core:1.9.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.core:core com.android.support:support-compat

Androidx class mapping:

androidx.core.util.Preconditions android.support.v4.util.Preconditions

Overview

Simple static methods to be called at the start of your own methods to verify correct arguments and state.

Summary

Methods
public static voidcheckArgument(boolean expression)

public static voidcheckArgument(boolean expression, java.lang.Object errorMessage)

Ensures that an expression checking an argument is true.

public static voidcheckArgument(boolean expression, java.lang.String messageTemplate, java.lang.Object messageArgs[])

Ensures that an expression checking an argument is true.

public static floatcheckArgumentFinite(float value, java.lang.String valueName)

Ensures that the argument floating point value is a finite number.

public static doublecheckArgumentInRange(double value, double lower, double upper, java.lang.String valueName)

Ensures that the argument double value is within the inclusive range.

public static floatcheckArgumentInRange(float value, float lower, float upper, java.lang.String valueName)

Ensures that the argument float value is within the inclusive range.

public static intcheckArgumentInRange(int value, int lower, int upper, java.lang.String valueName)

Ensures that the argument int value is within the inclusive range.

public static longcheckArgumentInRange(long value, long lower, long upper, java.lang.String valueName)

Ensures that the argument long value is within the inclusive range.

public static intcheckArgumentNonnegative(int value)

Ensures that that the argument numeric value is non-negative.

public static intcheckArgumentNonnegative(int value, java.lang.String errorMessage)

Ensures that that the argument numeric value is non-negative.

public static intcheckFlagsArgument(int requestedFlags, int allowedFlags)

Check the requested flags, throwing if any requested flags are outside the allowed set.

public static java.lang.ObjectcheckNotNull(java.lang.Object reference)

Ensures that an object reference passed as a parameter to the calling method is not null.

public static java.lang.ObjectcheckNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Ensures that an object reference passed as a parameter to the calling method is not null.

public static voidcheckState(boolean expression)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

public static voidcheckState(boolean expression, java.lang.String message)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

public static java.lang.CharSequencecheckStringNotEmpty(java.lang.CharSequence string)

Ensures that an string reference passed as a parameter to the calling method is not empty.

public static java.lang.CharSequencecheckStringNotEmpty(java.lang.CharSequence string, java.lang.Object errorMessage)

Ensures that an string reference passed as a parameter to the calling method is not empty.

public static java.lang.CharSequencecheckStringNotEmpty(java.lang.CharSequence string, java.lang.String messageTemplate, java.lang.Object messageArgs[])

Ensures that an string reference passed as a parameter to the calling method is not empty.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static void checkArgument(boolean expression)

public static void checkArgument(boolean expression, java.lang.Object errorMessage)

Ensures that an expression checking an argument is true.

Parameters:

expression: the expression to check
errorMessage: the exception message to use if the check fails; will be converted to a string using valueOf

public static void checkArgument(boolean expression, java.lang.String messageTemplate, java.lang.Object messageArgs[])

Ensures that an expression checking an argument is true.

Parameters:

expression: the expression to check
messageTemplate: a printf-style message template to use if the check fails; will be converted to a string using format
messageArgs: arguments for messageTemplate

public static java.lang.CharSequence checkStringNotEmpty(java.lang.CharSequence string)

Ensures that an string reference passed as a parameter to the calling method is not empty.

Parameters:

string: an string reference

Returns:

the string reference that was validated

public static java.lang.CharSequence checkStringNotEmpty(java.lang.CharSequence string, java.lang.Object errorMessage)

Ensures that an string reference passed as a parameter to the calling method is not empty.

Parameters:

string: an string reference
errorMessage: the exception message to use if the check fails; will be converted to a string using valueOf

Returns:

the string reference that was validated

public static java.lang.CharSequence checkStringNotEmpty(java.lang.CharSequence string, java.lang.String messageTemplate, java.lang.Object messageArgs[])

Ensures that an string reference passed as a parameter to the calling method is not empty.

Parameters:

string: an string reference
messageTemplate: a printf-style message template to use if the check fails; will be converted to a string using format
messageArgs: arguments for messageTemplate

Returns:

the string reference that was validated

public static java.lang.Object checkNotNull(java.lang.Object reference)

Ensures that an object reference passed as a parameter to the calling method is not null.

Parameters:

reference: an object reference

Returns:

the non-null reference that was validated

public static java.lang.Object checkNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Ensures that an object reference passed as a parameter to the calling method is not null.

Parameters:

reference: an object reference
errorMessage: the exception message to use if the check fails; will be converted to a string using valueOf

Returns:

the non-null reference that was validated

public static void checkState(boolean expression, java.lang.String message)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Parameters:

expression: a boolean expression
message: exception message

public static void checkState(boolean expression)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Parameters:

expression: a boolean expression

public static int checkFlagsArgument(int requestedFlags, int allowedFlags)

Check the requested flags, throwing if any requested flags are outside the allowed set.

Returns:

the validated requested flags.

public static int checkArgumentNonnegative(int value, java.lang.String errorMessage)

Ensures that that the argument numeric value is non-negative.

Parameters:

value: a numeric int value
errorMessage: the exception message to use if the check fails

Returns:

the validated numeric value

public static int checkArgumentNonnegative(int value)

Ensures that that the argument numeric value is non-negative.

Parameters:

value: a numeric int value

Returns:

the validated numeric value

public static int checkArgumentInRange(int value, int lower, int upper, java.lang.String valueName)

Ensures that the argument int value is within the inclusive range.

Parameters:

value: a int value
lower: the lower endpoint of the inclusive range
upper: the upper endpoint of the inclusive range
valueName: the name of the argument to use if the check fails

Returns:

the validated int value

public static long checkArgumentInRange(long value, long lower, long upper, java.lang.String valueName)

Ensures that the argument long value is within the inclusive range.

Parameters:

value: a long value
lower: the lower endpoint of the inclusive range
upper: the upper endpoint of the inclusive range
valueName: the name of the argument to use if the check fails

Returns:

the validated long value

public static float checkArgumentInRange(float value, float lower, float upper, java.lang.String valueName)

Ensures that the argument float value is within the inclusive range.

Parameters:

value: a float value
lower: the lower endpoint of the inclusive range
upper: the upper endpoint of the inclusive range
valueName: the name of the argument to use if the check fails

Returns:

the validated float value

public static double checkArgumentInRange(double value, double lower, double upper, java.lang.String valueName)

Ensures that the argument double value is within the inclusive range.

Parameters:

value: a double value
lower: the lower endpoint of the inclusive range
upper: the upper endpoint of the inclusive range
valueName: the name of the argument to use if the check fails

Returns:

the validated double value

public static float checkArgumentFinite(float value, java.lang.String valueName)

Ensures that the argument floating point value is a finite number.

A finite number is defined to be both representable (that is, not NaN) and not infinite (that is neither positive or negative infinity).

Parameters:

value: a floating point value
valueName: the name of the argument to use if the check fails

Returns:

the validated floating point value

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.core.util;

import android.text.TextUtils;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

import java.util.Locale;

/**
 * Simple static methods to be called at the start of your own methods to verify
 * correct arguments and state.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public final class Preconditions {
    public static void checkArgument(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    /**
     * Ensures that an expression checking an argument is true.
     *
     * @param expression the expression to check
     * @param errorMessage the exception message to use if the check fails; will
     *     be converted to a string using {@link String#valueOf(Object)}
     * @throws IllegalArgumentException if {@code expression} is false
     */
    public static void checkArgument(boolean expression, @NonNull Object errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    /**
     * Ensures that an expression checking an argument is true.
     *
     * @param expression the expression to check
     * @param messageTemplate a printf-style message template to use if the check fails; will
     *     be converted to a string using {@link String#format(String, Object...)}
     * @param messageArgs arguments for {@code messageTemplate}
     * @throws IllegalArgumentException if {@code expression} is false
     */
    public static void checkArgument(
            final boolean expression,
            final @NonNull String messageTemplate,
            final @NonNull Object... messageArgs) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(messageTemplate, messageArgs));
        }
    }

    /**
     * Ensures that an string reference passed as a parameter to the calling
     * method is not empty.
     *
     * @param string an string reference
     * @return the string reference that was validated
     * @throws IllegalArgumentException if {@code string} is empty
     */
    public static @NonNull <T extends CharSequence> T checkStringNotEmpty(
            @Nullable final T string) {
        if (TextUtils.isEmpty(string)) {
            throw new IllegalArgumentException();
        }
        return string;
    }

    /**
     * Ensures that an string reference passed as a parameter to the calling
     * method is not empty.
     *
     * @param string an string reference
     * @param errorMessage the exception message to use if the check fails; will
     *     be converted to a string using {@link String#valueOf(Object)}
     * @return the string reference that was validated
     * @throws IllegalArgumentException if {@code string} is empty
     */
    public static @NonNull <T extends CharSequence> T checkStringNotEmpty(
            @Nullable final T string, @NonNull final Object errorMessage) {
        if (TextUtils.isEmpty(string)) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
        return string;
    }

    /**
     * Ensures that an string reference passed as a parameter to the calling method is not empty.
     *
     * @param string an string reference
     * @param messageTemplate a printf-style message template to use if the check fails; will be
     *     converted to a string using {@link String#format(String, Object...)}
     * @param messageArgs arguments for {@code messageTemplate}
     * @return the string reference that was validated
     * @throws IllegalArgumentException if {@code string} is empty
     */
    public static @NonNull <T extends CharSequence> T checkStringNotEmpty(
            @Nullable final T string, @NonNull final String messageTemplate,
            @NonNull final Object... messageArgs) {
        if (TextUtils.isEmpty(string)) {
            throw new IllegalArgumentException(String.format(messageTemplate, messageArgs));
        }
        return string;
    }

    /**
     * Ensures that an object reference passed as a parameter to the calling
     * method is not null.
     *
     * @param reference an object reference
     * @return the non-null reference that was validated
     * @throws NullPointerException if {@code reference} is null
     */
    public static @NonNull <T> T checkNotNull(@Nullable T reference) {
        if (reference == null) {
            throw new NullPointerException();
        }
        return reference;
    }

    /**
     * Ensures that an object reference passed as a parameter to the calling
     * method is not null.
     *
     * @param reference an object reference
     * @param errorMessage the exception message to use if the check fails; will
     *     be converted to a string using {@link String#valueOf(Object)}
     * @return the non-null reference that was validated
     * @throws NullPointerException if {@code reference} is null
     */
    public static @NonNull <T> T checkNotNull(@Nullable T reference, @NonNull Object errorMessage) {
        if (reference == null) {
            throw new NullPointerException(String.valueOf(errorMessage));
        }
        return reference;
    }

    /**
     * Ensures the truth of an expression involving the state of the calling
     * instance, but not involving any parameters to the calling method.
     *
     * @param expression a boolean expression
     * @param message exception message
     * @throws IllegalStateException if {@code expression} is false
     */
    public static void checkState(boolean expression, @Nullable String message) {
        if (!expression) {
            throw new IllegalStateException(message);
        }
    }

    /**
     * Ensures the truth of an expression involving the state of the calling
     * instance, but not involving any parameters to the calling method.
     *
     * @param expression a boolean expression
     * @throws IllegalStateException if {@code expression} is false
     */
    public static void checkState(final boolean expression) {
        checkState(expression, null);
    }

    /**
     * Check the requested flags, throwing if any requested flags are outside the allowed set.
     *
     * @return the validated requested flags.
     */
    public static int checkFlagsArgument(final int requestedFlags, final int allowedFlags) {
        if ((requestedFlags & allowedFlags) != requestedFlags) {
            throw new IllegalArgumentException("Requested flags 0x"
                    + Integer.toHexString(requestedFlags) + ", but only 0x"
                    + Integer.toHexString(allowedFlags) + " are allowed");
        }
        return requestedFlags;
    }

    /**
     * Ensures that that the argument numeric value is non-negative.
     *
     * @param value a numeric int value
     * @param errorMessage the exception message to use if the check fails
     * @return the validated numeric value
     * @throws IllegalArgumentException if {@code value} was negative
     */
    public static @IntRange(from = 0) int checkArgumentNonnegative(final int value,
            @Nullable String errorMessage) {
        if (value < 0) {
            throw new IllegalArgumentException(errorMessage);
        }

        return value;
    }

    /**
     * Ensures that that the argument numeric value is non-negative.
     *
     * @param value a numeric int value
     *
     * @return the validated numeric value
     * @throws IllegalArgumentException if {@code value} was negative
     */
    public static @IntRange(from = 0) int checkArgumentNonnegative(final int value) {
        if (value < 0) {
            throw new IllegalArgumentException();
        }

        return value;
    }

    /**
     * Ensures that the argument int value is within the inclusive range.
     *
     * @param value a int value
     * @param lower the lower endpoint of the inclusive range
     * @param upper the upper endpoint of the inclusive range
     * @param valueName the name of the argument to use if the check fails
     *
     * @return the validated int value
     *
     * @throws IllegalArgumentException if {@code value} was not within the range
     */
    public static int checkArgumentInRange(int value, int lower, int upper,
            @NonNull String valueName) {
        if (value < lower) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
        } else if (value > upper) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
        }

        return value;
    }

    /**
     * Ensures that the argument long value is within the inclusive range.
     *
     * @param value a long value
     * @param lower the lower endpoint of the inclusive range
     * @param upper the upper endpoint of the inclusive range
     * @param valueName the name of the argument to use if the check fails
     *
     * @return the validated long value
     *
     * @throws IllegalArgumentException if {@code value} was not within the range
     */
    public static long checkArgumentInRange(long value, long lower, long upper,
            @NonNull String valueName) {
        if (value < lower) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
        } else if (value > upper) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
        }

        return value;
    }

    /**
     * Ensures that the argument float value is within the inclusive range.
     *
     * @param value a float value
     * @param lower the lower endpoint of the inclusive range
     * @param upper the upper endpoint of the inclusive range
     * @param valueName the name of the argument to use if the check fails
     *
     * @return the validated float value
     *
     * @throws IllegalArgumentException if {@code value} was not within the range
     */
    public static float checkArgumentInRange(float value, float lower, float upper,
            @NonNull String valueName) {
        if (value < lower) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%f, %f] (too low)", valueName, lower, upper));
        } else if (value > upper) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%f, %f] (too high)", valueName, lower, upper));
        }

        return value;
    }

    /**
     * Ensures that the argument double value is within the inclusive range.
     *
     * @param value a double value
     * @param lower the lower endpoint of the inclusive range
     * @param upper the upper endpoint of the inclusive range
     * @param valueName the name of the argument to use if the check fails
     *
     * @return the validated double value
     *
     * @throws IllegalArgumentException if {@code value} was not within the range
     */
    public static double checkArgumentInRange(double value, double lower, double upper,
            @NonNull String valueName) {
        if (value < lower) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%f, %f] (too low)", valueName, lower, upper));
        } else if (value > upper) {
            throw new IllegalArgumentException(
                    String.format(Locale.US,
                            "%s is out of range of [%f, %f] (too high)", valueName, lower, upper));
        }

        return value;
    }

    /**
     * Ensures that the argument floating point value is a finite number.
     *
     * <p>A finite number is defined to be both representable (that is, not NaN) and
     * not infinite (that is neither positive or negative infinity).</p>
     *
     * @param value a floating point value
     * @param valueName the name of the argument to use if the check fails
     *
     * @return the validated floating point value
     *
     * @throws IllegalArgumentException if {@code value} was not finite
     */
    public static float checkArgumentFinite(float value, @NonNull String valueName) {
        if (Float.isNaN(value)) {
            throw new IllegalArgumentException(valueName + " must not be NaN");
        } else if (Float.isInfinite(value)) {
            throw new IllegalArgumentException(valueName + " must not be infinite");
        }

        return value;
    }

    private Preconditions() {
    }
}