Package com.tnsai.parser
Class ParseResult<T>
java.lang.Object
com.tnsai.parser.ParseResult<T>
- Type Parameters:
T- the type of the successfully parsed value
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 Summary
Modifier and TypeMethodDescriptionbooleanstatic <T> ParseResult<T> Creates a failed parse result.<U> ParseResult<U> flatMap(Function<? super T, ParseResult<U>> mapper) Flat maps the successful value to a new ParseResult.get()Gets the parsed value.getError()Gets the error message if parsing failed.Gets the parsed value or a default if parsing failed.longGets the time taken to parse in milliseconds.Gets the original raw output that was parsed.Gets the list of validation errors if any.inthashCode()Performs the given action if this is a failure result.Performs the given action if this is a success result.voidifSuccessOrElse(Consumer<? super T> successAction, Consumer<String> failureAction) Performs one of two actions depending on success/failure.booleanReturns true if parsing failed.booleanReturns true if parsing was successful.<U> ParseResult<U> Maps the successful value to a new type.static <T> ParseResult<T> success(T value) Creates a successful parse result with default timing.static <T> ParseResult<T> Creates a successful parse result.Returns the value as an Optional.toString()static <T> ParseResult<T> validationFailure(String error, List<String> validationErrors, String rawOutput) Creates a failed parse result with validation errors.
-
Method Details
-
success
Creates a successful parse result.- Type Parameters:
T- the value type- Parameters:
value- the parsed valuerawOutput- the original raw outputparseTimeMs- time taken to parse in milliseconds- Returns:
- successful result
-
success
Creates a successful parse result with default timing.- Type Parameters:
T- the value type- Parameters:
value- the parsed value- Returns:
- successful result
-
failure
Creates a failed parse result.- Type Parameters:
T- the expected value type- Parameters:
error- the error messagerawOutput- 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 messagevalidationErrors- list of validation errorsrawOutput- 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
Gets the parsed value.- Returns:
- the parsed value
- Throws:
IllegalStateException- if this is a failure result
-
getOrElse
Gets the parsed value or a default if parsing failed.- Parameters:
defaultValue- the default value- Returns:
- the parsed value or default
-
getError
Gets the error message if parsing failed.- Returns:
- the error message, or null if successful
-
getValidationErrors
Gets the list of validation errors if any.- Returns:
- list of validation errors (may be empty)
-
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
Returns the value as an Optional.- Returns:
- Optional containing the value if successful, empty otherwise
-
map
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
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
Performs the given action if this is a success result.- Parameters:
action- the action to perform- Returns:
- this result for chaining
-
ifFailure
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
Performs one of two actions depending on success/failure.- Parameters:
successActionSpec- action for successfailureActionSpec- action for failure
-
toString
-
equals
-
hashCode
public int hashCode()
-