Class ParseResult<T>

java.lang.Object
com.tnsai.parser.ParseResult<T>
Type Parameters:
T - the type of the successfully parsed value

public final class ParseResult<T> extends Object
Result of a parsing operation, containing either a successfully parsed value or error details.

This is a monadic type similar to Either<Error, T> that allows for safe handling of parsing results without null checks or exception handling.

Usage


 ParseResult<User> result = parser.parse(llmOutput);

 // Check and get
 if (result.isSuccess()) {
     User user = result.get();
 }

 // Map transformation
 ParseResult<String> nameResult = result.map(User::name);

 // Handle both cases
 result.ifSuccessOrElse(
     user -> System.out.println("Parsed: " + user),
     error -> System.err.println("Error: " + error)
 );
 
  • Method Details

    • success

      public static <T> ParseResult<T> success(T value, String rawOutput, long parseTimeMs)
      Creates a successful parse result.
      Type Parameters:
      T - the value type
      Parameters:
      value - the parsed value
      rawOutput - the original raw output
      parseTimeMs - time taken to parse in milliseconds
      Returns:
      successful result
    • success

      public static <T> ParseResult<T> success(T value)
      Creates a successful parse result with default timing.
      Type Parameters:
      T - the value type
      Parameters:
      value - the parsed value
      Returns:
      successful result
    • failure

      public static <T> ParseResult<T> failure(String error, String rawOutput)
      Creates a failed parse result.
      Type Parameters:
      T - the expected value type
      Parameters:
      error - the error message
      rawOutput - the original raw output that failed to parse
      Returns:
      failed result
    • validationFailure

      public static <T> ParseResult<T> validationFailure(String error, List<String> validationErrors, String rawOutput)
      Creates a failed parse result with validation errors.
      Type Parameters:
      T - the expected value type
      Parameters:
      error - the main error message
      validationErrors - list of validation errors
      rawOutput - the original raw output
      Returns:
      failed result
    • isSuccess

      public boolean isSuccess()
      Returns true if parsing was successful.
    • isFailure

      public boolean isFailure()
      Returns true if parsing failed.
    • get

      public T get()
      Gets the parsed value.
      Returns:
      the parsed value
      Throws:
      IllegalStateException - if this is a failure result
    • getOrElse

      public T getOrElse(T defaultValue)
      Gets the parsed value or a default if parsing failed.
      Parameters:
      defaultValue - the default value
      Returns:
      the parsed value or default
    • getError

      public String getError()
      Gets the error message if parsing failed.
      Returns:
      the error message, or null if successful
    • getValidationErrors

      public List<String> getValidationErrors()
      Gets the list of validation errors if any.
      Returns:
      list of validation errors (may be empty)
    • getRawOutput

      public String getRawOutput()
      Gets the original raw output that was parsed.
      Returns:
      the raw output
    • getParseTimeMs

      public long getParseTimeMs()
      Gets the time taken to parse in milliseconds.
      Returns:
      parse time in ms
    • toOptional

      public Optional<T> toOptional()
      Returns the value as an Optional.
      Returns:
      Optional containing the value if successful, empty otherwise
    • map

      public <U> ParseResult<U> map(Function<? super T,? extends U> mapper)
      Maps the successful value to a new type.
      Type Parameters:
      U - the new type
      Parameters:
      mapper - the mapping function
      Returns:
      new result with mapped value or same error
    • flatMap

      public <U> ParseResult<U> flatMap(Function<? super T,ParseResult<U>> mapper)
      Flat maps the successful value to a new ParseResult.
      Type Parameters:
      U - the new type
      Parameters:
      mapper - the mapping function
      Returns:
      the mapped result or same error
    • ifSuccess

      public ParseResult<T> ifSuccess(Consumer<? super T> action)
      Performs the given action if this is a success result.
      Parameters:
      action - the action to perform
      Returns:
      this result for chaining
    • ifFailure

      public ParseResult<T> ifFailure(Consumer<String> action)
      Performs the given action if this is a failure result.
      Parameters:
      action - the action to perform with error message
      Returns:
      this result for chaining
    • ifSuccessOrElse

      public void ifSuccessOrElse(Consumer<? super T> successAction, Consumer<String> failureAction)
      Performs one of two actions depending on success/failure.
      Parameters:
      successActionSpec - action for success
      failureActionSpec - action for failure
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object