001/*
002 * Copyright 2015 The AppAuth for Android Authors. All Rights Reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the
010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
011 * express or implied. See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package net.openid.appauth;
016
017import android.text.TextUtils;
018import androidx.annotation.NonNull;
019import androidx.annotation.Nullable;
020
021import java.util.Collection;
022
023/**
024 * Utility class for guava style pre-condition checks. Not an official part of the AppAuth API;
025 * only intended for internal use and no guarantees are given on source or binary compatibility
026 * for this class between versions of AppAuth.
027 */
028public final class Preconditions {
029
030    /**
031     * Ensures that an object reference passed as a parameter to the calling method is not null.
032     *
033     * @param reference an object reference
034     * @return the non-null reference that was validated
035     * @throws NullPointerException if `reference` is `null`
036     */
037    public static <T> T checkNotNull(T reference) {
038        if (reference == null) {
039            throw new NullPointerException();
040        }
041        return reference;
042    }
043
044    /**
045     * Ensures that an object reference passed as a parameter to the calling method is not null.
046     *
047     * @param reference an object reference
048     * @param errorMessage the exception message to use if the check fails; will be converted to a
049     *     string using {@link String#valueOf(Object)}
050     * @return the non-null reference that was validated
051     * @throws NullPointerException if `reference` is `null`
052     */
053    public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
054        if (reference == null) {
055            throw new NullPointerException(String.valueOf(errorMessage));
056        }
057        return reference;
058    }
059
060    /**
061     * Ensures that a string is not null or empty.
062     */
063    @NonNull
064    public static String checkNotEmpty(String str, @Nullable Object errorMessage) {
065        // ensure that we throw NullPointerException if the value is null, otherwise,
066        // IllegalArgumentException if it is empty
067        checkNotNull(str, errorMessage);
068        checkArgument(!TextUtils.isEmpty(str), errorMessage);
069        return str;
070    }
071
072    /**
073     * Ensures that a collection is not null or empty.
074     */
075    @NonNull
076    public static <T extends Collection<?>> T checkCollectionNotEmpty(
077            T collection, @Nullable Object errorMessage) {
078        checkNotNull(collection, errorMessage);
079        checkArgument(!collection.isEmpty(), errorMessage);
080        return collection;
081    }
082
083    /**
084     * Ensures that the string is either null, or a non-empty string.
085     */
086    @NonNull
087    public static String checkNullOrNotEmpty(String str, @Nullable Object errorMessage) {
088        if (str != null) {
089            checkNotEmpty(str, errorMessage);
090        }
091        return str;
092    }
093
094    /**
095     * Ensures the truth of an expression involving one or more parameters to the calling method.
096     *
097     * @param expression a boolean expression
098     * @throws IllegalArgumentException if `expression` is `false`
099     */
100    public static void checkArgument(boolean expression) {
101        if (!expression) {
102            throw new IllegalArgumentException();
103        }
104    }
105
106    /**
107     * Ensures the truth of an expression involving one or more parameters to the calling method.
108     *
109     * @param expression a boolean expression
110     * @param errorMessage the exception message to use if the check fails; will be converted to a
111     *     string using {@link String#valueOf(Object)}
112     * @throws IllegalArgumentException if `expression` is `false`
113     */
114    public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
115        if (!expression) {
116            throw new IllegalArgumentException(String.valueOf(errorMessage));
117        }
118    }
119
120    /**
121     * Ensures the truth of an expression involving one or more parameters to the calling method.
122     * @param expression a boolean expression
123     * @param errorTemplate the exception message to use if the check fails; this is used
124     *     as the template for String.format.
125     * @param params the parameters to the exception message.
126     */
127    public static void checkArgument(
128            boolean expression,
129            @NonNull String errorTemplate,
130            Object... params) {
131        if (!expression) {
132            throw new IllegalArgumentException(String.format(errorTemplate, params));
133        }
134    }
135
136    private Preconditions() {
137        throw new IllegalStateException("This type is not intended to be instantiated");
138    }
139}