Class ArgumentAssert

java.lang.Object
top.tangyh.basic.utils.ArgumentAssert

public class ArgumentAssert extends Object
断言
断言某些对象或值是否符合规定,否则抛出异常。经常用于做变量检查
Author:
Looly
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    checkBetween(double value, double min, double max)
    检查值是否在指定范围内
    static int
    checkBetween(int value, int min, int max)
    检查值是否在指定范围内
    static long
    checkBetween(long value, long min, long max)
    检查值是否在指定范围内
    static Number
    checkBetween(Number value, Number min, Number max)
    检查值是否在指定范围内
    checkGt(LocalDateTime value, LocalDateTime min, String... msg)
    检查日志是否大于指定值
    static int
    checkIndex(int index, int size)
    检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
    static int
    checkIndex(int index, int size, String errorMsgTemplate, Object... params)
    检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
    checkLt(LocalDateTime value, LocalDateTime max, String... msg)
    检查日志是否小于指定值
    static <T> T
    contain(Collection<T> collection, T item)
    断言给定值是否被另一个集合包含(即是否不为子串)
    static <T> T
    contain(Collection<T> collection, T item, String errorMsgTemplate, Object... params)
    断言给定值是否被另一个集合包含(即是否不为子串)
    static <T, X extends Throwable>
    T
    contain(Collection<T> collection, T item, Supplier<X> errorSupplier)
    断言给定值是否被另一个集合包含(即是否不为子串)
    static void
    equals(Object expected, Object actual, String errorMsgTemplate, Object... params)
    断言2个字符串是否相等,如果不等用指定错误码抛出异常
    static <X extends Throwable>
    void
    equals(Object expected, Object actual, Supplier<? extends X> supplier)
    断言2个字符串是否相等,如果 不相等 抛出给定的异常
    static void
    isAssignable(Class<?> superType, Class<?> subType)
    断言 superType.isAssignableFrom(subType) 是否为 true.
    static void
    isAssignable(Class<?> superType, Class<?> subType, String errorMsgTemplate, Object... params)
    断言 superType.isAssignableFrom(subType) 是否为 true.
    static void
    isFalse(boolean expression)
    断言是否为假,如果为 true 抛出 ArgumentException 异常
    static void
    isFalse(boolean expression, String errorMsgTemplate, Object... params)
    断言是否为假,如果为 true 抛出 ArgumentException 异常
    static <X extends Throwable>
    void
    isFalse(boolean expression, Supplier<X> errorSupplier)
    断言是否为假,如果为 true 抛出指定类型异常
    并使用指定的函数获取错误信息返回
    static <T> T
    isInstanceOf(Class<?> type, T obj)
    断言给定对象是否是给定类的实例
    static <T> T
    isInstanceOf(Class<?> type, T obj, String errorMsgTemplate, Object... params)
    断言给定对象是否是给定类的实例
    static void
    isNull(Object object)
    断言对象是否为null ,如果不为null 抛出ArgumentException 异常
    static void
    isNull(Object object, String errorMsgTemplate, Object... params)
    断言对象是否为null ,如果不为null 抛出ArgumentException 异常
    static <X extends Throwable>
    void
    isNull(Object object, Supplier<X> errorSupplier)
    断言对象是否为null ,如果不为null 抛出指定类型异常 并使用指定的函数获取错误信息返回
    static void
    isTrue(boolean expression)
    断言是否为真,如果为 false 抛出 ArgumentException 异常
    static void
    isTrue(boolean expression, String errorMsgTemplate, Object... params)
    断言是否为真,如果为 false 抛出 ArgumentException 异常
    static <X extends Throwable>
    void
    isTrue(boolean expression, Supplier<? extends X> supplier)
    断言是否为真,如果为 false 抛出给定的异常
    static <T> T[]
    noNullElements(T[] array)
    断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含
    static <T> T[]
    noNullElements(T[] array, String errorMsgTemplate, Object... params)
    断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含
    static <T, X extends Throwable>
    T[]
    noNullElements(T[] array, Supplier<X> errorSupplier)
    断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含 并使用指定的函数获取错误信息返回
    static <T, E> T
    notAllNull(T object, E obj, String errorMsgTemplate, Object... params)
    判断2个参数是否同时为空
    static <T, E> T
    notAnyNull(T object, E obj, String errorMsgTemplate, Object... params)
    判断2个参数是否任意一个为空
    static <T extends CharSequence>
    T
    notBlank(T text)
    检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 ArgumentException
    static <T extends CharSequence>
    T
    notBlank(T text, String errorMsgTemplate, Object... params)
    检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 ArgumentException
    static <T extends CharSequence, X extends Throwable>
    T
    notBlank(T text, Supplier<X> errorMsgSupplier)
    检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出自定义异常。
    static <T extends CharSequence, X extends Throwable>
    T
    notContain(CharSequence textToSearch, T substring, Supplier<X> errorSupplier)
    断言给定字符串是否不被另一个字符串包含(即是否为子串) 并使用指定的函数获取错误信息返回
    static String
    notContain(String textToSearch, String substring)
    断言给定字符串是否不被另一个字符串包含(即是否为子串)
    static String
    notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params)
    断言给定字符串是否不被另一个字符串包含(即是否为子串)
    static <T> T
    notContain(Collection<T> collection, T item)
    断言给定值是否不被另一个集合包含(即是否为子串)
    static <T> T
    notContain(Collection<T> collection, T item, String errorMsgTemplate, Object... params)
    断言给定值是否不被另一个集合包含(即是否为子串)
    static <T, X extends Throwable>
    T
    notContain(Collection<T> collection, T item, Supplier<X> errorSupplier)
    断言给定值是否不被另一个集合包含(即是否为子串)
    static <T extends CharSequence>
    T
    notEmpty(T text)
    检查给定字符串是否为空,为空抛出 ArgumentException
    static <T> T[]
    notEmpty(T[] array)
    断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素
    static <T> T[]
    notEmpty(T[] array, String errorMsgTemplate, Object... params)
    断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素
    static <T, X extends Throwable>
    T[]
    notEmpty(T[] array, Supplier<X> errorSupplier)
    断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素 并使用指定的函数获取错误信息返回
    static <T extends CharSequence>
    T
    notEmpty(T text, String errorMsgTemplate, Object... params)
    检查给定字符串是否为空,为空抛出 ArgumentException
    static <T extends CharSequence, X extends Throwable>
    T
    notEmpty(T text, Supplier<X> errorSupplier)
    检查给定字符串是否为空,为空抛出自定义异常,并使用指定的函数获取错误信息返回。
    static <T> T
    notNull(T object)
    断言对象是否不为null ,如果为null 抛出ArgumentException 异常
    static <T> T
    notNull(T object, String errorMsgTemplate, Object... params)
    断言对象是否不为null ,如果为null 抛出ArgumentException 异常 Assert that an object is not null .
    static <T, X extends Throwable>
    T
    notNull(T object, Supplier<X> errorSupplier)
    断言对象是否不为null ,如果为null 抛出指定类型异常 并使用指定的函数获取错误信息返回
    static void
    state(boolean expression)
    检查boolean表达式,当检查结果为false时抛出 IllegalStateException
    static void
    state(boolean expression, String errorMsgTemplate, Object... params)
    检查boolean表达式,当检查结果为false时抛出 IllegalStateException
    static void
    state(boolean expression, Supplier<String> errorMsgSupplier)
    检查boolean表达式,当检查结果为false时抛出 IllegalStateException

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ArgumentAssert

      public ArgumentAssert()
  • Method Details

    • isTrue

      public static <X extends Throwable> void isTrue(boolean expression, Supplier<? extends X> supplier) throws X
      断言是否为真,如果为 false 抛出给定的异常
       Assert.isTrue(i > 0, ArgumentException::new);
       
      Type Parameters:
      X - 异常类型
      Parameters:
      expression - 布尔值
      supplier - 指定断言不通过时抛出的异常
      Throws:
      X - if expression is false
    • isTrue

      public static void isTrue(boolean expression, String errorMsgTemplate, Object... params) throws ArgumentException
      断言是否为真,如果为 false 抛出 ArgumentException 异常
       Assert.isTrue(i > 0, "The value must be greater than zero");
       
      Parameters:
      expression - 布尔值
      errorMsgTemplate - 错误抛出异常附带的消息模板,变量用{}代替
      params - 参数列表
      Throws:
      ArgumentException - if expression is false
    • isTrue

      public static void isTrue(boolean expression) throws ArgumentException
      断言是否为真,如果为 false 抛出 ArgumentException 异常
       Assert.isTrue(i > 0, "The value must be greater than zero");
       
      Parameters:
      expression - 布尔值
      Throws:
      ArgumentException - if expression is false
    • isFalse

      public static <X extends Throwable> void isFalse(boolean expression, Supplier<X> errorSupplier) throws X
      断言是否为假,如果为 true 抛出指定类型异常
      并使用指定的函数获取错误信息返回
        Assert.isFalse(i > 0, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      X - 异常类型
      Parameters:
      expression - 布尔值
      errorSupplier - 指定断言不通过时抛出的异常
      Throws:
      X - if expression is false
      Since:
      5.4.5
    • isFalse

      public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws ArgumentException
      断言是否为假,如果为 true 抛出 ArgumentException 异常
       Assert.isFalse(i < 0, "The value must be greater than zero");
       
      Parameters:
      expression - 布尔值
      errorMsgTemplate - 错误抛出异常附带的消息模板,变量用{}代替
      params - 参数列表
      Throws:
      ArgumentException - if expression is false
    • isFalse

      public static void isFalse(boolean expression) throws ArgumentException
      断言是否为假,如果为 true 抛出 ArgumentException 异常
       Assert.isFalse(i < 0);
       
      Parameters:
      expression - 布尔值
      Throws:
      ArgumentException - if expression is false
    • isNull

      public static <X extends Throwable> void isNull(Object object, Supplier<X> errorSupplier) throws X
      断言对象是否为null ,如果不为null 抛出指定类型异常 并使用指定的函数获取错误信息返回
       Assert.isNull(value, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      X - 异常类型
      Parameters:
      object - 被检查的对象
      errorSupplier - 错误抛出异常附带的消息生产接口
      Throws:
      X - if the object is not null
      Since:
      5.4.5
    • isNull

      public static void isNull(Object object, String errorMsgTemplate, Object... params) throws ArgumentException
      断言对象是否为null ,如果不为null 抛出ArgumentException 异常
       Assert.isNull(value, "The value must be null");
       
      Parameters:
      object - 被检查的对象
      errorMsgTemplate - 消息模板,变量使用{}表示
      params - 参数列表
      Throws:
      ArgumentException - if the object is not null
    • isNull

      public static void isNull(Object object) throws ArgumentException
      断言对象是否为null ,如果不为null 抛出ArgumentException 异常
       Assert.isNull(value);
       
      Parameters:
      object - 被检查对象
      Throws:
      ArgumentException - if the object is not null
    • notNull

      public static <T, X extends Throwable> T notNull(T object, Supplier<X> errorSupplier) throws X
      断言对象是否不为null ,如果为null 抛出指定类型异常 并使用指定的函数获取错误信息返回
       Assert.notNull(clazz, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      T - 被检查对象泛型类型
      X - 异常类型
      Parameters:
      object - 被检查对象
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      被检查后的对象 t
      Throws:
      X - if the object is null
      Since:
      5.4.5
    • notNull

      public static <T> T notNull(T object, String errorMsgTemplate, Object... params) throws ArgumentException
      断言对象是否不为null ,如果为null 抛出ArgumentException 异常 Assert that an object is not null .
       Assert.notNull(clazz, "The class must not be null");
       
      Type Parameters:
      T - 被检查对象泛型类型
      Parameters:
      object - 被检查对象
      errorMsgTemplate - 错误消息模板,变量使用{}表示
      params - 参数
      Returns:
      被检查后的对象 t
      Throws:
      ArgumentException - if the object is null
    • notAllNull

      public static <T, E> T notAllNull(T object, E obj, String errorMsgTemplate, Object... params) throws ArgumentException
      判断2个参数是否同时为空
      Parameters:
      object - 参数1
      obj - 参数2
      errorMsgTemplate - 错误消息模板,变量使用{}表示
      params - 参数
      Throws:
      ArgumentException - if the object is null
    • notAnyNull

      public static <T, E> T notAnyNull(T object, E obj, String errorMsgTemplate, Object... params) throws ArgumentException
      判断2个参数是否任意一个为空
      Parameters:
      object - 参数1
      obj - 参数2
      errorMsgTemplate - 错误消息模板,变量使用{}表示
      params - 参数
      Throws:
      ArgumentException - if the object is null
    • notNull

      public static <T> T notNull(T object) throws ArgumentException
      断言对象是否不为null ,如果为null 抛出ArgumentException 异常
       Assert.notNull(clazz);
       
      Type Parameters:
      T - 被检查对象类型
      Parameters:
      object - 被检查对象
      Returns:
      非空对象 t
      Throws:
      ArgumentException - if the object is null
    • notEmpty

      public static <T extends CharSequence, X extends Throwable> T notEmpty(T text, Supplier<X> errorSupplier) throws X
      检查给定字符串是否为空,为空抛出自定义异常,并使用指定的函数获取错误信息返回。
       Assert.notEmpty(name, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      T - 字符串类型
      X - 异常类型
      Parameters:
      text - 被检查字符串
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      非空字符串 t
      Throws:
      X - 被检查字符串为空抛出此异常
      Since:
      5.4.5
      See Also:
      • StrUtil#isNotEmpty(CharSequence)
    • notEmpty

      public static <T extends CharSequence> T notEmpty(T text, String errorMsgTemplate, Object... params) throws ArgumentException
      检查给定字符串是否为空,为空抛出 ArgumentException
       Assert.notEmpty(name, "Name must not be empty");
       
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 被检查字符串
      errorMsgTemplate - 错误消息模板,变量使用{}表示
      params - 参数
      Returns:
      非空字符串 t
      Throws:
      ArgumentException - 被检查字符串为空
      See Also:
      • StrUtil#isNotEmpty(CharSequence)
    • notEmpty

      public static <T extends CharSequence> T notEmpty(T text) throws ArgumentException
      检查给定字符串是否为空,为空抛出 ArgumentException
       Assert.notEmpty(name);
       
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 被检查字符串
      Returns:
      被检查的字符串 t
      Throws:
      ArgumentException - 被检查字符串为空
      See Also:
      • StrUtil#isNotEmpty(CharSequence)
    • notBlank

      public static <T extends CharSequence, X extends Throwable> T notBlank(T text, Supplier<X> errorMsgSupplier) throws X
      检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出自定义异常。 并使用指定的函数获取错误信息返回
       Assert.notBlank(name, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      T - 字符串类型
      X - 异常类型
      Parameters:
      text - 被检查字符串
      errorMsgSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      非空字符串 t
      Throws:
      X - 被检查字符串为空白
      See Also:
      • StrUtil#isNotBlank(CharSequence)
    • notBlank

      public static <T extends CharSequence> T notBlank(T text, String errorMsgTemplate, Object... params) throws ArgumentException
      检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 ArgumentException
       Assert.notBlank(name, "Name must not be blank");
       
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 被检查字符串
      errorMsgTemplate - 错误消息模板,变量使用{}表示
      params - 参数
      Returns:
      非空字符串 t
      Throws:
      ArgumentException - 被检查字符串为空白
      See Also:
      • StrUtil#isNotBlank(CharSequence)
    • notBlank

      public static <T extends CharSequence> T notBlank(T text) throws ArgumentException
      检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 ArgumentException
       Assert.notBlank(name, "Name must not be blank");
       
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 被检查字符串
      Returns:
      非空字符串 t
      Throws:
      ArgumentException - 被检查字符串为空白
      See Also:
      • StrUtil#isNotBlank(CharSequence)
    • notContain

      public static <T extends CharSequence, X extends Throwable> T notContain(CharSequence textToSearch, T substring, Supplier<X> errorSupplier) throws X
      断言给定字符串是否不被另一个字符串包含(即是否为子串) 并使用指定的函数获取错误信息返回
       Assert.notContain(name, "rod", ()->{
            // to query relation message
            return new ArgumentException("relation message to return ");
        });
       
      Type Parameters:
      T - 字符串类型
      X - 异常类型
      Parameters:
      textToSearch - 被搜索的字符串
      substring - 被检查的子串
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      被检查的子串 t
      Throws:
      X - 非子串抛出异常
      Since:
      5.4.5
      See Also:
      • StrUtil#contains(CharSequence, CharSequence)
    • notContain

      public static String notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定字符串是否不被另一个字符串包含(即是否为子串)
       Assert.notContain(name, "rod", "Name must not contain 'rod'");
       
      Parameters:
      textToSearch - 被搜索的字符串
      substring - 被检查的子串
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的子串 string
      Throws:
      ArgumentException - 非子串抛出异常
    • notContain

      public static String notContain(String textToSearch, String substring) throws ArgumentException
      断言给定字符串是否不被另一个字符串包含(即是否为子串)
       Assert.notContain(name, "rod", "Name must not contain 'rod'");
       
      Parameters:
      textToSearch - 被搜索的字符串
      substring - 被检查的子串
      Returns:
      被检查的子串 string
      Throws:
      ArgumentException - 非子串抛出异常
    • contain

      public static <T> T contain(Collection<T> collection, T item) throws ArgumentException
      断言给定值是否被另一个集合包含(即是否不为子串)
       Assert.notContain(names, "张三", "name's 中必须包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
    • contain

      public static <T> T contain(Collection<T> collection, T item, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定值是否被另一个集合包含(即是否不为子串)
       Assert.notContain(names, "张三", "name's 中必须包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
    • contain

      public static <T, X extends Throwable> T contain(Collection<T> collection, T item, Supplier<X> errorSupplier) throws X
      断言给定值是否被另一个集合包含(即是否不为子串)
       Assert.notContain(names, "张三", "name's 中必须包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
      X extends Throwable
    • notContain

      public static <T> T notContain(Collection<T> collection, T item) throws ArgumentException
      断言给定值是否不被另一个集合包含(即是否为子串)
       Assert.notContain(names, "张三", "name's 中不能包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
    • notContain

      public static <T> T notContain(Collection<T> collection, T item, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定值是否不被另一个集合包含(即是否为子串)
       Assert.notContain(names, "张三", "name's 中不能包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
    • notContain

      public static <T, X extends Throwable> T notContain(Collection<T> collection, T item, Supplier<X> errorSupplier) throws X
      断言给定值是否不被另一个集合包含(即是否为子串)
       Assert.notContain(names, "张三", "name's 中不能包含 '张三'");
       
      Parameters:
      collection - 被搜索的集合
      item - 被检查的值
      Returns:
      被检查的值 string
      Throws:
      ArgumentException - 非子串抛出异常
      X extends Throwable
    • notEmpty

      public static <T, X extends Throwable> T[] notEmpty(T[] array, Supplier<X> errorSupplier) throws X
      断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素 并使用指定的函数获取错误信息返回
       Assert.notEmpty(array, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      T - 数组元素类型
      X - 异常类型
      Parameters:
      array - 被检查的数组
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      被检查的数组 t [ ]
      Throws:
      X - if the object array is null or has no elements
      Since:
      5.4.5
      See Also:
      • ArrayUtil#isNotEmpty(Object[])
    • notEmpty

      public static <T> T[] notEmpty(T[] array, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素
       Assert.notEmpty(array, "The array must have elements");
       
      Type Parameters:
      T - 数组元素类型
      Parameters:
      array - 被检查的数组
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的数组 t [ ]
      Throws:
      ArgumentException - if the object array is null or has no elements
    • notEmpty

      public static <T> T[] notEmpty(T[] array) throws ArgumentException
      断言给定数组是否包含元素,数组必须不为 null 且至少包含一个元素
       Assert.notEmpty(array, "The array must have elements");
       
      Type Parameters:
      T - 数组元素类型
      Parameters:
      array - 被检查的数组
      Returns:
      被检查的数组 t [ ]
      Throws:
      ArgumentException - if the object array is null or has no elements
    • noNullElements

      public static <T, X extends Throwable> T[] noNullElements(T[] array, Supplier<X> errorSupplier) throws X
      断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含 并使用指定的函数获取错误信息返回
       Assert.noNullElements(array, ()->{
            // to query relation message
            return new ArgumentException("relation message to return ");
        });
       
      Type Parameters:
      T - 数组元素类型
      X - 异常类型
      Parameters:
      array - 被检查的数组
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      被检查的数组 t [ ]
      Throws:
      X - if the object array contains a null element
      Since:
      5.4.5
      See Also:
      • ArrayUtil#hasNull(Object[])
    • noNullElements

      public static <T> T[] noNullElements(T[] array, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含
       Assert.noNullElements(array, "The array must have non-null elements");
       
      Type Parameters:
      T - 数组元素类型
      Parameters:
      array - 被检查的数组
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的数组 t [ ]
      Throws:
      ArgumentException - if the object array contains a null element
    • noNullElements

      public static <T> T[] noNullElements(T[] array) throws ArgumentException
      断言给定数组是否不包含null元素,如果数组为空或 null将被认为不包含
       Assert.noNullElements(array);
       
      Type Parameters:
      T - 数组元素类型
      Parameters:
      array - 被检查的数组
      Returns:
      被检查的数组 t [ ]
      Throws:
      ArgumentException - if the object array contains a null element
    • notEmpty

      public static <E, T extends Iterable<E>, X extends Throwable> T notEmpty(T collection, Supplier<X> errorSupplier) throws X
      断言给定集合非空 并使用指定的函数获取错误信息返回
       Assert.notEmpty(collection, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      E - 集合元素类型
      T - 集合类型
      X - 异常类型
      Parameters:
      collection - 被检查的集合
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      非空集合 t
      Throws:
      X - if the collection is null or has no elements
      Since:
      5.4.5
      See Also:
      • CollUtil#isNotEmpty(Iterable)
    • notEmpty

      public static <E, T extends Iterable<E>> T notEmpty(T collection, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定集合非空
       Assert.notEmpty(collection, "Collection must have elements");
       
      Type Parameters:
      E - 集合元素类型
      T - 集合类型
      Parameters:
      collection - 被检查的集合
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      非空集合 t
      Throws:
      ArgumentException - if the collection is null or has no elements
    • notEmpty

      public static <E, T extends Iterable<E>> T notEmpty(T collection) throws ArgumentException
      断言给定集合非空
       Assert.notEmpty(collection);
       
      Type Parameters:
      E - 集合元素类型
      T - 集合类型
      Parameters:
      collection - 被检查的集合
      Returns:
      被检查集合 t
      Throws:
      ArgumentException - if the collection is null or has no elements
    • notEmpty

      public static <K, V, T extends Map<K, V>, X extends Throwable> T notEmpty(T map, Supplier<X> errorSupplier) throws X
      断言给定Map非空 并使用指定的函数获取错误信息返回
       Assert.notEmpty(map, ()->{
            // to query relation message
            return new ArgumentException("relation message to return");
        });
       
      Type Parameters:
      K - Key类型
      V - Value类型
      T - Map类型
      X - 异常类型
      Parameters:
      map - 被检查的Map
      errorSupplier - 错误抛出异常附带的消息生产接口
      Returns:
      被检查的Map t
      Throws:
      X - if the map is null or has no entries
      Since:
      5.4.5
      See Also:
      • MapUtil#isNotEmpty(Map)
    • notEmpty

      public static <K, V, T extends Map<K, V>> T notEmpty(T map, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定Map非空
       Assert.notEmpty(map, "Map must have entries");
       
      Type Parameters:
      K - Key类型
      V - Value类型
      T - Map类型
      Parameters:
      map - 被检查的Map
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查的Map t
      Throws:
      ArgumentException - if the map is null or has no entries
    • notEmpty

      public static <K, V, T extends Map<K, V>> T notEmpty(T map) throws ArgumentException
      断言给定Map非空
       Assert.notEmpty(map, "Map must have entries");
       
      Type Parameters:
      K - Key类型
      V - Value类型
      T - Map类型
      Parameters:
      map - 被检查的Map
      Returns:
      被检查的Map t
      Throws:
      ArgumentException - if the map is null or has no entries
    • isInstanceOf

      public static <T> T isInstanceOf(Class<?> type, T obj)
      断言给定对象是否是给定类的实例
       Assert.instanceOf(Foo.class, foo);
       
      Type Parameters:
      T - 被检查对象泛型类型
      Parameters:
      type - 被检查对象匹配的类型
      obj - 被检查对象
      Returns:
      被检查的对象 t
      Throws:
      ArgumentException - if the object is not an instance of clazz
      See Also:
    • isInstanceOf

      public static <T> T isInstanceOf(Class<?> type, T obj, String errorMsgTemplate, Object... params) throws ArgumentException
      断言给定对象是否是给定类的实例
       Assert.instanceOf(Foo.class, foo);
       
      Type Parameters:
      T - 被检查对象泛型类型
      Parameters:
      type - 被检查对象匹配的类型
      obj - 被检查对象
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      被检查对象 t
      Throws:
      ArgumentException - if the object is not an instance of clazz
      See Also:
    • isAssignable

      public static void isAssignable(Class<?> superType, Class<?> subType) throws ArgumentException
      断言 superType.isAssignableFrom(subType) 是否为 true.
       Assert.isAssignable(Number.class, myClass);
       
      Parameters:
      superType - 需要检查的父类或接口
      subType - 需要检查的子类
      Throws:
      ArgumentException - 如果子类非继承父类,抛出此异常
    • isAssignable

      public static void isAssignable(Class<?> superType, Class<?> subType, String errorMsgTemplate, Object... params) throws ArgumentException
      断言 superType.isAssignableFrom(subType) 是否为 true.
       Assert.isAssignable(Number.class, myClass);
       
      Parameters:
      superType - 需要检查的父类或接口
      subType - 需要检查的子类
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Throws:
      ArgumentException - 如果子类非继承父类,抛出此异常
    • state

      public static void state(boolean expression, Supplier<String> errorMsgSupplier) throws IllegalStateException
      检查boolean表达式,当检查结果为false时抛出 IllegalStateException。 并使用指定的函数获取错误信息返回
       Assert.state(id == null, ()->{
            // to query relation message
            return "relation message to return ";
        });
       
      Parameters:
      expression - boolean 表达式
      errorMsgSupplier - 错误抛出异常附带的消息生产接口
      Throws:
      IllegalStateException - 表达式为 false 抛出此异常
    • state

      public static void state(boolean expression, String errorMsgTemplate, Object... params) throws IllegalStateException
      检查boolean表达式,当检查结果为false时抛出 IllegalStateException
       Assert.state(id == null, "The id property must not already be initialized");
       
      Parameters:
      expression - boolean 表达式
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Throws:
      IllegalStateException - 表达式为 false 抛出此异常
    • state

      public static void state(boolean expression) throws IllegalStateException
      检查boolean表达式,当检查结果为false时抛出 IllegalStateException
       Assert.state(id == null);
       
      Parameters:
      expression - boolean 表达式
      Throws:
      IllegalStateException - 表达式为 false 抛出此异常
    • checkIndex

      public static int checkIndex(int index, int size) throws ArgumentException, IndexOutOfBoundsException
      检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
       0 ≤ index < size
       
      Parameters:
      index - 下标
      size - 长度
      Returns:
      检查后的下标 int
      Throws:
      ArgumentException - 如果size < 0 抛出此异常
      IndexOutOfBoundsException - 如果index < 0或者 index ≥ size 抛出此异常
      Since:
      4.1.9
    • checkIndex

      public static int checkIndex(int index, int size, String errorMsgTemplate, Object... params) throws ArgumentException, IndexOutOfBoundsException
      检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
       0 ≤ index < size
       
      Parameters:
      index - 下标
      size - 长度
      errorMsgTemplate - 异常时的消息模板
      params - 参数列表
      Returns:
      检查后的下标 int
      Throws:
      ArgumentException - 如果size < 0 抛出此异常
      IndexOutOfBoundsException - 如果index < 0或者 index ≥ size 抛出此异常
      Since:
      4.1.9
    • checkBetween

      public static int checkBetween(int value, int min, int max)
      检查值是否在指定范围内
      Parameters:
      value - 值
      min - 最小值(包含)
      max - 最大值(包含)
      Returns:
      检查后的长度值 int
      Since:
      4.1.10
    • checkGt

      public static LocalDateTime checkGt(LocalDateTime value, LocalDateTime min, String... msg)
      检查日志是否大于指定值
      Parameters:
      value - 值
      min - 最小值(包含)
      Returns:
      检查后的长度值 int
      Since:
      4.1.10
    • checkLt

      public static LocalDateTime checkLt(LocalDateTime value, LocalDateTime max, String... msg)
      检查日志是否小于指定值
      Parameters:
      value - 值
      max - 最大值(包含)
      Returns:
      检查后的长度值 int
      Since:
      4.1.10
    • checkBetween

      public static long checkBetween(long value, long min, long max)
      检查值是否在指定范围内
      Parameters:
      value - 值
      min - 最小值(包含)
      max - 最大值(包含)
      Returns:
      检查后的长度值 long
      Since:
      4.1.10
    • checkBetween

      public static double checkBetween(double value, double min, double max)
      检查值是否在指定范围内
      Parameters:
      value - 值
      min - 最小值(包含)
      max - 最大值(包含)
      Returns:
      检查后的长度值 double
      Since:
      4.1.10
    • checkBetween

      public static Number checkBetween(Number value, Number min, Number max)
      检查值是否在指定范围内
      Parameters:
      value - 值
      min - 最小值(包含)
      max - 最大值(包含)
      Returns:
      检查后的长度值 number
      Since:
      4.1.10
    • equals

      public static void equals(Object expected, Object actual, String errorMsgTemplate, Object... params)
      断言2个字符串是否相等,如果不等用指定错误码抛出异常
      Parameters:
      expected - 预期的值
      actual - 需要比较的字符串expected
      errorMsgTemplate - 错误抛出异常附带的消息模板,变量用{}代替
      params - 参数列表
      Throws:
      ArgumentException - if expression is false
    • equals

      public static <X extends Throwable> void equals(Object expected, Object actual, Supplier<? extends X> supplier) throws X
      断言2个字符串是否相等,如果 不相等 抛出给定的异常
      Parameters:
      expected - 预期的值
      actual - 需要比较的字符串
      supplier - 指定断言不通过时抛出的异常
      Throws:
      X - if expression is false