Package ortus.boxlang.runtime.dynamic
Class Attempt<T>
java.lang.Object
ortus.boxlang.runtime.dynamic.Attempt<T>
This class is a fluent class inspired by Java optionals. It allows for a more
fluent way to handle truthy and falsey values in BoxLang with functional aspects.
It is useful when you have a value that could be null or not, and you want to handle it in a more functional way.
Attemps are also immutable, so you can chain methods to handle the value in a more functional way, but it never mutates the original value.
You can also seed the value with a Function (closure or lambda) that will be executed when the value is requested. This gives you a delayed attempt.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Attempt<T> empty()Create an empty attemptbooleanEquals methodIf a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.<U> Attempt<U> If a value is present, returns the result of applying the givenAttempt-bearing mapping function to the value, otherwise returns an emptyAttempt.get()Get the value of the attemptgetOrDefault(T other) Alias to `orElse` but more fluentinthashCode()Hash code of the valueIf the attempt is NOT present, run the consumerbooleanifFailed()Functional alias toisEmpty()If the attempt is invalid, run the consumer This is useful for side effects If the attempt is empty and invalid, the consumer is not runIf a value is present, performs the given action with the value, otherwise does nothing.ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.If the attempt is valid, run the consumer This is useful for side effects If the attempt is empty and invalid, the consumer is not runbooleanisEmpty()Verifies if the attempt is empty or not using the following rules: - If the value is a function, execute it and set the value to the result - If the value is null, it is empty - If the value is a truthy/falsey value, evaluate itbooleanisNull()Verifies if the value is null or notbooleanVerifies if the attempt is empty or not using the following rules: - If the value is null, it is empty - If the value is castable to BoxLang Boolean, evaluate it - If the value is an object, it is not emptybooleanisValid()Verifies if the attempt is valid or not according to the validation rules registered If the attempt is empty, it is not valid<U> Attempt<U> Map the attempt to a new value with a supplierstatic <T> Attempt<T> of(T value) Create an attempt from a value.If a value is present, returns the Attempt, otherwise returns an Attempt produced by the supplying function.If a value is present, returns the value, otherwise returns the other passed value passedIf a value is present, returns the value, otherwise returns the result produced by the supplying function.orThrow()If a value is present, returns the value, otherwise throws a NoElementExceptionIf a value is present, returns the value, otherwise throws a providedIf a value is present, returns the value, otherwise throws a NoElementException with a custom messagestream()If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.Stores a value to explicitly match againsttoBeBetween(Double min, Double max) Validates the attempt to be between a range of numbers This assumes the value is a number or castable to a number The range is inclusive If the value is null, it is not validValidates the attempt to be a specific BoxLang type that you can pass to theisValidfunction.toMatchRegex(String pattern) Validates the attempt to match a regex pattern with case sensitivity This assumes the value is a string or castable to a stringtoMatchRegex(String pattern, Boolean caseSensitive) Validates the attempt to match a regex pattern with case sensitivity or notRegister a validation function to the attempt This function will be executed when the attempt is evaluated It must return TRUE for the attempt to be validtoString()Returns the string representation of the valuebooleanReturntrueif there is a value present, otherwisefalse.
-
Method Details
-
of
Create an attempt from a value. This can be anything, a truthy or falsey or null- Parameters:
value- The value of the attempt, truthy or falsey- Returns:
- a new attempt with the value
-
empty
Create an empty attempt- Returns:
- An empty attempt
-
toMatchRegex
Validates the attempt to match a regex pattern with case sensitivity This assumes the value is a string or castable to a string- Parameters:
pattern- The pattern to match- Returns:
- The attempt
-
toMatchRegex
Validates the attempt to match a regex pattern with case sensitivity or not- Parameters:
pattern- The pattern to matchcaseSensitive- True if the match is case sensitive, false otherwise- Returns:
- The attempt
-
toBeBetween
Validates the attempt to be between a range of numbers This assumes the value is a number or castable to a number The range is inclusive If the value is null, it is not valid- Parameters:
min- The minimum valuemax- The maximum value- Returns:
- The attempt
-
toBeType
Validates the attempt to be a specific BoxLang type that you can pass to theisValidfunction.- Parameters:
type- The type to validate- Returns:
- The attempt
- See Also:
-
toSatisfy
Register a validation function to the attempt This function will be executed when the attempt is evaluated It must return TRUE for the attempt to be valid- Parameters:
predicate- The predicate to register- Returns:
- The attempt
-
toBe
Stores a value to explicitly match against- Parameters:
other- The value to match against- Returns:
- The attempt
-
isValid
public boolean isValid()Verifies if the attempt is valid or not according to the validation rules registered If the attempt is empty, it is not valid- Returns:
- True if the attempt is valid, false otherwise
-
ifValid
If the attempt is valid, run the consumer This is useful for side effects If the attempt is empty and invalid, the consumer is not run- Parameters:
action- The action to run if the attempt is valid- Returns:
- The attempt
-
ifInvalid
If the attempt is invalid, run the consumer This is useful for side effects If the attempt is empty and invalid, the consumer is not run- Parameters:
action- The action to run if the attempt is invalid- Returns:
- The attempt
-
get
Get the value of the attempt- Returns:
- The value of the attempt
- Throws:
NoElementException- If the attempt is empty
-
isEmpty
public boolean isEmpty()Verifies if the attempt is empty or not using the following rules: - If the value is a function, execute it and set the value to the result - If the value is null, it is empty - If the value is a truthy/falsey value, evaluate it -
isNull
public boolean isNull()Verifies if the value is null or not- Returns:
- True if the attempt is null
-
ifFailed
public boolean ifFailed()Functional alias toisEmpty()- Returns:
- True if the attempt is empty
-
wasSuccessful
public boolean wasSuccessful()Returntrueif there is a value present, otherwisefalse. This is an alias toisPresent()for functional programming- Returns:
trueif there is a value present, otherwisefalse
-
isPresent
public boolean isPresent()Verifies if the attempt is empty or not using the following rules: - If the value is null, it is empty - If the value is castable to BoxLang Boolean, evaluate it - If the value is an object, it is not empty -
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action- The action to perform
-
ifPresentOrElse
If a value is present, performs the given action with the value, otherwise performs the given empty-based action. -
ifEmpty
If the attempt is NOT present, run the consumer- Parameters:
consumer- The consumer to run
-
or
If a value is present, returns the Attempt, otherwise returns an Attempt produced by the supplying function.- Parameters:
supplier- The supplier to run if the attempt is empty
-
orElse
If a value is present, returns the value, otherwise returns the other passed value passed- Parameters:
other- The value to return if the attempt is empty- Returns:
- The value of the attempt or the value passed in
-
getOrDefault
Alias to `orElse` but more fluent- Parameters:
other- The value to return if the attempt is empty- Returns:
- The value of the attempt or the value passed in
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
supplier- The supplier to run if the attempt is empty- Returns:
- The value of the attempt or the value of the supplier
-
map
Map the attempt to a new value with a supplier- Parameters:
mapper- The mapper to map the attempt to- Returns:
- The new attempt
-
flatMap
If a value is present, returns the result of applying the givenAttempt-bearing mapping function to the value, otherwise returns an emptyAttempt.This method is similar to
, but the mapping function is one whose result is already aninvalid reference
#map(Function)Attempt, and if invoked,flatMapdoes not wrap it within an additionalAttempt.- Type Parameters:
U- The type of value of theAttemptreturned by the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Attempt-bearing mapping function to the value of thisAttempt, if a value is present, otherwise an emptyAttempt
-
orThrow
If a value is present, returns the value, otherwise throws a NoElementException- Returns:
- The value of the attempt if present
- Throws:
NoElementException- If the attempt is empty
-
orThrow
If a value is present, returns the value, otherwise throws a NoElementException with a custom message- Parameters:
message- The message to display- Returns:
- The value of the attempt if present
- Throws:
NoElementException- If the attempt is empty
-
orThrow
If a value is present, returns the value, otherwise throws a provided- Parameters:
throwable- The exception to throw if the attempt is emptymessage- The message to display- Returns:
- The value of the attempt if present
- Throws:
NoElementException- If the attempt is empty
-
stream
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream. -
toString
Returns the string representation of the value -
hashCode
public int hashCode()Hash code of the value -
equals
Equals method -
filter
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.- Parameters:
predicate- The predicate to test the value- Returns:
- The attempt if the predicate is true, else an empty attempt
-