Class VariableLookupResult<T>

java.lang.Object
org.daiitech.naftah.parser.VariableLookupResult<T>
Type Parameters:
T - the type of the variable's value

public final class VariableLookupResult<T> extends Object
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 Details

    • name

      private final String name
    • value

      private final T value
    • found

      private final boolean found
  • Constructor Details

    • VariableLookupResult

      private VariableLookupResult(String name, T value, boolean found)
  • Method Details

    • of

      public static <T> VariableLookupResult<T> of(String name, T value)
      Creates a result indicating that the variable was found.
      Type Parameters:
      T - the type of the variable value
      Parameters:
      name - the variable name
      value - the value of the variable (may be null)
      Returns:
      a VariableLookupResult indicating success
    • notFound

      public static <T> VariableLookupResult<T> notFound(String name)
      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 VariableLookupResult indicating failure
    • isFound

      public boolean isFound()
      Returns whether the variable was found.
      Returns:
      true if the variable was found; false otherwise
    • get

      public T 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

      public T orElse(T other)
      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 other if not found