Class OpenAssert
java.lang.Object
cloud.opencode.base.core.assertion.OpenAssert
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,%stemplate 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 Summary
Modifier and TypeMethodDescriptionstatic voidexclusiveBetween(double start, double end, double value) Asserts 断言 double 值在排除边界的范围内static voidexclusiveBetween(long start, long end, long value) Asserts 断言 long 值在排除边界的范围内static <T extends Comparable<T>>
TexclusiveBetween(T start, T end, T value) Asserts 断言值在排除边界的范围内 (start, end)static <T extends Comparable<T>>
TexclusiveBetween(T start, T end, T value, String message) Asserts 断言值在排除边界的范围内 (start, end)static voidinclusiveBetween(double start, double end, double value) Asserts 断言 double 值在包含边界的范围内static voidinclusiveBetween(long start, long end, long value) Asserts 断言 long 值在包含边界的范围内static <T extends Comparable<T>>
TinclusiveBetween(T start, T end, T value) Asserts 断言值在包含边界的范围内 [start, end]static <T extends Comparable<T>>
TinclusiveBetween(T start, T end, T value, String message) Asserts 断言值在包含边界的范围内 [start, end]static voidisAssignableFrom(Class<?> superType, Class<?> subType, String message) Asserts 断言类型可赋值static voidAsserts 断言条件为 falsestatic voidisInstanceOf(Class<?> type, Object obj, String message) Asserts 断言对象是指定类型的实例static voidAsserts 断言条件为 truestatic voidAsserts 断言条件为 true(带格式化参数)static voidmatchesPattern(CharSequence input, String pattern, String message) Asserts 断言字符串匹配正则static <T> T[]noNullElements(T[] array, String message) Asserts 断言数组无 null 元素static <T extends Iterable<?>>
TnoNullElements(T iterable, String message) Asserts 断言集合无 null 元素static <T extends CharSequence>
TAsserts 断言字符串非空白static <T> T[]Asserts 断言数组非空static <T extends CharSequence>
TAsserts 断言字符串非空static <T extends Collection<?>>
TAsserts 断言集合非空static <T extends Map<?,?>>
TAsserts 断言 Map 非空static <T> TAsserts 断言对象非 nullstatic <T> TAsserts 断言对象非 null(带格式化参数)static voidAsserts 断言状态条件static voidvalidIndex(int index, int size) Asserts 断言索引有效 [0, size)static voidvalidIndex(int index, int size, String message) Asserts 断言索引有效 [0, size)static <T> T[]validIndex(T[] array, int index, String message) Asserts 断言数组索引有效static <T extends CharSequence>
TvalidIndex(T chars, int index, String message) Asserts 断言字符序列索引有效static <T extends Collection<?>>
TvalidIndex(T collection, int index, String message) Asserts 断言集合索引有效
-
Method Details
-
notNull
Asserts 断言对象非 null -
notNull
-
isTrue
Asserts 断言条件为 true -
isTrue
-
isFalse
Asserts 断言条件为 false -
state
Asserts 断言状态条件 -
notEmpty
Asserts 断言字符串非空 -
notBlank
Asserts 断言字符串非空白 -
matchesPattern
Asserts 断言字符串匹配正则 -
notEmpty
Asserts 断言集合非空 -
notEmpty
-
notEmpty
Asserts 断言数组非空 -
noNullElements
Asserts 断言数组无 null 元素 -
noNullElements
-
inclusiveBetween
Asserts 断言值在包含边界的范围内 [start, end] -
inclusiveBetween
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
Asserts 断言值在排除边界的范围内 (start, end) -
exclusiveBetween
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
Asserts 断言索引有效 [0, size) -
validIndex
Asserts 断言数组索引有效 -
validIndex
Asserts 断言字符序列索引有效 -
validIndex
Asserts 断言集合索引有效 -
isInstanceOf
-
isAssignableFrom
-