Class OpenAssert

java.lang.Object
cloud.opencode.base.core.assertion.OpenAssert

public final class OpenAssert extends Object
Assertion Utility - Spring Assert-style validation for business application developers 断言工具类 - 面向业务应用开发者的 Spring Assert 风格验证

Provides rich assertion methods (notNull, notEmpty, notBlank, inclusiveBetween, isInstanceOf, matchesPattern, etc.) designed for business application validation. Throws OpenException subclasses on validation failures.

提供丰富的断言方法(notNull、notEmpty、notBlank、inclusiveBetween、 isInstanceOf、matchesPattern 等),专为业务应用验证设计。 验证失败时抛出 OpenException 子类。

When to use this class vs Preconditions | 本类与 Preconditions 的选择:

  • OpenAssert — Spring Assert-style, for business application code: rich validation API (notNull, notEmpty, notBlank, inclusiveBetween, isInstanceOf, matchesPattern), collection/map/array emptiness checks, type checks. 适用于业务应用代码:丰富的验证 API,集合/Map/数组空值检查,类型检查。
  • Preconditions — Guava-style, for library/framework internals: compact API (checkNotNull, checkArgument, checkState), index boundary checks, %s template formatting. 适用于库/框架内部:紧凑的 API,索引边界检查,%s 模板格式化。

Features | 主要功能:

  • Null checks (notNull) - 空值检查
  • Boolean checks (isTrue, isFalse, state) - 布尔检查
  • String checks (notEmpty, notBlank, matchesPattern) - 字符串检查
  • Collection checks (notEmpty, noNullElements) - 集合检查
  • Range checks (inclusiveBetween, exclusiveBetween) - 范围检查
  • Index checks (validIndex) - 索引检查
  • Type checks (isInstanceOf, isAssignableFrom) - 类型检查

Usage Examples | 使用示例:

OpenAssert.notNull(user, "User must not be null");
OpenAssert.notBlank(name, "Name must not be blank");
OpenAssert.isTrue(age > 0, "Age must be positive");
OpenAssert.inclusiveBetween(1, 100, value);
OpenAssert.validIndex(index, list.size());

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • notNull

      public static <T> T notNull(T object, String message)
      Asserts 断言对象非 null
    • notNull

      public static <T> T notNull(T object, String template, Object... args)
      Asserts 断言对象非 null(带格式化参数)
    • isTrue

      public static void isTrue(boolean expression, String message)
      Asserts 断言条件为 true
    • isTrue

      public static void isTrue(boolean expression, String template, Object... args)
      Asserts 断言条件为 true(带格式化参数)
    • isFalse

      public static void isFalse(boolean expression, String message)
      Asserts 断言条件为 false
    • state

      public static void state(boolean expression, String message)
      Asserts 断言状态条件
    • notEmpty

      public static <T extends CharSequence> T notEmpty(T cs, String message)
      Asserts 断言字符串非空
    • notBlank

      public static <T extends CharSequence> T notBlank(T cs, String message)
      Asserts 断言字符串非空白
    • matchesPattern

      public static void matchesPattern(CharSequence input, String pattern, String message)
      Asserts 断言字符串匹配正则
    • notEmpty

      public static <T extends Collection<?>> T notEmpty(T collection, String message)
      Asserts 断言集合非空
    • notEmpty

      public static <T extends Map<?,?>> T notEmpty(T map, String message)
      Asserts 断言 Map 非空
    • notEmpty

      public static <T> T[] notEmpty(T[] array, String message)
      Asserts 断言数组非空
    • noNullElements

      public static <T> T[] noNullElements(T[] array, String message)
      Asserts 断言数组无 null 元素
    • noNullElements

      public static <T extends Iterable<?>> T noNullElements(T iterable, String message)
      Asserts 断言集合无 null 元素
    • inclusiveBetween

      public static <T extends Comparable<T>> T inclusiveBetween(T start, T end, T value)
      Asserts 断言值在包含边界的范围内 [start, end]
    • inclusiveBetween

      public static <T extends Comparable<T>> T inclusiveBetween(T start, T end, T value, String message)
      Asserts 断言值在包含边界的范围内 [start, end]
    • inclusiveBetween

      public static void inclusiveBetween(long start, long end, long value)
      Asserts 断言 long 值在包含边界的范围内
    • inclusiveBetween

      public static void inclusiveBetween(double start, double end, double value)
      Asserts 断言 double 值在包含边界的范围内
    • exclusiveBetween

      public static <T extends Comparable<T>> T exclusiveBetween(T start, T end, T value)
      Asserts 断言值在排除边界的范围内 (start, end)
    • exclusiveBetween

      public static <T extends Comparable<T>> T exclusiveBetween(T start, T end, T value, String message)
      Asserts 断言值在排除边界的范围内 (start, end)
    • exclusiveBetween

      public static void exclusiveBetween(long start, long end, long value)
      Asserts 断言 long 值在排除边界的范围内
    • exclusiveBetween

      public static void exclusiveBetween(double start, double end, double value)
      Asserts 断言 double 值在排除边界的范围内
    • validIndex

      public static void validIndex(int index, int size)
      Asserts 断言索引有效 [0, size)
    • validIndex

      public static void validIndex(int index, int size, String message)
      Asserts 断言索引有效 [0, size)
    • validIndex

      public static <T> T[] validIndex(T[] array, int index, String message)
      Asserts 断言数组索引有效
    • validIndex

      public static <T extends CharSequence> T validIndex(T chars, int index, String message)
      Asserts 断言字符序列索引有效
    • validIndex

      public static <T extends Collection<?>> T validIndex(T collection, int index, String message)
      Asserts 断言集合索引有效
    • isInstanceOf

      public static void isInstanceOf(Class<?> type, Object obj, String message)
      Asserts 断言对象是指定类型的实例
    • isAssignableFrom

      public static void isAssignableFrom(Class<?> superType, Class<?> subType, String message)
      Asserts 断言类型可赋值