LabelBuilder { public String enumeration(Iterable<String> names, String conjunction) { String enumeration = ""; if (names != null && !Iterables.isEmpty(names)) { int size = Iterables.size(names); if (size == 1) { return QUOTE + label(names.iterator().next()) + QUOTE; } else if (size == 2) { return QUOTE + label(Iterables.getFirst(names, "")) + QUOTE + SPACE + conjunction + SPACE + QUOTE + label(Iterables.getLast(names)) + QUOTE; } else { String last = Iterables.getLast(names); LinkedList<String> allButLast = new LinkedList<>(); Iterables.addAll(allButLast, names); allButLast.removeLast(); enumeration = allButLast.stream() .map(name -> QUOTE + label(name) + QUOTE) .collect(Collectors.joining(", ")); enumeration = enumeration + SPACE + conjunction + SPACE + QUOTE + label(last) + QUOTE; } } return enumeration; }  String label(Property property); String label(String name); String enumeration(Iterable<String> names, String conjunction);  }
@Test public void enumeration() throws Exception { assertEquals("'First'", builder.enumeration(singletonList("first"), "and")); assertEquals("'First' or 'Second'", builder.enumeration(asList("first", "second"), "or")); assertEquals("'First', 'Second' and / or 'Third'", builder.enumeration(asList("first", "second", "third"), "and / or")); }