Enum Class Unit

java.lang.Object
java.lang.Enum<Unit>
cloud.opencode.base.functional.monad.Unit
All Implemented Interfaces:
Serializable, Comparable<Unit>, Constable

public enum Unit extends Enum<Unit>
Unit - Represents a valueless result (void equivalent as a type) Unit - 表示无值结果(void 的类型等价物)

Use when a generic API requires a type parameter but the actual value is irrelevant. Unlike Void (which cannot be instantiated), Unit has exactly one value: INSTANCE.

当泛型 API 需要类型参数但实际值无关时使用。 与 Void(不可实例化)不同,Unit 恰好有一个值:INSTANCE

Features | 主要功能:

  • Singleton enum value - 单例枚举值
  • Type-safe void replacement - 类型安全的 void 替代
  • Utility methods for functional pipelines - 函数式管道的实用方法

Usage Examples | 使用示例:

// As a type parameter for generic APIs
// 作为泛型 API 的类型参数
CompletableFuture<Unit> future = runAsync(() -> doWork())
    .thenApply(Unit.ignore());

// As a supplier
// 作为供应者
Supplier<Unit> supplier = Unit.supplier();

// Discard function result
// 丢弃函数结果
Function<String, Unit> discard = Unit.ignore();

Security | 安全性:

  • Thread-safe: Yes (enum singleton) - 线程安全: 是 (枚举单例)
  • Serializable: Yes (enum) - 可序列化: 是 (枚举)
  • Null-safe: Yes (always returns INSTANCE) - 空值安全: 是 (始终返回 INSTANCE)
Since:
JDK 25, opencode-base-functional V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Enum Constant Details

    • INSTANCE

      public static final Unit INSTANCE
      The single Unit value 唯一的 Unit 值
  • Method Details

    • values

      public static Unit[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static Unit valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • ignore

      public static <T> Function<T,Unit> ignore()
      Return a function that discards its input and returns Unit 返回一个丢弃输入并返回 Unit 的函数

      Useful for converting any value-producing operation into a Unit-producing one.

      用于将任何产生值的操作转换为产生 Unit 的操作。

      Type Parameters:
      T - input type (ignored) - 输入类型(被忽略)
      Returns:
      function that always returns INSTANCE - 始终返回 INSTANCE 的函数
    • supplier

      public static Supplier<Unit> supplier()
      Return a supplier that always supplies Unit 返回一个始终提供 Unit 的供应者
      Returns:
      supplier of Unit - Unit 的供应者
    • toString

      public String toString()
      Returns "()" as the string representation of Unit 返回 "()" 作为 Unit 的字符串表示
      Overrides:
      toString in class Enum<Unit>
      Returns:
      "()" - "()"