I have these two methods:
public static String buildLine(String[] values) {
StringBuilder lineBuilder = new StringBuilder();
for (int i = 0; i < values.length - 1; i++) {
lineBuilder.append(values[i]).append(SEPARATOR);
}
lineBuilder.append(values[values.length - 1]);
return lineBuilder.toString();
}
public static String buildLineFromNodeWorker(String[] values, NodeDecorator nodeWorker) {
StringBuilder lineBuilder = new StringBuilder();
for (int i = 0; i < values.length - 1; i++) {
lineBuilder.append(
nodeWorker.getItemValueFromAttribute(values[i]))
.append(SEPARATOR);
}
lineBuilder
.append(nodeWorker
.getItemValueFromAttribute(values[values.length - 1]));
return lineBuilder.toString();
}
they're identical except for what I'm passing to the append
method from the StringBuilder
.
I'm not able to find a way to write the "business logic" of the loop just once.