List<String> listToJoin = ...Yes, this works like a charm, but just looks plain ugly. And don't ask how long one has to debug if you get the position of the first = false wrong.
boolean first = true;
StringBuilder result = new StringBuilder();
for(String item : listToJoin) {
if (!first) {
result.append(", ");
}
first = false;
result.append(item);
}
System.out.println(result);
A simple class called Monoflop can help here. A properly commented version can be found on GitHub, but a very short version will do here:
public class Monoflop {
private boolean toggled = false;
/**Using this neat helper results in the following:
* Reads and returns the internal state.
* Toggles it to true once.
*/
public boolean successiveCall() {
if (toggled) {
return true;
}
toggled = true;
return false;
}
/**
* Inverse of successiveCall
*/
public boolean firstCall() {
return !successiveCall();
}
}
List<String> listToJoin = ...Granted, you didn't save a whole lot of line in this example. However the logic is much more visible and you have less possibilities for errors. The only thing which is a bit misleading is that a call named successiveCall() has side-effects. On the otherhad, as it toggles the internal state, I didn't want to make it a getter (isSuccessiveCall()) since that would be even more evil.
Monoflop mf = new Monoflop();
StringBuilder result = new StringBuilder();
for(String item : listToJoin) {
if (mf.successiveCall()) {
result.append(", ");
}
result.append(item);
}
System.out.println(result);
Feel free to use this class in your own code base (but use the one from GitHub - as it is better documented). However, if you like it and you have uses for the fastest dependency injection framework out there, with lots of other features, check out: http://sirius-lib.net (GitHub). SIRIUS (which contains Monoflop) is OpenSource (MIT license) and developed and maintained by scireum GmbH.
No comments:
Post a Comment