Class Preconditions

java.lang.Object
cloud.opencode.base.core.Preconditions

public final class Preconditions extends Object
Preconditions Class - Parameter validation and state checking (Guava-style) 前置条件校验类 - 参数校验和状态检查 (Guava 风格)

Provides parameter validation, state checking and index validation with fluent API support.

用于参数校验和状态检查。所有方法返回校验后的值,支持链式调用。

Features | 主要功能:

  • Null checking (checkNotNull) - 非空校验
  • Argument validation (checkArgument) - 参数校验
  • State validation (checkState) - 状态校验
  • Index validation (checkElementIndex, checkPositionIndex) - 索引校验
  • Formatted error messages with %s placeholders - 格式化错误消息

Usage Examples | 使用示例:

// Null checking - 非空校验
this.name = Preconditions.checkNotNull(name, "name cannot be null");

// Argument validation - 参数校验
Preconditions.checkArgument(age > 0, "age must be positive, got: %s", age);

// State validation - 状态校验
Preconditions.checkState(isInitialized, "service not initialized");

// Index validation - 索引校验
int idx = Preconditions.checkElementIndex(index, list.size(), "index");

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 Type
    Method
    Description
    static void
    checkArgument(boolean expression)
    Checks the argument condition, otherwise throws OpenIllegalArgumentException 检查参数条件,否则抛出 OpenIllegalArgumentException
    static void
    checkArgument(boolean expression, String errorMessage)
    Checks the argument condition, otherwise throws OpenIllegalArgumentException with message 检查参数条件,否则抛出带消息的 OpenIllegalArgumentException
    static void
    checkArgument(boolean expression, String template, Object... args)
    Checks the argument condition, otherwise throws OpenIllegalArgumentException with formatted message 检查参数条件,否则抛出格式化消息的 OpenIllegalArgumentException
    static int
    checkElementIndex(int index, int size)
    Checks if the element index is valid [0, size) 检查元素索引是否有效 [0, size)
    static int
    checkElementIndex(int index, int size, String desc)
    Checks if the element index is valid [0, size) 检查元素索引是否有效 [0, size)
    static <T> T
    checkNotNull(T reference)
    Checks that the object is not null, otherwise throws NullPointerException 检查对象非空,否则抛出 NullPointerException
    static <T> T
    checkNotNull(T reference, String errorMessage)
    Checks that the object is not null, otherwise throws NullPointerException with message 检查对象非空,否则抛出带消息的 NullPointerException
    static <T> T
    checkNotNull(T reference, String template, Object... args)
    Checks that the object is not null, otherwise throws NullPointerException with formatted message 检查对象非空,否则抛出格式化消息的 NullPointerException
    static int
    checkPositionIndex(int index, int size)
    Checks if the position index is valid [0, size] 检查位置索引是否有效 [0, size]
    static int
    checkPositionIndex(int index, int size, String desc)
    Checks if the position index is valid [0, size] 检查位置索引是否有效 [0, size]
    static void
    checkPositionIndexes(int start, int end, int size)
    Checks if the position range is valid [start, end] and end <= size.
    static void
    checkState(boolean expression)
    Checks the state condition, otherwise throws OpenIllegalStateException 检查状态条件,否则抛出 OpenIllegalStateException
    static void
    checkState(boolean expression, String errorMessage)
    Checks the state condition, otherwise throws OpenIllegalStateException with message 检查状态条件,否则抛出带消息的 OpenIllegalStateException
    static void
    checkState(boolean expression, String template, Object... args)
    Checks the state condition, otherwise throws OpenIllegalStateException with formatted message 检查状态条件,否则抛出格式化消息的 OpenIllegalStateException

    Methods inherited from class Object

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

    • checkNotNull

      public static <T> T checkNotNull(T reference)
      Checks that the object is not null, otherwise throws NullPointerException 检查对象非空,否则抛出 NullPointerException
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      reference - the object to check | 待检查的对象
      Returns:
      the validated object (supports chaining) | 校验后的对象(支持链式调用)
      Throws:
      NullPointerException - if the object is null | 如果对象为 null
    • checkNotNull

      public static <T> T checkNotNull(T reference, String errorMessage)
      Checks that the object is not null, otherwise throws NullPointerException with message 检查对象非空,否则抛出带消息的 NullPointerException
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      reference - the object to check | 待检查的对象
      errorMessage - the error message | 异常消息
      Returns:
      the validated object (supports chaining) | 校验后的对象(支持链式调用)
      Throws:
      NullPointerException - if the object is null | 如果对象为 null
    • checkNotNull

      public static <T> T checkNotNull(T reference, String template, Object... args)
      Checks that the object is not null, otherwise throws NullPointerException with formatted message 检查对象非空,否则抛出格式化消息的 NullPointerException
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      reference - the object to check | 待检查的对象
      template - the message template with %s placeholders | 消息模板,使用 %s 作为占位符
      args - the template arguments | 模板参数
      Returns:
      the validated object (supports chaining) | 校验后的对象(支持链式调用)
      Throws:
      NullPointerException - if the object is null | 如果对象为 null
    • checkArgument

      public static void checkArgument(boolean expression)
      Checks the argument condition, otherwise throws OpenIllegalArgumentException 检查参数条件,否则抛出 OpenIllegalArgumentException
      Parameters:
      expression - the condition expression | 条件表达式
      Throws:
      OpenIllegalArgumentException - if the condition is false | 如果条件为 false
    • checkArgument

      public static void checkArgument(boolean expression, String errorMessage)
      Checks the argument condition, otherwise throws OpenIllegalArgumentException with message 检查参数条件,否则抛出带消息的 OpenIllegalArgumentException
      Parameters:
      expression - the condition expression | 条件表达式
      errorMessage - the error message | 异常消息
      Throws:
      OpenIllegalArgumentException - if the condition is false | 如果条件为 false
    • checkArgument

      public static void checkArgument(boolean expression, String template, Object... args)
      Checks the argument condition, otherwise throws OpenIllegalArgumentException with formatted message 检查参数条件,否则抛出格式化消息的 OpenIllegalArgumentException
      Parameters:
      expression - the condition expression | 条件表达式
      template - the message template with %s placeholders | 消息模板,使用 %s 作为占位符
      args - the template arguments | 模板参数
      Throws:
      OpenIllegalArgumentException - if the condition is false | 如果条件为 false
    • checkState

      public static void checkState(boolean expression)
      Checks the state condition, otherwise throws OpenIllegalStateException 检查状态条件,否则抛出 OpenIllegalStateException
      Parameters:
      expression - the condition expression | 条件表达式
      Throws:
      OpenIllegalStateException - if the condition is false | 如果条件为 false
    • checkState

      public static void checkState(boolean expression, String errorMessage)
      Checks the state condition, otherwise throws OpenIllegalStateException with message 检查状态条件,否则抛出带消息的 OpenIllegalStateException
      Parameters:
      expression - the condition expression | 条件表达式
      errorMessage - the error message | 异常消息
      Throws:
      OpenIllegalStateException - if the condition is false | 如果条件为 false
    • checkState

      public static void checkState(boolean expression, String template, Object... args)
      Checks the state condition, otherwise throws OpenIllegalStateException with formatted message 检查状态条件,否则抛出格式化消息的 OpenIllegalStateException
      Parameters:
      expression - the condition expression | 条件表达式
      template - the message template with %s placeholders | 消息模板,使用 %s 作为占位符
      args - the template arguments | 模板参数
      Throws:
      OpenIllegalStateException - if the condition is false | 如果条件为 false
    • checkElementIndex

      public static int checkElementIndex(int index, int size)
      Checks if the element index is valid [0, size) 检查元素索引是否有效 [0, size)
      Parameters:
      index - the index value | 索引值
      size - the collection size | 集合大小
      Returns:
      the validated index | 校验后的索引
      Throws:
      OpenIllegalArgumentException - if the index is invalid | 如果索引无效
    • checkElementIndex

      public static int checkElementIndex(int index, int size, String desc)
      Checks if the element index is valid [0, size) 检查元素索引是否有效 [0, size)
      Parameters:
      index - the index value | 索引值
      size - the collection size | 集合大小
      desc - the index description | 索引描述
      Returns:
      the validated index | 校验后的索引
      Throws:
      OpenIllegalArgumentException - if the index is invalid | 如果索引无效
    • checkPositionIndex

      public static int checkPositionIndex(int index, int size)
      Checks if the position index is valid [0, size] 检查位置索引是否有效 [0, size]
      Parameters:
      index - the index value | 索引值
      size - the collection size | 集合大小
      Returns:
      the validated index | 校验后的索引
      Throws:
      OpenIllegalArgumentException - if the index is invalid | 如果索引无效
    • checkPositionIndex

      public static int checkPositionIndex(int index, int size, String desc)
      Checks if the position index is valid [0, size] 检查位置索引是否有效 [0, size]
      Parameters:
      index - the index value | 索引值
      size - the collection size | 集合大小
      desc - the index description | 索引描述
      Returns:
      the validated index | 校验后的索引
      Throws:
      OpenIllegalArgumentException - if the index is invalid | 如果索引无效
    • checkPositionIndexes

      public static void checkPositionIndexes(int start, int end, int size)
      Checks if the position range is valid [start, end] and end <= size. 检查位置范围是否有效 [start, end]end <= size
      Parameters:
      start - the start index | 起始索引
      end - the end index | 结束索引
      size - the collection size | 集合大小
      Throws:
      OpenIllegalArgumentException - if the range is invalid | 如果范围无效