Record Class Option.Some<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.functional.monad.Option.Some<T>
Type Parameters:
T - value type - 值类型
All Implemented Interfaces:
Option<T>
Enclosing interface:
Option<T>

public static record Option.Some<T>(T value) extends Record implements Option<T>
Some - Contains a value Some - 包含值
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
  • Nested Class Summary

    Nested classes/interfaces inherited from interface Option

    Option.None<T>, Option.Some<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Some(T value)
    Creates an instance of a Some record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    filter(Predicate<? super T> predicate)
    Filter the value with predicate 使用谓词过滤值
    <U> Option<U>
    flatMap(Function<? super T, Option<U>> mapper)
    Transform to another Option if Some 如果是 Some 则转换为另一个 Option
    <R> R
    fold(Supplier<? extends R> ifNone, Function<? super T, ? extends R> ifSome)
    Fold this Option by applying one of two functions 通过应用两个函数之一来折叠此 Option
    get()
    Get the value, throws if None 获取值,如果是 None 则抛出异常
    getOrElse(Supplier<? extends T> supplier)
    Get value or compute default if None 获取值或计算默认值(如果是 None)
    getOrElse(T defaultValue)
    Get value or default if None 获取值或默认值(如果是 None)
    final int
    Returns a hash code value for this object.
    boolean
    Check if this is None 检查是否为 None
    boolean
    Check if this is Some 检查是否为 Some
    <U> Option<U>
    map(Function<? super T, ? extends U> mapper)
    Transform the value if Some 如果是 Some 则转换值
    onNone(Runnable action)
    Execute action if None 如果是 None 则执行操作
    orElse(Option<T> other)
    Return this or other Option if None 返回本 Option 或其他 Option(如果是 None)
    orElse(Supplier<Option<T>> supplier)
    Return this or computed Option if None 返回本 Option 或计算的 Option(如果是 None)
    peek(Consumer<? super T> action)
    Execute action if Some 如果是 Some 则执行操作
    <L> Either<L,T>
    toEither(L left)
    Convert to Either (None becomes Left) 转换为 Either(None 变为 Left)
    Convert to Optional 转换为 Optional
    Returns a string representation of this record class.
    Returns the value of the value record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Option

    contains, exists, forAll, stream, toTry, toValidation, zip
  • Constructor Details

    • Some

      public Some(T value)
      Creates an instance of a Some record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • isSome

      public boolean isSome()
      Description copied from interface: Option
      Check if this is Some 检查是否为 Some
      Specified by:
      isSome in interface Option<T>
      Returns:
      true if Some - 如果是 Some 返回 true
    • isNone

      public boolean isNone()
      Description copied from interface: Option
      Check if this is None 检查是否为 None
      Specified by:
      isNone in interface Option<T>
      Returns:
      true if None - 如果是 None 返回 true
    • get

      public T get()
      Description copied from interface: Option
      Get the value, throws if None 获取值,如果是 None 则抛出异常
      Specified by:
      get in interface Option<T>
      Returns:
      the value - 值
    • map

      public <U> Option<U> map(Function<? super T, ? extends U> mapper)
      Description copied from interface: Option
      Transform the value if Some 如果是 Some 则转换值
      Specified by:
      map in interface Option<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Option
    • flatMap

      public <U> Option<U> flatMap(Function<? super T, Option<U>> mapper)
      Description copied from interface: Option
      Transform to another Option if Some 如果是 Some 则转换为另一个 Option
      Specified by:
      flatMap in interface Option<T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function returning Option - 返回 Option 的转换函数
      Returns:
      resulting Option
    • filter

      public Option<T> filter(Predicate<? super T> predicate)
      Description copied from interface: Option
      Filter the value with predicate 使用谓词过滤值
      Specified by:
      filter in interface Option<T>
      Parameters:
      predicate - filter condition - 过滤条件
      Returns:
      filtered Option (None if predicate not satisfied)
    • fold

      public <R> R fold(Supplier<? extends R> ifNone, Function<? super T, ? extends R> ifSome)
      Description copied from interface: Option
      Fold this Option by applying one of two functions 通过应用两个函数之一来折叠此 Option

      This is the catamorphism for Option - handles both cases symmetrically.

      这是 Option 的态射 - 对称地处理两种情况。

      Example | 示例:

      String result = option.fold(
          () -> "No value",          // None case
          value -> "Got: " + value    // Some case
      );
      
      Specified by:
      fold in interface Option<T>
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      ifNone - function for None case - None 情况的函数
      ifSome - function for Some case - Some 情况的函数
      Returns:
      result of applying the appropriate function
    • getOrElse

      public T getOrElse(T defaultValue)
      Description copied from interface: Option
      Get value or default if None 获取值或默认值(如果是 None)
      Specified by:
      getOrElse in interface Option<T>
      Parameters:
      defaultValue - default value - 默认值
      Returns:
      value or default
    • getOrElse

      public T getOrElse(Supplier<? extends T> supplier)
      Description copied from interface: Option
      Get value or compute default if None 获取值或计算默认值(如果是 None)
      Specified by:
      getOrElse in interface Option<T>
      Parameters:
      supplier - default value supplier - 默认值供应商
      Returns:
      value or computed default
    • orElse

      public Option<T> orElse(Option<T> other)
      Description copied from interface: Option
      Return this or other Option if None 返回本 Option 或其他 Option(如果是 None)
      Specified by:
      orElse in interface Option<T>
      Parameters:
      other - alternative Option - 备选 Option
      Returns:
      this if Some, other if None
    • orElse

      public Option<T> orElse(Supplier<Option<T>> supplier)
      Description copied from interface: Option
      Return this or computed Option if None 返回本 Option 或计算的 Option(如果是 None)
      Specified by:
      orElse in interface Option<T>
      Parameters:
      supplier - Option supplier - Option 供应商
      Returns:
      this if Some, computed if None
    • toOptional

      public Optional<T> toOptional()
      Description copied from interface: Option
      Convert to Optional 转换为 Optional
      Specified by:
      toOptional in interface Option<T>
      Returns:
      Optional
    • toEither

      public <L> Either<L,T> toEither(L left)
      Description copied from interface: Option
      Convert to Either (None becomes Left) 转换为 Either(None 变为 Left)
      Specified by:
      toEither in interface Option<T>
      Type Parameters:
      L - left type - 左类型
      Parameters:
      left - value for Left if None - None 时的 Left 值
      Returns:
      Either
    • peek

      public Option<T> peek(Consumer<? super T> action)
      Description copied from interface: Option
      Execute action if Some 如果是 Some 则执行操作
      Specified by:
      peek in interface Option<T>
      Parameters:
      action - action to execute - 要执行的操作
      Returns:
      this Option for chaining
    • onNone

      public Option<T> onNone(Runnable action)
      Description copied from interface: Option
      Execute action if None 如果是 None 则执行操作
      Specified by:
      onNone in interface Option<T>
      Parameters:
      action - action to execute - 要执行的操作
      Returns:
      this Option for chaining
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public T value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component