EventListConverter implements Function<List<Event>, List<EventItem>> { @Override public List<EventItem> apply(List<Event> events) { if (events.isEmpty()) { return Collections.emptyList(); } return events.stream() .collect(Collectors.groupingBy(event -> event.getStart().toLocalDate())) .entrySet() .stream() .flatMap(this::convert) .sorted() .collect(Collectors.toList()); }  @Override List<EventItem> apply(List<Event> events);  }
@Test public void testConversion() { List<EventItem> result = new EventListConverter().apply(data); assertTrue(result.size() > data.size()); boolean previousWasHeader = false; boolean previousWasLastOfSection = true; boolean previousWasFirstOfSection = false; for (EventItem item : result) { if (previousWasLastOfSection) { assertTrue(item.isHeader()); previousWasHeader = true; previousWasLastOfSection = false; } else if (previousWasHeader) { previousWasHeader = false; previousWasFirstOfSection = true; } else if (previousWasFirstOfSection) { previousWasFirstOfSection = false; } else if (item.isItem()) { previousWasLastOfSection = item.isLastOfSection(); } } assertTrue(result.get(result.size() - 1).isLastOfSection()); List<LocalDate> dates = result.stream() .filter(EventItem::isHeader) .map(EventItem::getHeader) .collect(Collectors.toList()); List<LocalDate> sorted = new ArrayList<>(dates); Collections.sort(sorted); assertEquals(sorted, dates); }
@Test public void testEmpty() { EventListConverter converter = new EventListConverter(); List<Event> empty = Collections.emptyList(); List<EventItem> result = converter.apply(empty); assertTrue(result.isEmpty()); }