public class

Preconditions

extends java.lang.Object

 java.lang.Object

↳androidx.text.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 floatcheckArgumentInRange(float value, float lower, float upper, java.lang.String valueName)

Ensures that the argument floating point 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 intcheckArgumentNonnegative(int value, java.lang.String errorMessage)

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

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.

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

Methods

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 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 float checkArgumentInRange(float value, float lower, float upper, java.lang.String valueName)

Ensures that the argument floating point value is within the inclusive range.

While this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.

Parameters:

value: a floating point 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 floating point 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

Source

/*
 * Copyright 2018 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.text;

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

import java.util.Locale;

// TODO(haoyuchang): Remove this class when we can use the androidx-core Precondition

/**
 * Simple static methods to be called at the start of your own methods to verify
 * correct arguments and state.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public class Preconditions {

    /**
     * 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(final 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(final T reference, final Object errorMessage) {
        if (reference == null) {
            throw new NullPointerException(String.valueOf(errorMessage));
        }
        return reference;
    }

    /**
     * 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,
            final String errorMessage) {
        if (value < 0) {
            throw new IllegalArgumentException(errorMessage);
        }

        return value;
    }

    /**
     * Ensures that the argument floating point value is within the inclusive range.
     *
     * <p>While this can be used to range check against +/- infinity, note that all NaN numbers
     * will always be out of range.</p>
     *
     * @param value     a floating point 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 floating point value
     * @throws IllegalArgumentException if {@code value} was not within the range
     */
    public static float checkArgumentInRange(float value, float lower, float upper,
            String valueName) {
        if (Float.isNaN(value)) {
            throw new IllegalArgumentException(valueName + " must not be NaN");
        } else 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 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,
            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;
    }

    private Preconditions() {
    }
}