美文网首页
断言工具类模版

断言工具类模版

作者: aimaile | 来源:发表于2021-09-08 11:32 被阅读0次
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Collection;

/**
 * Utility class for guava style pre-condition checks. Not an official part of the AppAuth API;
 * only intended for internal use and no guarantees are given on source or binary compatibility
 * for this class between versions of AppAuth.
 */
public final 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 `reference` is `null`
     */
    public static <T> T checkNotNull(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 `reference` is `null`
     */
    public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
        if (reference == null) {
            throw new NullPointerException(String.valueOf(errorMessage));
        }
        return reference;
    }

    /**
     * Ensures that a string is not null or empty.
     */
    @NonNull
    public static String checkNotEmpty(String str, @Nullable Object errorMessage) {
        // ensure that we throw NullPointerException if the value is null, otherwise,
        // IllegalArgumentException if it is empty
        checkNotNull(str, errorMessage);
        checkArgument(!TextUtils.isEmpty(str), errorMessage);
        return str;
    }

    /**
     * Ensures that a collection is not null or empty.
     */
    @NonNull
    public static <T extends Collection<?>> T checkCollectionNotEmpty(
            T collection, @Nullable Object errorMessage) {
        checkNotNull(collection, errorMessage);
        checkArgument(!collection.isEmpty(), errorMessage);
        return collection;
    }

    /**
     * Ensures that the string is either null, or a non-empty string.
     */
    @NonNull
    public static String checkNullOrNotEmpty(String str, @Nullable Object errorMessage) {
        if (str != null) {
            checkNotEmpty(str, errorMessage);
        }
        return str;
    }

    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression a boolean expression
     * @throws IllegalArgumentException if `expression` is `false`
     */
    public static void checkArgument(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression a boolean expression
     * @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 `expression` is `false`
     */
    public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     * @param expression a boolean expression
     * @param errorTemplate the exception message to use if the check fails; this is used
     *     as the template for String.format.
     * @param params the parameters to the exception message.
     */
    public static void checkArgument(
            boolean expression,
            @NonNull String errorTemplate,
            Object... params) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(errorTemplate, params));
        }
    }

    private Preconditions() {
        throw new IllegalStateException("This type is not intended to be instantiated");
    }
}

相关文章

  • 断言工具类模版

  • Assert工具类

    org.springframework.util.Assert工具类 1.介绍 Assert :“断言”,它断定某...

  • 算法(01)排序

    //工具类//断言类 //数组生成类 //时间测试类 //排序 冒泡 冒泡排序也叫做起泡排序 执行流程(本课程统一...

  • TestNG断言

    TestNG中的Assertion,也是断言。断言是测试中最难写的部分。 Assert类(硬断言) 断言类是Ass...

  • 模版

    模版 模版函数 使用模版函数不需要指定类型,直接传参就可以了。 模版类 使用模版类需要指定类型。

  • java Freemarker 模版引擎工具类

    package org.fh.util; import java.io.BufferedWriter; impor...

  • ruby断言小花招

    断言类 正式上忽略断言 跳过方法执行

  • SpringBoot内置工具类之 断言 Assert

    说到断言Assert,我们在查看源码时经常看到,它是使用比较频繁的一个工具类,但我也经常忽略它的存在。现在就让我们...

  • 工作小结与规划

    一、新闻爬虫 1. 任务发布根据预先定义的模版库生成,支持工具:模版库维护工具需解决问题:将模版维护工具在线化,和...

  • 个人博客开发之blog-api项目整合MyBatisPlus代码

    整合MybatisPlus 导入依赖 配置文件编写 添加mapper扫码 CRUD代码生成 编写代码生成模版工具类...

网友评论

      本文标题:断言工具类模版

      本文链接:https://www.haomeiwen.com/subject/pfkvwltx.html