ErrorReporter { @Private static void reportList(StringBuilder builder, List<String> messages) { if (messages.isEmpty()) return; LinkedHashSet<String> set = new LinkedHashSet<String>(messages); int size = set.size(); int i = 0; for (String message : set) { if (i++ > 0) { if (i == size) { builder.append(" or "); } else { builder.append(", "); } } builder.append(message); } }    }
@Test public void testReportList() { assertEquals("", reportList()); assertEquals("foo", reportList("foo")); assertEquals("foo or bar", reportList("foo", "bar")); assertEquals("foo, bar or baz", reportList("foo", "bar", "baz")); assertEquals("foo, bar or baz", reportList("foo", "bar", "baz", "baz")); assertEquals("foo or bar", reportList("foo", "foo", "bar")); }