FormatUtils { public static String formatEntityCollection(EntityModelFactory entityModelFactory, AttributeModel attributeModel, Object collection, String separator) { List<String> result = new ArrayList<>(); Iterable<?> col = (Iterable<?>) collection; for (Object next : col) { if (next instanceof AbstractEntity) { EntityModel<?> entityModel = entityModelFactory.getModel(next.getClass()); String displayProperty = entityModel.getDisplayProperty(); if (displayProperty != null) { result.add(ClassUtils.getFieldValueAsString(next, displayProperty)); } else { result.add(next.toString()); } } else if (next instanceof Number) { result.add( VaadinUtils.numberToString(attributeModel, next, true, VaadinUtils.getLocale(), VaadinUtils.getCurrencySymbol())); } else { result.add(next.toString()); } } return result.stream().collect(Collectors.joining(separator)); } private  FormatUtils(); static String extractAndFormat(Grid<T> grid, AttributeModel am, Object obj); static String formatEntity(EntityModel<?> entityModel, Object value); static String formatEntityCollection(EntityModelFactory entityModelFactory, AttributeModel attributeModel, Object collection,
            String separator); static String formatPropertyValue(EntityModelFactory entityModelFactory, AttributeModel model, Object value, String separator); @SuppressWarnings("unchecked") static String formatPropertyValue(Grid<T> grid, EntityModelFactory entityModelFactory, AttributeModel model, Object value,
            Locale locale, String separator);  }
@Test public void testFormatEntityCollection() { Entity1 t1 = new Entity1(); t1.setId(1); t1.setName("a1"); Entity1 t2 = new Entity1(); t2.setId(2); t2.setName("a2"); Entity1 t3 = new Entity1(); t3.setId(3); t3.setName("a3"); Entity3 e3 = new Entity3(); e3.setEntities(Sets.newLinkedHashSet(t1, t2, t3)); String result = FormatUtils.formatEntityCollection(factory, null, e3.getEntities(), ", "); assertEquals("a1, a2, a3", result); }
@Test public void testFormatEntityCollection_ElementCollection() { Entity4 e = new Entity4(); e.setDecimals(Set.of(BigDecimal.valueOf(5), BigDecimal.valueOf(6))); String result = FormatUtils.formatEntityCollection(factory, factory.getModel(Entity4.class).getAttributeModel("decimals"), e.getDecimals(), ","); assertTrue(result.contains("5,00")); assertTrue(result.contains("6,00")); }
@Test public void testFormatEntityCollection_ElementCollectionPercentage() { Entity4 e = new Entity4(); e.setDecimalPercentages(Set.of(BigDecimal.valueOf(5), BigDecimal.valueOf(6))); String result = FormatUtils.formatEntityCollection(factory, factory.getModel(Entity4.class).getAttributeModel("decimalPercentages"), e.getDecimalPercentages(), ","); assertTrue(result.contains("5,00%")); assertTrue(result.contains("6,00%")); }