Printer { static String format(Object e, Formatter formatter) { if (formatter != null) { return formatter.format(e); } if (e instanceof Object[]) { return Arrays.toString((Object[]) e); } return String.valueOf(e); } private  Printer(); static String print(Collection<?> list, String title, int maxLines, Formatter formatter); static String print(Collection<?> list, String title); static String print(Object[] array, String title); static String repeat(char c, int count);  }
@Test public void testFormat() { Object[] objects = { new Integer("123"), new Integer("123"), new String[]{"A", "B", "C"}, new BigDecimal("1.23") }; Formatter[] formatters = { null, new Formatter() { @Override public String format(Object obj) { return "#" + obj + "#"; } }, null, null }; String[] expectedResults = { "123", "#123#", "[A, B, C]", "1.23" }; for (int i = 0; i < objects.length; i++) { Object obj = objects[i]; Formatter formatter = formatters[i]; String expectedResult = expectedResults[i]; String result = Printer.format(obj, formatter); assertEquals(expectedResult, result); } }