Interface Result<T,E>

Type Parameters:
T - the type of the success value
E - the type of the error value
All Known Implementing Classes:
Result.Error, Result.Ok

public sealed interface Result<T,E> permits Result.Error<T,E>, Result.Ok<T,E>
A sealed interface representing the result of a computation that may either succeed (Ok) or fail (Error). Inspired by the Result type in functional programming languages.
Author:
Chakib Daii
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Represents a failed result containing an error value of type E.
    static final class 
    Represents a successful result containing a value of type T.
  • 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.
    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.
  • Method Details

    • isOk

      boolean isOk()
      Returns true if the result is an Ok, otherwise false.
    • isError

      boolean isError()
      Returns true if the result is an Error, otherwise false.
    • unwrap

      T unwrap() throws NaftahBugError
      Returns the contained success value if present, or throws a NaftahBugError if this is an Error.
      Returns:
      the success value
      Throws:
      NaftahBugError - if called on an Error
    • unwrapError

      E unwrapError() throws NaftahBugError
      Returns the contained error value if present, or throws a NaftahBugError if this is an Ok.
      Returns:
      the error value
      Throws:
      NaftahBugError - if called on an Ok
    • unwrapOr

      T unwrapOr(T defaultValue)
      Returns the success value if this is an Ok, otherwise returns the given default.
      Parameters:
      defaultValue - the value to return if this is an Error
      Returns:
      the success value or the default
    • map

      <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.
      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

      <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.
      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