Skip to content

Instantly share code, notes, and snippets.

@danhyun
Last active August 29, 2015 14:20
Show Gist options
  • Save danhyun/318a15ec52a535739ff0 to your computer and use it in GitHub Desktop.
Save danhyun/318a15ec52a535739ff0 to your computer and use it in GitHub Desktop.
jdk8 refactor
// before
private String getExpandValuesFromSet (Set<String> valueSet, Boolean surroundValueWithQuotes) {
StringBuilder expansionValuesString = new StringBuilder(" (");
Iterator<String> valueIterator = valueSet.iterator();
if (valueIterator.hasNext()) {
String value = valueIterator.next();
if (surroundValueWithQuotes) {
expansionValuesString.append("'" + value + "'");
} else {
expansionValuesString.append(value);
}
while (valueIterator.hasNext()) {
String nextValue = valueIterator.next();
expansionValuesString.append(",");
if (surroundValueWithQuotes) {
expansionValuesString.append("'" + nextValue + "'");
} else {
expansionValuesString.append(nextValue);
}
}
}
expansionValuesString.append(")");
return expansionValuesString.toString();
}
// after
private StringJoiner getExpandValuesFromSet (Set<String> valueSet, Boolean surroundValueWithQuotes) {
StringJoiner clause = new StringJoiner(", ", " (", ") ");
Function<String, String> surrounder =
getQuoteSurrounder(surroundValueWithQuotes);
valueSet.stream()
.map(cleaner)
.map(surrounder)
.forEach(clause::add);
return clause;
}
private Function<String, String> getQuoteSurrounder(boolean surround) {
return surround ?
s -> "'" + s + "'" :
Function.identity();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment