Class ParseAttempt<T>

java.lang.Object
com.pixelmonmod.api.parsing.ParseAttempt<T>
Type Parameters:
T - The type being parsed to

public class ParseAttempt<T> extends Object
A data transfer object storing information about an attempt to parse an object
If, for some reason, the syntax is incorrect then it will provide an error in getError()
If there is an exception during parsing then it will provide the error in getException()
  • Constructor Details

    • ParseAttempt

      protected ParseAttempt(T instance, String error, Throwable exception)
  • Method Details

    • get

      @NotNull public T get()
      Gets the instance that was parsed
      It's important to use wasSuccess() to check if this is null
      Returns:
      The parsed instance
      Throws:
      NoSuchElementException - if not successful
    • orThrow

      public <A extends Throwable> T orThrow(A a) throws A
      Gets the instance that was parsed otherwise throws the given exception
      Type Parameters:
      A - The exception's type
      Parameters:
      a - The exception to throw
      Returns:
      The instance
      Throws:
      A - The exception
    • orThrow

      public <A extends Throwable> T orThrow(Supplier<A> a) throws A
      Gets the instance that was parsed otherwise throws the given exception
      Type Parameters:
      A - The exception's type
      Parameters:
      a - The exception to throw
      Returns:
      The instance
      Throws:
      A - The exception
    • getError

      @NotNull public @NotNull String getError()
      Gets the error if an error occurred
      It's important to use wasError() to check if this is null
      Returns:
      The error
      Throws:
      NoSuchElementException - if no error occurred
    • getException

      @NotNull public @NotNull Throwable getException()
      Gets the exception if an exception occurred
      It's important to use wasExceptional() to check if this is null
      Returns:
      The exception
      Throws:
      NoSuchElementException - if no exception occurred
    • wasSuccess

      public boolean wasSuccess()
      Checks if the attempt to parse was successful
      This will return false if there was a syntax error, and you can find the reason for the failure in getError() or in getException()
      Returns:
      If it successfully created a spec
    • wasError

      public boolean wasError()
      Checks if the attempt to parse had an error
      Returns:
      If it had an error during parsing
    • wasExceptional

      public boolean wasExceptional()
      Checks if the attempt to parse had an exception
      Returns:
      If it had an exception during parsing
    • ifSuccessful

      public ParseAttempt<T> ifSuccessful(Consumer<T> consumer)
      Executes the consumer provided if the parse attempt was a success
      Returning itself so you can chain other calls such as ifFailure(Consumer)
      Parameters:
      consumer - The consumer that will run if the parse was successful
      Returns:
      The same instance of the object
    • ifFailure

      public ParseAttempt<T> ifFailure(Consumer<String> consumer)
      Executes the consumer provided if the parse attempt was a failure, but not because of an exception
      Returning itself so you can chain other calls such as ifError(Consumer)
      Parameters:
      consumer - The consumer that will run if the parse was a failure
      Returns:
      The same instance of the object
    • ifError

      public ParseAttempt<T> ifError(Consumer<Throwable> consumer)
      Executes the consumer provided if the parse attempt was a failure with an exception
      Returning itself so you can chain other calls such as ifSuccessful(Consumer)
      Parameters:
      consumer - The consumer that will run if the parse was a failure with an exception
      Returns:
      The same instance of the object
    • map

      public <A> ParseAttempt<A> map(Function<T,A> mapper)
      Maps the parse attempt type from the current one to the desired
      Type Parameters:
      A - The type being mapped to
      Parameters:
      mapper - The mapping function
      Returns:
      The new instance of the mapped parse attempt
    • success

      public static <B> ParseAttempt<B> success(B instance)
      Creates a successful instance of the type B
      Type Parameters:
      B - The type that was parsed
      Parameters:
      instance - The type created
      Returns:
      The successful parsed instance
    • error

      public static <B> ParseAttempt<B> error(String error)
      Creates a failed instance of the type B with given error
      Type Parameters:
      B - The type that was parsed
      Parameters:
      error - The error causing failure
      Returns:
      The failed parsed instance
    • exception

      public static <B> ParseAttempt<B> exception(Throwable exception)
      Creates a failed instance of the type B with given exception
      Type Parameters:
      B - The type that was parsed
      Parameters:
      exception - The exception causing failure
      Returns:
      The failed parsed instance