Package com.pixelmonmod.api.parsing
Class ParseAttempt<T>
java.lang.Object
com.pixelmonmod.api.parsing.ParseAttempt<T>
- Type Parameters:
T
- The type being parsed to
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
If there is an exception during parsing then it will provide the error in
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 Summary
ModifierConstructorDescriptionprotected
ParseAttempt
(T instance, String error, Throwable exception) -
Method Summary
Modifier and TypeMethodDescriptionstatic <B> ParseAttempt<B>
Creates a failed instance of the type B with given errorstatic <B> ParseAttempt<B>
Creates a failed instance of the type B with given exceptionget()
Gets the instance that was parsed
It's important to usewasSuccess()
to check if this is null@NotNull String
getError()
Gets the error if an error occurred
It's important to usewasError()
to check if this is null@NotNull Throwable
Gets the exception if an exception occurred
It's important to usewasExceptional()
to check if this is nullExecutes the consumer provided if the parse attempt was a failure with an exception
Returning itself so you can chain other calls such asifSuccessful(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 asifError(Consumer)
ifSuccessful
(Consumer<T> consumer) Executes the consumer provided if the parse attempt was a success
Returning itself so you can chain other calls such asifFailure(Consumer)
<A> ParseAttempt<A>
Maps the parse attempt type from the current one to the desiredorThrow
(A a) Gets the instance that was parsed otherwise throws the given exceptionGets the instance that was parsed otherwise throws the given exceptionstatic <B> ParseAttempt<B>
success
(B instance) Creates a successful instance of the type Bboolean
wasError()
Checks if the attempt to parse had an errorboolean
Checks if the attempt to parse had an exceptionboolean
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 ingetError()
or ingetException()
-
Constructor Details
-
ParseAttempt
-
-
Method Details
-
get
Gets the instance that was parsed
It's important to usewasSuccess()
to check if this is null- Returns:
- The parsed instance
- Throws:
NoSuchElementException
- if not successful
-
orThrow
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
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
Gets the error if an error occurred
It's important to usewasError()
to check if this is null- Returns:
- The error
- Throws:
NoSuchElementException
- if no error occurred
-
getException
Gets the exception if an exception occurred
It's important to usewasExceptional()
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 ingetError()
or ingetException()
- 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
Executes the consumer provided if the parse attempt was a success
Returning itself so you can chain other calls such asifFailure(Consumer)
- Parameters:
consumer
- The consumer that will run if the parse was successful- Returns:
- The same instance of the object
-
ifFailure
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 asifError(Consumer)
- Parameters:
consumer
- The consumer that will run if the parse was a failure- Returns:
- The same instance of the object
-
ifError
Executes the consumer provided if the parse attempt was a failure with an exception
Returning itself so you can chain other calls such asifSuccessful(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
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
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
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
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
-