Class Assert

java.lang.Object
com.sure.tool.lang.Assert

public class Assert extends Object
断言工具类,参考 Hutool 的 Assert 与 Spring 的 Assert 设计。

断言失败时抛出 IllegalArgumentException。

Since:
0.1.0
Author:
suretool
  • Method Details

    • isTrue

      public static void isTrue(boolean expression, String errorMsgTemplate, Object... params)
      断言表达式为真。
      Parameters:
      expression - 表达式
      errorMsgTemplate - 错误信息模板,支持一对花括号占位符
      params - 占位符参数
    • isTrue

      public static void isTrue(boolean expression)
      断言表达式为真。
      Parameters:
      expression - 表达式
    • isFalse

      public static void isFalse(boolean expression, String errorMsgTemplate, Object... params)
      断言表达式为假。
      Parameters:
      expression - 表达式
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
    • isNull

      public static void isNull(Object object, String errorMsgTemplate, Object... params)
      断言对象为 null。
      Parameters:
      object - 对象
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
    • isNotNull

      public static <T> T isNotNull(T object, String errorMsgTemplate, Object... params)
      断言对象非 null。
      Type Parameters:
      T - 对象类型
      Parameters:
      object - 对象
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原对象
    • notNull

      public static <T> T notNull(T object, String errorMsgTemplate, Object... params)
      断言对象非 null。
      Type Parameters:
      T - 对象类型
      Parameters:
      object - 对象
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原对象
    • notEmpty

      public static <T extends CharSequence> T notEmpty(T text, String errorMsgTemplate, Object... params)
      断言字符串非空(null 或空串均失败)。
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 字符串
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原字符串
    • notBlank

      public static <T extends CharSequence> T notBlank(T text, String errorMsgTemplate, Object... params)
      断言字符串非空白。
      Type Parameters:
      T - 字符串类型
      Parameters:
      text - 字符串
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原字符串
    • notEmpty

      public static <T> T[] notEmpty(T[] array, String errorMsgTemplate, Object... params)
      断言数组非空。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原数组
    • notEmpty

      public static <T> Collection<T> notEmpty(Collection<T> collection, String errorMsgTemplate, Object... params)
      断言集合非空。
      Type Parameters:
      T - 元素类型
      Parameters:
      collection - 集合
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原集合
    • notEmpty

      public static <K,V> Map<K,V> notEmpty(Map<K,V> map, String errorMsgTemplate, Object... params)
      断言 Map 非空。
      Type Parameters:
      K - key 类型
      V - value 类型
      Parameters:
      map - Map
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Returns:
      原 Map
    • isInstanceOf

      public static void isInstanceOf(Class<?> type, Object obj, String errorMsgTemplate, Object... params)
      断言对象是指定类型实例。
      Parameters:
      type - 目标类型
      obj - 对象
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
    • match

      public static void match(String regex, CharSequence content, String errorMsgTemplate, Object... params)
      断言字符串匹配正则。
      Parameters:
      regex - 正则表达式
      content - 字符串
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
    • fail

      public static void fail(String errorMsgTemplate, Object... params)
      无条件抛出断言失败异常。
      Parameters:
      errorMsgTemplate - 错误信息模板
      params - 占位符参数
      Throws:
      IllegalArgumentException - 始终抛出