Skip to content

Commit

Permalink
SafeFunction.getUsingDefault()
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 6, 2024
1 parent e9be50d commit b344258
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SafeFunction.getUsingDefault()

## [8.6.4] - 2024-07-18

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ public static <T> T getWithDefault( UnsafeSupplier<T, Exception> supplier, Funct
}
return res;
}

/**
* <p>Return the value provided by the supplier, if null will use the alternative supplier.</p>
*
* @param <T> the return type
* @param supplier the {@link UnsafeSupplier} the return value supplier
* @param altSupplier the alternative {@link UnsafeSupplier} the return value supplier
* @return the value evaluated by the supplier
*/
public static <T> T getUsingDefault( UnsafeSupplier<T, Exception> supplier, UnsafeSupplier<T, Exception> altSupplier ) {
return SafeFunction.get( () -> {
T res = supplier.get();
if ( res != null ) {
return res;
} else {
return altSupplier.get();
}
} );
}

/**
* <p>Apply an UnsafeVoid function, using a consumer the handle any Exception raised.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class TestSafeFunction {
private static final DAOException TEST_CHECKED_EX = new DAOException( "test checked" );

private static final DAORuntimeException TEST_RUNTIME_EX = new DAORuntimeException( "test runtime" );

@Test
public void testGetUsingDefault() {
Assert.assertEquals( "2", SafeFunction.getUsingDefault( () -> null, () -> "2" ) );
Assert.assertEquals( "0", SafeFunction.getUsingDefault( () -> "0", () -> "1" ) );
}

@Test
public void testFailGet() {
Expand Down

0 comments on commit b344258

Please sign in to comment.