Attempts
Fluent functional container for handling nullable values with validation pipelines
π Why Use Attempts?
Traditional vs Attempt Approach
// β Traditional approach - verbose and error-prone
user = userService.get( rc.id );
if ( isNull( user ) ) {
throw( "UserNotFoundException" );
}
email = user.getEmail();
if ( isNull( email ) ) {
throw( "Email not found" );
}
domain = email.listLast( "@" );
// β
Attempt approach - clean and declarative
attempt( userService.get( rc.id ) )
.flatMap( user -> user.getEmail() )
.map( email -> email.listLast( "@" ) )
.ifPresentOrElse(
domain -> println( "Domain: #domain#" ),
() -> throw( "User or email not found" )
);π» Attempts in Code
π Attempt States
State Checking Methods
Method
Returns
Description
Rules for Present State
Examples
ποΈ Creating Attempts
Empty Attempt
Attempt with Value
Creation Patterns
π οΈ Working with Attempts
π Method Reference
π State Checking Methods
isEmpty():boolean
isEmpty():booleanisPresent():boolean
isPresent():booleanisNull():boolean
isNull():booleanhasFailed():boolean / wasSuccessful():boolean
hasFailed():boolean / wasSuccessful():booleanequals( object ):boolean
equals( object ):booleanπ― Value Retrieval Methods
get():any / getOrFail():any
get():any / getOrFail():anyorElse( defaultValue ):any / getOrDefault( defaultValue ):any
orElse( defaultValue ):any / getOrDefault( defaultValue ):anyorElseGet( supplier ):any / getOrSupply( supplier ):any
orElseGet( supplier ):any / getOrSupply( supplier ):anyorThrow( [type], [message|exception] ):any
orThrow( [type], [message|exception] ):anyπ Transformation Methods
map( mapper ):attempt
map( mapper ):attemptfilter( predicate ):attempt
filter( predicate ):attemptflatMap( mapper ):attempt
flatMap( mapper ):attemptComplete Example: Before and After
Chaining Multiple FlatMaps
π Conditional Action Methods
ifPresent( action ):attempt / ifSuccessful( action ):attempt
ifPresent( action ):attempt / ifSuccessful( action ):attemptifEmpty( action ):attempt / ifFailed( action ):attempt
ifEmpty( action ):attempt / ifFailed( action ):attemptifPresentOrElse( presentAction, emptyAction ):attempt
ifPresentOrElse( presentAction, emptyAction ):attemptπ Chaining and Fallback Methods
or( supplier ):attempt
or( supplier ):attemptπ Stream Integration
stream():Stream<T>
stream():Stream<T>toOptional():Optional<T>
toOptional():Optional<T>π§ Utility Methods
toString():String
toString():StringhashCode():int / equals( object ):boolean
hashCode():int / equals( object ):booleanβ
Validation Pipelines
Validation Flow
Validation Process
π― Validation Matchers
toBe( value ):attempt
toBe( value ):attempttoBeBetween( min, max ):attempt
toBeBetween( min, max ):attempttoBeType( type ):attempt
toBeType( type ):attempttoMatchRegex( pattern, [caseSensitive=true] ):attempt
toMatchRegex( pattern, [caseSensitive=true] ):attempttoSatisfy( predicate ):attempt
toSatisfy( predicate ):attemptπ¨ Validation Method Reference
Method
Returns
Description
π Validation Pipeline Examples
Multi-Stage Validation
API Input Validation
Form Validation Pipeline
π‘ Validation Best Practices
π Best Practices Summary
When to Use Attempts
Design Patterns
1. π― Fail Fast with orThrow()
orThrow()2. π Transform Chains
3. π Conditional Logic
4. π Fallback Chains
5. β
Validation Pipelines
Performance Considerations
Code Style Guidelines
Common Mistakes to Avoid
π Related Documentation
π Summary
Last updated
Was this helpful?
