Class Option<T>

java.lang.Object
com.sure.tool.lang.Option<T>
Type Parameters:
T - 值类型

public final class Option<T> extends Object
JDK Optional 超集门面:补高频便捷方法(getOrElse(T)、 onEmpty(Runnable)、orElse(Option)),保持链式 Fluent 体验。 零依赖,对标 Guava Optional 与 Vavr Option。

使用示例:

String name = Option.ofNullable(user)
        .map(User::getName)
        .filter(n -> !n.isBlank())
        .onEmpty(() -> log("missing name"))
        .getOrElse("anonymous");

null 语义:of(T) 拒绝 null(NPE);ofNullable(T)/ empty() 接受空;空时 get() 抛 NoSuchElementException, getOrElse(T)/onEmpty(Runnable)/orElse(Option) 提供空值处理。

Since:
1.12.0
  • Method Details

    • of

      public static <T> Option<T> of(T value)
      非空值包装(null 抛 NPE,语义与 Optional.of 一致)。
      Type Parameters:
      T - 泛型
      Parameters:
      value - 值
      Returns:
      Option
      Throws:
      NullPointerException - value 为 null
    • ofNullable

      public static <T> Option<T> ofNullable(T value)
      可空包装。
      Type Parameters:
      T - 泛型
      Parameters:
      value - 值(可为 null)
      Returns:
      Option
    • empty

      public static <T> Option<T> empty()
      空 Option。
      Type Parameters:
      T - 泛型
      Returns:
      Option
    • isPresent

      public boolean isPresent()
      是否有值。
      Returns:
      true=有值
    • isEmpty

      public boolean isEmpty()
      是否为空。
      Returns:
      true=空
    • get

      public T get()
      获取值(空抛 NoSuchElementException)。
      Returns:
      值
      Throws:
      NoSuchElementException - 空时
    • getOrElse

      public T getOrElse(T defaultValue)
      取值;空返回默认值。
      Parameters:
      defaultValue - 默认值
      Returns:
      值或默认值
    • getOrElseGet

      public T getOrElseGet(Supplier<T> supplier)
      取值;空由供应器提供。
      Parameters:
      supplier - 供应器
      Returns:
      值或供应值
    • orElse

      public Option<T> orElse(Option<T> other)
      空时取另一个 Option。
      Parameters:
      other - 备选 Option
      Returns:
      有值时返回自身,否则返回 other
    • map

      public <U> Option<U> map(Function<? super T, ? extends U> mapper)
      映射(空短路)。
      Type Parameters:
      U - 新泛型
      Parameters:
      mapper - 映射函数
      Returns:
      新 Option(mapper 返回 null 视为空)
    • flatMap

      public <U> Option<U> flatMap(Function<? super T, ? extends Option<? extends U>> mapper)
      平铺映射。
      Type Parameters:
      U - 新泛型
      Parameters:
      mapper - 返回 Option 的映射函数
      Returns:
      新 Option
    • filter

      public Option<T> filter(Predicate<? super T> predicate)
      谓词过滤(不满足转空)。
      Parameters:
      predicate - 谓词
      Returns:
      过滤后的 Option
    • peek

      public Option<T> peek(Consumer<? super T> consumer)
      有值时消费(链式可用)。
      Parameters:
      consumer - 消费者
      Returns:
      this
    • onEmpty

      public Option<T> onEmpty(Runnable runnable)
      空时执行(链式可用)。
      Parameters:
      runnable - 空时动作
      Returns:
      this
    • ifPresent

      public void ifPresent(Consumer<? super T> consumer)
      有值时消费(终止式)。
      Parameters:
      consumer - 消费者
    • toOptional

      public Optional<T> toOptional()
      转 JDK Optional。
      Returns:
      Optional