Class ContainerUtil

java.lang.Object
cloud.opencode.base.core.container.ContainerUtil

public final class ContainerUtil extends Object
Container Utility - Generic size/empty operations for Collection, Map, Array, CharSequence, Optional 容器工具 - 对 Collection、Map、Array、CharSequence、Optional 的通用 size/empty 操作

Features | 主要功能:

  • Generic size() for Collection, Map, Array, CharSequence, Optional - 通用size()方法
  • Generic isEmpty()/isNotEmpty() checks - 通用isEmpty()/isNotEmpty()检查
  • Pattern matching for type dispatch - 使用模式匹配进行类型分派

Usage Examples | 使用示例:

int size = ContainerUtil.size(List.of(1, 2, 3));  // 3
boolean empty = ContainerUtil.isEmpty(Map.of());    // true
boolean notEmpty = ContainerUtil.isNotEmpty("abc"); // true

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes, null returns 0/true for size/isEmpty - 空值安全: 是,null对size返回0,对isEmpty返回true

Performance | 性能特性:

  • Time complexity: O(1) per check - 每次检查 O(1)
  • Space complexity: O(1) - O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • size

      public static int size(Object obj)
      Returns the size of the container object. 返回容器对象的大小。

      Supported types: Collection, Map, CharSequence, Array, Optional. Returns -1 if the object type is not recognized as a container.

      支持的类型:Collection、Map、CharSequence、Array、Optional。 如果对象类型不是可识别的容器则返回 -1。

      Parameters:
      obj - the container object | 容器对象
      Returns:
      the size of the container, or -1 if the object type is not recognized as a container 容器的大小,如果对象类型不是可识别的容器则返回 -1
      API Note:
      Callers should check for a return value of -1 to detect unsupported types. A return value of -1 does not indicate an error, but rather that the given object is not a recognized container type. 调用方应检查返回值是否为 -1 以检测不支持的类型。 返回 -1 并不表示错误,而是表示给定对象不是可识别的容器类型。
    • isEmpty

      public static boolean isEmpty(Object obj)
      Checks if the container object is empty. 检查容器对象是否为空。
      Parameters:
      obj - the container object | 容器对象
      Returns:
      true if null, empty, or unsupported type | 如果为 null、空或不支持的类型返回 true
    • isNotEmpty

      public static boolean isNotEmpty(Object obj)
      Checks if the container object is not empty. 检查容器对象是否非空。
      Parameters:
      obj - the container object | 容器对象
      Returns:
      true if not null and not empty | 如果非 null 且非空返回 true