Class Result<T>

java.lang.Object
net.luis.utils.util.Result<T>
Type Parameters:
T - The type of the result
All Implemented Interfaces:
Supplier<Either<T,String>>

public class Result<T> extends Object implements Supplier<Either<T,String>>
Represents the result of an operation that may fail.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Either<T,String>
    The internal result of this instance.
    This is either the result or the error message.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Result(@NotNull Either<T,String> result)
    Constructs a new result with the specified result.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    @NotNull Optional<String>
    Returns the error message of the result as an Optional.
    static <T> @NotNull Result<T>
    error(@NotNull String error)
    Creates a new failed result with the specified error message.
    @NotNull String
    Gets the error message of the operation or throws an exception.
    <R> @NotNull Result<R>
    flatMap(@NotNull Function<T,Result<R>> mapper)
    Maps the result of the operation to another result of the same type.
    This is useful for chaining operations or validating the result.
    If the result is an error, the mapping is not applied.
    @NotNull Either<T,String>
    get()
     
    int
     
    boolean
    Checks if the result is an error.
    boolean
    Checks if the result is successful.
    <R> @NotNull Result<R>
    map(@NotNull Function<T,R> mapper)
    Maps the result of the operation to another type.
    If the result is an error, the mapping is not applied.
    @UnknownNullability T
    orElse(T fallback)
    Gets the result of the operation or the specified default value.
    This is useful for providing a fallback value if the operation fails.
    This method is equivalent to orElseGet(Supplier) with a constant supplier.
    @UnknownNullability T
    orElseGet(@NotNull Supplier<? extends T> supplier)
    Gets the result of the operation or the result of the specified supplier.
    This is useful for providing a fallback value if the operation fails.
    @UnknownNullability T
    Gets the result of the operation or throws an exception.
    <X extends RuntimeException>
    @UnknownNullability T
    orThrow(@NotNull Function<String,? extends X> exceptionSupplier)
    Gets the result of the operation or throws the specified exception.
    @NotNull Optional<T>
    Returns the result of the operation as an Optional.
    static <T> @NotNull Result<T>
    success(T value)
    Creates a new successful result with the specified value.
     

    Methods inherited from class java.lang.Object

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

    • result

      private final Either<T,String> result
      The internal result of this instance.
      This is either the result or the error message.
  • Constructor Details

    • Result

      protected Result(@NotNull @NotNull Either<T,String> result)
      Constructs a new result with the specified result.
      Parameters:
      result - The result of the operation
      Throws:
      NullPointerException - If the result is null
  • Method Details

    • success

      @NotNull public static <T> @NotNull Result<T> success(@Nullable T value)
      Creates a new successful result with the specified value.
      Type Parameters:
      T - The type of the result
      Parameters:
      value - The value of the result
      Returns:
      The created result
    • error

      @NotNull public static <T> @NotNull Result<T> error(@NotNull @NotNull String error)
      Creates a new failed result with the specified error message.
      Type Parameters:
      T - The type of the result
      Parameters:
      error - The error message of the result
      Returns:
      The created result
      Throws:
      NullPointerException - If the error message is null
    • get

      @NotNull public @NotNull Either<T,String> get()
      Specified by:
      get in interface Supplier<T>
      Returns:
      The raw Either of this result
    • isSuccess

      public boolean isSuccess()
      Checks if the result is successful.
      Returns:
      True if the result is successful, otherwise false
    • result

      @NotNull public @NotNull Optional<T> result()
      Returns the result of the operation as an Optional.
      Returns:
      The result of the operation
    • orThrow

      public @UnknownNullability T orThrow()
      Gets the result of the operation or throws an exception.
      Returns:
      The result of the operation
      Throws:
      IllegalStateException - If the result is an error
    • orThrow

      public <X extends RuntimeException> @UnknownNullability T orThrow(@NotNull @NotNull Function<String,? extends X> exceptionSupplier)
      Gets the result of the operation or throws the specified exception.
      Type Parameters:
      X - The type of the exception
      Parameters:
      exceptionSupplier - The supplier holding the exception to be thrown
      Returns:
      The result of the operation
      Throws:
      NullPointerException - If the exception supplier is null
      X - If the result is an error
    • isError

      public boolean isError()
      Checks if the result is an error.
      Returns:
      True if the result is an error, otherwise false
    • error

      @NotNull public @NotNull Optional<String> error()
      Returns the error message of the result as an Optional.
      Returns:
      The error message
    • errorOrThrow

      @NotNull public @NotNull String errorOrThrow()
      Gets the error message of the operation or throws an exception.
      Returns:
      The error message
      Throws:
      IllegalStateException - If the result is not an error
    • map

      @NotNull public <R> @NotNull Result<R> map(@NotNull @NotNull Function<T,R> mapper)
      Maps the result of the operation to another type.
      If the result is an error, the mapping is not applied.
      Type Parameters:
      R - The type of the mapped result
      Parameters:
      mapper - The mapper function
      Returns:
      The mapped result
      Throws:
      NullPointerException - If the mapper is null
    • flatMap

      @NotNull public <R> @NotNull Result<R> flatMap(@NotNull @NotNull Function<T,Result<R>> mapper)
      Maps the result of the operation to another result of the same type.
      This is useful for chaining operations or validating the result.
      If the result is an error, the mapping is not applied.
      Type Parameters:
      R - The type of the mapped result
      Parameters:
      mapper - The mapper function
      Returns:
      The mapped result
      Throws:
      NullPointerException - If the mapper is null
    • orElse

      public @UnknownNullability T orElse(@NotNull T fallback)
      Gets the result of the operation or the specified default value.
      This is useful for providing a fallback value if the operation fails.
      This method is equivalent to orElseGet(Supplier) with a constant supplier.
      Parameters:
      fallback - The fallback value
      Returns:
      The result of the operation or the fallback value
      Throws:
      NullPointerException - If the fallback value is null
    • orElseGet

      public @UnknownNullability T orElseGet(@NotNull @NotNull Supplier<? extends T> supplier)
      Gets the result of the operation or the result of the specified supplier.
      This is useful for providing a fallback value if the operation fails.
      Parameters:
      supplier - The supplier of the fallback value
      Returns:
      The result of the operation or the fallback value
      Throws:
      NullPointerException - If the supplier is null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object