I realized I was building a lot of comma separated lists in my Suneido Java code. I had used a mixture of approaches.
If I had an array or iterable, I could use things like Guava's Joiner or Ints.join. But if I needed to process the items that wouldn't work. (You could use something like Iterables transform or FluentIterables, but without lambdas, it's a lot of overhead to write a class just for one line of processing.)
Otherwise, I'd use a StringBuilder with a variety of ways to handle getting commas between items but not before the first or after the last.
Sometimes I'd do:
And other times:
Note that in the last two you need to check for an empty list or else the deleteCharAt or substring will fail.
I decided to write a utility class to make my code simpler and more consistent. Here's an example of its use:
The add methods are for the comma separated items, the append methods are just passed to the internal StringBuilder. The comma separator is hard coded, but obviously it would be easy to allow passing it in. I decided to implement the Appendable interface since it was easy.