I was reading this post on using Optionals for method arguments and the consensus seems to be to never use them for that purpose.
Guava Optional as method argument for optional parameters
But are there exceptions to this rule at all on the API side (not application side)? I can't think of a cleaner way to handle certain situations especially in initialization situations that may or may not use an argument parameter.
Take this enumerable I'm building that supplies four database connections our business uses. The top three don't require arguments because the DbManager
factory already has the configuration information it needs. But the SQLite connection needs a URL for the SQLite database being used. Because of that, I needed to turn my Supplier
into a Function<Optional<String>,DatabaseConnection>
.
Is this legitimate case of using Optional
as a method parameter? If not how can I make this enumerable?
private static enum DbConnectionSupplier {
ORACLE(optArg -> DbManager.getOracleConnection()),
MYSQL(optArg -> DbManager.getMySQLConnection()),
TERADATA(optArg -> DbManager.getTeradataConnection()),
SQLITE(optArg -> DbManager.getSQLiteConnection(optArg.get()));
private final Function<Optional<String>, DatabaseConnection> supplier;
private DbConnectionSupplier(Function<Optional<String>, DatabaseConnection> supplier) {
this.supplier = supplier;
}
}