int size = store.getProperty("cache.limit", 500, Integer::parseInt); boolean enabled = store.getProperty("cache.enabled", true, Boolean::getBoolean);
I took his example and refactored it slightly to return Optional, and I ended up with the following:
public Optional<String> getProperty( String propertyName) { return Optional.ofNullable(map.get(propertyName)); } public <T> Optional<T> getProperty( String propertyName, ThrowingFunction<String,? extends T,? extends Exception> func ) { return getProperty(propertyName).map(val -> { try { return func.apply( val ); } catch ( Exception e ) { LOGGER.severe( () -> "Invalid property transform, will default " + e.getMessage() ); return null; } }); }
This means that the default value ends up being provided by the Optional which is a nice application of OAOO.
int size = store.getProperty("cache.limit", Integer::parseInt).orElse(500); boolean enabled = store.getProperty("cache.enabled", Boolean::getBoolean).orElse(true);
I think this is even tidier; but it does depend on who you feel about using Optionals.
No comments:
Post a Comment