LogoLogo
GitHubFree book
  • 🏠Introduction
  • Using the Library
    • ðŸŒąGetting Started
      • Adding Result to Your Build
      • Creating Results
    • ðŸŠīBasic Usage
      • Checking Success or Failure
      • Unwrapping Values
      • Conditional Actions
    • 🚀Advanced Usage
      • Screening Results
      • Transforming Results
    • 🏁Recap
  • Add-ons
    • ðŸ’ĪLazy Results
    • ðŸ—ĢïļFluent Assertions
    • 📜Jackson Module
    • 🧑‍🚀Micronaut Serialization
  • Other resources
    • ðŸ“ĶBill of Materials
    • 📈Benchmarks
    • ðŸĪ–Demo Projects
      • Spring Boot Demo Project
      • Micronaut Demo Project
    • ⚖ïļLicense
Powered by GitBook
LogoLogo

Source Code

  • GitHub
  • License

Quality

  • SonarCloud
  • Benchmarks

Documentation

  • Free book
  • Javadoc

Releases

  • Maven Central
  • Bill of Materials

Copyright 2024 Guillermo Calvo

On this page
  • Validating Success
  • Recovering From Failure
  • Conclusion

Was this helpful?

Edit on GitHub
Export as PDF
  1. Using the Library
  2. Advanced Usage

Screening Results

How to reject success values and accept failure values

PreviousAdvanced UsageNextTransforming Results

Last updated 8 months ago

Was this helpful?

Screening mechanisms provide greater flexibility in handling edge cases and enable more robust error recovery strategies.

The following methods allow you to run inline tests on the wrapped value of a result to dynamically transform a success into a failure or a failure into a success.

Validating Success

The method allows you to transform a success into a failure based on certain conditions. It takes two parameters:

  1. A to determine if the success value is acceptable.

  2. A mapping that will produce a failure value if the value is deemed unacceptable.

This can be used to enforce additional validation constraints on success values.

@Test
void testFilter() {
  // Given
  Result<Integer, String> result = success(1);
  // When
  Result<Integer, String> filtered = result.filter(x -> x % 2 == 0, x -> "It's odd");
  // Then
  assertTrue(filtered.hasFailure());
}

In this example, we use a lambda expression to validate that the success value inside result is even. Since the number is odd, it transforms the result into a failure.

Note that it is illegal for the mapping function to return null.

Recovering From Failure

This method is useful for implementing fallback mechanisms or recovery strategies, ensuring the application logic remains resilient and adaptable.

@Test
void testRecover() {
  // Given
  Result<Integer, String> result = failure("OK");
  // When
  Result<Integer, String> filtered = result.recover("OK"::equals, String::length);
  // Then
  assertTrue(filtered.hasSuccess());
}

In this example, we use method references to check if the failure value equals OK and then transform the result into a success.

Conclusion

The method allows you to transform a failure into a success based on certain conditions. It also receives two parameters:

A to determine if the failure value is recoverable.

A mapping that will produce a success value from the acceptable failure value.

We covered how to filter out unwanted success values and accept failure values using and . These methods enable you to refine results based on specific criteria, ensuring that only the relevant values are processed down the line.

🚀
Result::filter
Predicate
Function
Result::recover
Predicate
Function
filter
recover