Class Result.Ok<T,E>

java.lang.Object
org.daiitech.naftah.builtin.lang.Result.Ok<T,E>
Type Parameters:
T - the type of the success value
E - the type of the error value (unused in Ok, but part of Result)
All Implemented Interfaces:
Result<T,E>
Enclosing interface:
Result<T,E>

public static final class Result.Ok<T,E> extends Object implements Result<T,E>
Represents a successful result containing a value of type T.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.daiitech.naftah.builtin.lang.Result

    Result.Error<T,E>, Result.Ok<T,E>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final T
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Ok(T value)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <U> Result<U,E>
    flatMap(Function<? super T,Result<U,E>> mapper)
    Applies a function that returns another Result, effectively flattening the structure.
    boolean
    Returns true if the result is an Error, otherwise false.
    boolean
    Returns true if the result is an Ok, otherwise false.
    <U> Result<U,E>
    map(Function<? super T,? extends U> mapper)
    Maps the success value to another value using the given function, if present.
    static <T, E> Result.Ok<T,E>
    of(T value)
    Creates a new Ok result with the given value.
    Returns the contained success value if present, or throws a NaftahBugError if this is an Error.
    Returns the contained error value if present, or throws a NaftahBugError if this is an Ok.
    unwrapOr(T defaultValue)
    Returns the success value if this is an Ok, otherwise returns the given default.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • value

      private final T value
  • Constructor Details

    • Ok

      private Ok(T value)
  • Method Details

    • of

      public static <T, E> Result.Ok<T,E> of(T value)
      Creates a new Ok result with the given value.
      Parameters:
      value - the success value
      Returns:
      a new Ok instance
    • isOk

      public boolean isOk()
      Returns true if the result is an Ok, otherwise false.
      Specified by:
      isOk in interface Result<T,E>
    • isError

      public boolean isError()
      Returns true if the result is an Error, otherwise false.
      Specified by:
      isError in interface Result<T,E>
    • unwrap

      public T unwrap()
      Returns the contained success value if present, or throws a NaftahBugError if this is an Error.
      Specified by:
      unwrap in interface Result<T,E>
      Returns:
      the success value
    • unwrapError

      public E unwrapError()
      Returns the contained error value if present, or throws a NaftahBugError if this is an Ok.
      Specified by:
      unwrapError in interface Result<T,E>
      Returns:
      the error value
    • unwrapOr

      public T unwrapOr(T defaultValue)
      Returns the success value if this is an Ok, otherwise returns the given default.
      Specified by:
      unwrapOr in interface Result<T,E>
      Parameters:
      defaultValue - the value to return if this is an Error
      Returns:
      the success value or the default
    • map

      public <U> Result<U,E> map(Function<? super T,? extends U> mapper)
      Maps the success value to another value using the given function, if present. If this is an Error, the error is propagated.
      Specified by:
      map in interface Result<T,E>
      Type Parameters:
      U - the type of the result of the mapping function
      Parameters:
      mapper - a function to apply to the success value
      Returns:
      a new Result containing the mapped value or the original error
    • flatMap

      public <U> Result<U,E> flatMap(Function<? super T,Result<U,E>> mapper)
      Applies a function that returns another Result, effectively flattening the structure. If this is an Error, it is propagated unchanged.
      Specified by:
      flatMap in interface Result<T,E>
      Type Parameters:
      U - the type of the value in the returned result
      Parameters:
      mapper - a function returning a Result
      Returns:
      the result of applying the function, or the original error
    • toString

      public String toString()
      Overrides:
      toString in class Object