From 2e126e794c1880734a7917bd34f12ff07fd0cb44 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Sun, 6 Nov 2022 12:22:39 +0100 Subject: [PATCH] BAEL-5925 Code for the finding max date in object list article --- .../baeldung/streams/maxdate/DateHelper.java | 50 ++++++++ .../com/baeldung/streams/maxdate/Event.java | 17 +++ .../baeldung/streams/maxdate/LocalEvent.java | 16 +++ .../streams/maxdate/DateHelperUnitTest.java | 109 ++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/DateHelper.java create mode 100644 core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/Event.java create mode 100644 core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/LocalEvent.java create mode 100644 core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streams/maxdate/DateHelperUnitTest.java diff --git a/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/DateHelper.java b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/DateHelper.java new file mode 100644 index 0000000000..6a116d76ae --- /dev/null +++ b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/DateHelper.java @@ -0,0 +1,50 @@ +package com.baeldung.streams.maxdate; + +import java.time.LocalDate; +import java.util.Comparator; +import java.util.Date; +import java.util.List; + +public class DateHelper { + + static final Date findMaxDateOf(List events) { + if (events == null || events.isEmpty()) { + return null; + } + return events.stream() + .map(Event::getDate) + .max(Date::compareTo) + .get(); + } + + static final Date findMaxDateOfWithComparator(List events) { + if (events == null || events.isEmpty()) { + return null; + } + return events.stream() + .map(Event::getDate) + .max(Comparator.naturalOrder()) + .get(); + } + + static final LocalDate findMaxDateOfLocalEvents(List events) { + if (events == null || events.isEmpty()) { + return null; + } + return events.stream() + .map(LocalEvent::getDate) + .max(LocalDate::compareTo) + .get(); + } + + static final LocalDate findMaxDateOfLocalEventsWithComparator(List events) { + if (events == null || events.isEmpty()) { + return null; + } + return events.stream() + .map(LocalEvent::getDate) + .max(Comparator.naturalOrder()) + .get(); + } + +} diff --git a/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/Event.java b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/Event.java new file mode 100644 index 0000000000..e4e8c4fc7d --- /dev/null +++ b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/Event.java @@ -0,0 +1,17 @@ +package com.baeldung.streams.maxdate; + +import java.util.Date; + +public class Event { + + Date date; + + public Event(Date date) { + this.date = date; + } + + public Date getDate() { + return date; + } + +} diff --git a/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/LocalEvent.java b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/LocalEvent.java new file mode 100644 index 0000000000..aa1f4c1394 --- /dev/null +++ b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/maxdate/LocalEvent.java @@ -0,0 +1,16 @@ +package com.baeldung.streams.maxdate; + +import java.time.LocalDate; + +public class LocalEvent { + + LocalDate date; + + public LocalEvent(LocalDate date) { + this.date = date; + } + + public LocalDate getDate() { + return date; + } +} diff --git a/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streams/maxdate/DateHelperUnitTest.java b/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streams/maxdate/DateHelperUnitTest.java new file mode 100644 index 0000000000..acef1d993b --- /dev/null +++ b/core-java-modules/core-java-streams-4/src/test/java/com/baeldung/streams/maxdate/DateHelperUnitTest.java @@ -0,0 +1,109 @@ +package com.baeldung.streams.maxdate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.time.LocalDate; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang3.time.DateUtils; +import org.junit.jupiter.api.Test; + +class DateHelperUnitTest { + + Date TODAY = new Date(); + Event TODAYS_EVENT = new Event(TODAY); + Date TOMORROW = DateUtils.addDays(TODAY, 1); + Event TOMORROWS_EVENT = new Event(TOMORROW); + Date NEXT_WEEK = DateUtils.addDays(TODAY, 7); + Event NEXT_WEEK_EVENT = new Event(NEXT_WEEK); + + LocalDate TODAY_LOCAL = LocalDate.now(); + LocalEvent TODAY_LOCAL_EVENT = new LocalEvent(TODAY_LOCAL); + LocalDate TOMORROW_LOCAL = TODAY_LOCAL.plusDays(1); + LocalEvent TOMORROW_LOCAL_EVENT = new LocalEvent(TOMORROW_LOCAL); + LocalDate NEXT_WEEK_LOCAL = TODAY_LOCAL.plusWeeks(1); + LocalEvent NEXT_WEEK_LOCAL_EVENT = new LocalEvent(NEXT_WEEK_LOCAL); + + @Test + void givenNullEventList_WhenFindMaxDateOf_ThenNull() { + assertNull(DateHelper.findMaxDateOf(null)); + } + + @Test + void givenEmptyEventList_WhenFindMaxDateOf_ThenNull() { + assertNull(DateHelper.findMaxDateOf(List.of())); + } + + @Test + void givenSingleElementEventList_WhenFindMaxDateOf_ThenReturnElementDate() { + assertEquals(TODAY, DateHelper.findMaxDateOf(List.of(TODAYS_EVENT))); + } + + @Test + void givenEventList_WhenFindMaxDateOf_ThenReturnMaxDate() { + assertEquals(NEXT_WEEK, DateHelper.findMaxDateOf(List.of(TODAYS_EVENT, TOMORROWS_EVENT, NEXT_WEEK_EVENT))); + } + + @Test + void givenNullEventList_WhenFindMaxDateOfWithComparator_ThenNull() { + assertNull(DateHelper.findMaxDateOfWithComparator(null)); + } + + @Test + void givenEmptyEventList_WhenFindMaxDateOfWithComparator_ThenNull() { + assertNull(DateHelper.findMaxDateOfWithComparator(List.of())); + } + + @Test + void givenSingleElementEventList_WhenFindMaxDateOfWithComparator_ThenReturnElementDate() { + assertEquals(TODAY, DateHelper.findMaxDateOfWithComparator(List.of(TODAYS_EVENT))); + } + + @Test + void givenEventList_WhenFindMaxDateOfWithComparator_ThenReturnMaxDate() { + assertEquals(NEXT_WEEK, DateHelper.findMaxDateOfWithComparator(List.of(TODAYS_EVENT, TOMORROWS_EVENT, NEXT_WEEK_EVENT))); + } + + @Test + void givenNullLocalEventList_WhenFindMaxDateOfLocalEvents_ThenNull() { + assertNull(DateHelper.findMaxDateOfLocalEvents(null)); + } + + @Test + void givenEmptyLocalEventList_WhenFindMaxDateOfLocalEvents_ThenNull() { + assertNull(DateHelper.findMaxDateOfLocalEvents(List.of())); + } + + @Test + void givenSingleElementLocalEventList_WhenFindMaxDateOfLocalEvents_ThenReturnElementDate() { + assertEquals(TODAY_LOCAL, DateHelper.findMaxDateOfLocalEvents(List.of(TODAY_LOCAL_EVENT))); + } + + @Test + void givenLocalEventList_WhenFindMaxDateOfLocalEvents_ThenReturnMaxDate() { + assertEquals(NEXT_WEEK_LOCAL, DateHelper.findMaxDateOfLocalEvents(List.of(TODAY_LOCAL_EVENT, TOMORROW_LOCAL_EVENT, NEXT_WEEK_LOCAL_EVENT))); + } + + @Test + void givenNullLocalEventList_WhenFindMaxDateOfLocalEventsWithComparator_ThenNull() { + assertNull(DateHelper.findMaxDateOfLocalEventsWithComparator(null)); + } + + @Test + void givenEmptyLocalEventList_WhenFindMaxDateOfLocalEventsWithComparator_ThenNull() { + assertNull(DateHelper.findMaxDateOfLocalEventsWithComparator(List.of())); + } + + @Test + void givenSingleElementLocalEventList_WhenFindMaxDateOfLocalEventsWithComparator_ThenReturnElementDate() { + assertEquals(TODAY_LOCAL, DateHelper.findMaxDateOfLocalEventsWithComparator(List.of(TODAY_LOCAL_EVENT))); + } + + @Test + void givenLocalEventList_WhenFindMaxDateOfLocalEventsWithComparator_ThenReturnMaxDate() { + assertEquals(NEXT_WEEK_LOCAL, DateHelper.findMaxDateOfLocalEventsWithComparator(List.of(TODAY_LOCAL_EVENT, TOMORROW_LOCAL_EVENT, NEXT_WEEK_LOCAL_EVENT))); + } + +}