Created
December 13, 2012 17:14
-
-
Save apmckinlay/4278048 to your computer and use it in GitHub Desktop.
A utility class to build comma separated lists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CommaStringBuilder implements Appendable { | |
private final StringBuilder sb; | |
private boolean first = true; | |
public CommaStringBuilder() { | |
sb = new StringBuilder(); | |
} | |
public CommaStringBuilder(String s) { | |
sb = new StringBuilder(s); | |
} | |
public CommaStringBuilder(StringBuilder sb) { | |
this.sb = sb; | |
} | |
public CommaStringBuilder add(String s) { | |
if (first) | |
first = false; | |
else | |
sb.append(","); | |
sb.append(s); | |
return this; | |
} | |
public CommaStringBuilder add(Object x) { | |
return add(x.toString()); | |
} | |
public CommaStringBuilder add(long i) { | |
if (first) | |
first = false; | |
else | |
sb.append(","); | |
sb.append(i); | |
return this; | |
} | |
@Override | |
public CommaStringBuilder append(CharSequence s) { | |
sb.append(s); | |
return this; | |
} | |
@Override | |
public CommaStringBuilder append(CharSequence csq, int start, int end) { | |
sb.append(csq, start, end); | |
return this; | |
} | |
@Override | |
public CommaStringBuilder append(char c) { | |
sb.append(c); | |
return this; | |
} | |
public CommaStringBuilder append(Object x) { | |
sb.append(x); | |
return this; | |
} | |
public CommaStringBuilder append(long i) { | |
sb.append(i); | |
return this; | |
} | |
public void clear() { | |
sb.setLength(0); | |
first = true; | |
} | |
@Override | |
public String toString() { | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment