I have the following example code
public String getAliasSQL() {
StringBuilder builder = new StringBuilder();
if (field.getAlias().isPresent()) {
builder.append(" AS ");
builder.append(field.getAlias().get());
}
return builder.toString();
}
@Override
public Optional<String> getAlias() {
return field.getAlias();
}
And I am wondering if it is possible to do this with a one line statement using the guava or() method or another simple trick.
Something like
public String getAliasSQL() {
return field.getAlias().or("").prefixIfNotEmpty(" AS ");
}
But in a more readable way so it still would be clean code.
field.getAlias().transform(new Function(String, String) {
@Override
public String apply(String alias) {
return " AS " + alias;
}
}).or("");
This is verbose of course, but that is caused by the lack of lambda before Java 8. With Java 8, it becomes
field.getAlias().transform(alias -> " AS " + alias).or("");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.