Package org.daiitech.naftah.parser
Class VariableLookupResult<T>
java.lang.Object
org.daiitech.naftah.parser.VariableLookupResult<T>
- Type Parameters:
T- the type of the variable's value
Represents the result of a variable lookup operation.
This class encapsulates three pieces of information:
- The name of the variable being looked up.
- The value of the variable, which may be
null. - Whether the variable was found in the context.
This is useful in cases where a variable's value may legitimately be null,
and using Optional<T> is not sufficient to distinguish between "not found"
and "found with a null value".
If isFound() returns false, calling get() will throw
an exception (typically used to indicate a bug or missing variable at runtime).
- Author:
- Chakib Daii
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget()Returns the value of the variable, or throws an exception if it was not found.booleanisFound()Returns whether the variable was found.static <T> VariableLookupResult<T>Creates a result indicating that the variable was not found.static <T> VariableLookupResult<T>Creates a result indicating that the variable was found.Returns the variable value if found, or the given fallback value otherwise.
-
Field Details
-
name
-
value
-
found
private final boolean found
-
-
Constructor Details
-
VariableLookupResult
-
-
Method Details
-
of
Creates a result indicating that the variable was found.- Type Parameters:
T- the type of the variable value- Parameters:
name- the variable namevalue- the value of the variable (may benull)- Returns:
- a
VariableLookupResultindicating success
-
notFound
Creates a result indicating that the variable was not found.- Type Parameters:
T- the expected type of the variable- Parameters:
name- the variable name that was looked up- Returns:
- a
VariableLookupResultindicating failure
-
isFound
public boolean isFound()Returns whether the variable was found.- Returns:
trueif the variable was found;falseotherwise
-
get
Returns the value of the variable, or throws an exception if it was not found.- Returns:
- the variable value (may be
null) - Throws:
RuntimeException- if the variable was not found
-
orElse
Returns the variable value if found, or the given fallback value otherwise.- Parameters:
other- the fallback value to return if not found- Returns:
- the variable value or
otherif not found
-