From 10afbf782c1cc7bfce007e81ad00d665414dc2f5 Mon Sep 17 00:00:00 2001 From: Idevaldo De Lira Date: Thu, 8 Aug 2024 15:57:55 +0200 Subject: [PATCH 1/2] Adds date comparison methods --- .../java/sirius/kernel/commons/Dates.java | 60 ++++++++++++++----- .../kotlin/sirius/kernel/commons/DatesTest.kt | 36 ++++++++++- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/main/java/sirius/kernel/commons/Dates.java b/src/main/java/sirius/kernel/commons/Dates.java index 306d13b0..e47c4665 100644 --- a/src/main/java/sirius/kernel/commons/Dates.java +++ b/src/main/java/sirius/kernel/commons/Dates.java @@ -8,6 +8,9 @@ package sirius.kernel.commons; +import com.google.common.collect.Range; + +import javax.annotation.Nonnull; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Arrays; @@ -42,10 +45,7 @@ public static LocalDateTime computeLatestDateTime(LocalDateTime... dateTimes) { * @return the latest date-time object or null if no date is given */ public static LocalDateTime computeLatestDateTime(List dateTimes) { - return dateTimes.stream() - .filter(Objects::nonNull) - .max(LocalDateTime::compareTo) - .orElse(null); + return dateTimes.stream().filter(Objects::nonNull).max(LocalDateTime::compareTo).orElse(null); } /** @@ -60,14 +60,12 @@ public static LocalDate computeLatestDate(LocalDate... dates) { /** * Computes the latest date of the given date objects. + * * @param dates a list of date objects to compare * @return the latest date object or null if no date is given */ public static LocalDate computeLatestDate(List dates) { - return dates.stream() - .filter(Objects::nonNull) - .max(LocalDate::compareTo) - .orElse(null); + return dates.stream().filter(Objects::nonNull).max(LocalDate::compareTo).orElse(null); } /** @@ -87,10 +85,7 @@ public static LocalDateTime computeEarliestDateTime(LocalDateTime... dateTimes) * @return the earliest date-time object or null if no date is given */ public static LocalDateTime computeEarliestDateTime(List dateTimes) { - return dateTimes.stream() - .filter(Objects::nonNull) - .min(LocalDateTime::compareTo) - .orElse(null); + return dateTimes.stream().filter(Objects::nonNull).min(LocalDateTime::compareTo).orElse(null); } /** @@ -110,9 +105,42 @@ public static LocalDate computeEarliestDate(LocalDate... dates) { * @return the earliest date object or null if no date is given */ public static LocalDate computeEarliestDate(List dates) { - return dates.stream() - .filter(Objects::nonNull) - .min(LocalDate::compareTo) - .orElse(null); + return dates.stream().filter(Objects::nonNull).min(LocalDate::compareTo).orElse(null); + } + + /** + * Determines if the given date is before or after the given reference date. + * + * @param dateToCheck the date to check + * @param referenceDate the reference date + * @return true if the date is before or after the reference date, false otherwise + */ + public static boolean isBeforeOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) { + return Range.atMost(referenceDate).contains(dateToCheck); + } + + /** + * Determines if the given date is after or equal to the given reference date. + * + * @param dateToCheck the date to check + * @param referenceDate the reference date + * @return true if the date is after or equal to the reference date, false otherwise + */ + public static boolean isAfterOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) { + return Range.atLeast(referenceDate).contains(dateToCheck); + } + + /** + * Determines if the given date is within the given range. + * + * @param startDate the range's start date + * @param endDate the range's end date + * @param dateToCheck the date to check + * @return true if the date is within the range, false otherwise + */ + public static boolean isWithinRange(@Nonnull LocalDate startDate, + @Nonnull LocalDate endDate, + @Nonnull LocalDate dateToCheck) { + return Range.closed(startDate, endDate).contains(dateToCheck); } } diff --git a/src/test/kotlin/sirius/kernel/commons/DatesTest.kt b/src/test/kotlin/sirius/kernel/commons/DatesTest.kt index 4851e2a7..7aa05f40 100644 --- a/src/test/kotlin/sirius/kernel/commons/DatesTest.kt +++ b/src/test/kotlin/sirius/kernel/commons/DatesTest.kt @@ -10,7 +10,10 @@ package sirius.kernel.commons import java.time.LocalDate import java.time.LocalDateTime -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue /** * Tests the [Dates] class. @@ -51,4 +54,35 @@ class DatesTest { assertEquals(theDayBeforeYesterday, Dates.computeEarliestDate(null, theDayBeforeYesterday, now, yesterday)) assertEquals(null, Dates.computeEarliestDate(null, null, null)) } + + @Test + fun isBeforeOrEqual() { + val theDayBeforeYesterday = LocalDate.now().minusDays(2) + val yesterday = LocalDate.now().minusDays(1) + val now = LocalDate.now() + assertTrue(Dates.isBeforeOrEqual(theDayBeforeYesterday, yesterday)) + assertTrue(Dates.isBeforeOrEqual(yesterday, yesterday)) + assertFalse(Dates.isBeforeOrEqual(now, yesterday)) + } + + @Test + fun isAfterOrEqual() { + val theDayBeforeYesterday = LocalDate.now().minusDays(2) + val yesterday = LocalDate.now().minusDays(1) + val now = LocalDate.now() + assertTrue(Dates.isAfterOrEqual(now, yesterday)) + assertTrue(Dates.isAfterOrEqual(yesterday, yesterday)) + assertFalse(Dates.isAfterOrEqual(theDayBeforeYesterday, yesterday)) + } + + @Test + fun isWithinRange() { + val theDayBeforeYesterday = LocalDate.now().minusDays(2) + val yesterday = LocalDate.now().minusDays(1) + val now = LocalDate.now() + assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, yesterday)) + assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, theDayBeforeYesterday)) + assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, now)) + assertFalse(Dates.isWithinRange(theDayBeforeYesterday, yesterday, now)) + } } From 3c49d4c8a562c14d519a06d4ca581d70519b6277 Mon Sep 17 00:00:00 2001 From: Idevaldo De Lira Date: Fri, 9 Aug 2024 08:35:04 +0200 Subject: [PATCH 2/2] Replaces Range by vanilla LocalDate calls --- src/main/java/sirius/kernel/commons/Dates.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/sirius/kernel/commons/Dates.java b/src/main/java/sirius/kernel/commons/Dates.java index e47c4665..ff228c2d 100644 --- a/src/main/java/sirius/kernel/commons/Dates.java +++ b/src/main/java/sirius/kernel/commons/Dates.java @@ -8,8 +8,6 @@ package sirius.kernel.commons; -import com.google.common.collect.Range; - import javax.annotation.Nonnull; import java.time.LocalDate; import java.time.LocalDateTime; @@ -116,7 +114,7 @@ public static LocalDate computeEarliestDate(List dates) { * @return true if the date is before or after the reference date, false otherwise */ public static boolean isBeforeOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) { - return Range.atMost(referenceDate).contains(dateToCheck); + return dateToCheck.isBefore(referenceDate) || dateToCheck.equals(referenceDate); } /** @@ -127,11 +125,11 @@ public static boolean isBeforeOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull L * @return true if the date is after or equal to the reference date, false otherwise */ public static boolean isAfterOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) { - return Range.atLeast(referenceDate).contains(dateToCheck); + return dateToCheck.isAfter(referenceDate) || dateToCheck.equals(referenceDate); } /** - * Determines if the given date is within the given range. + * Determines if the given date is within the given range, inclusive the range dates. * * @param startDate the range's start date * @param endDate the range's end date @@ -141,6 +139,6 @@ public static boolean isAfterOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull Lo public static boolean isWithinRange(@Nonnull LocalDate startDate, @Nonnull LocalDate endDate, @Nonnull LocalDate dateToCheck) { - return Range.closed(startDate, endDate).contains(dateToCheck); + return isAfterOrEqual(dateToCheck, startDate) && isBeforeOrEqual(dateToCheck, endDate); } }