From 79bf70c71d38163f04d6b5bd3eae5105fcc96c92 Mon Sep 17 00:00:00 2001 From: Jean Arnaud Date: Sat, 5 Dec 2020 09:19:37 +0100 Subject: [PATCH] Simplify usage --- README.md | 8 ++--- .../jarnaud/republican/GRConverter.java | 2 +- .../republican/LeapYearCalculator.java | 2 +- .../com/github/jarnaud/republican/RDate.java | 35 +++++++++++++++++++ .../jarnaud/republican/RGConverter.java | 2 +- .../com/github/jarnaud/republican/RMonth.java | 2 +- .../jarnaud/republican/ConverterTest.java | 13 +++++++ .../github/jarnaud/republican/RDateTest.java | 16 +++++++++ 8 files changed, 71 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ba5b61b..1ad1295 100644 --- a/README.md +++ b/README.md @@ -36,20 +36,18 @@ NB: To find the latest version, please refer to [Maven search](https://search.ma ### Quickstart -- To convert a Gregorian (usual) date into a Republican date: +- To convert a Gregorian local date into a Republican date: ```java -GRConverter c = new GRConverter(); LocalDate date = LocalDate.of(1792, 9, 22); -RDate rDate = c.convert(date); +RDate rDate = RDate.of(localDate); ``` - To convert a Republican date into a Gregorian date: ```java -RGConverter c = new RGConverter(); RDate rDate = RDate.of(1, RMonth.Vendemiaire, 1); -LocalDate date = c.convert(rDate); +LocalDate date = c.toLocalDate(); ``` - `RDate` represents a Republican date and provides some utility methods: diff --git a/src/main/java/com/github/jarnaud/republican/GRConverter.java b/src/main/java/com/github/jarnaud/republican/GRConverter.java index 411ef4f..cb18e41 100644 --- a/src/main/java/com/github/jarnaud/republican/GRConverter.java +++ b/src/main/java/com/github/jarnaud/republican/GRConverter.java @@ -7,7 +7,7 @@ /** * Converter from Gregorian to Republican dates. */ -public class GRConverter { +class GRConverter { /** * Officially the Republican calendar started on 1792-09-22 (An I Vendemiaire 1). diff --git a/src/main/java/com/github/jarnaud/republican/LeapYearCalculator.java b/src/main/java/com/github/jarnaud/republican/LeapYearCalculator.java index fcaaeba..33fb117 100644 --- a/src/main/java/com/github/jarnaud/republican/LeapYearCalculator.java +++ b/src/main/java/com/github/jarnaud/republican/LeapYearCalculator.java @@ -3,7 +3,7 @@ /** * Process the number of leap years between the start of the Republican calendar and a given year. */ -public class LeapYearCalculator { +class LeapYearCalculator { private static final int START_REPUBLICAN_YEAR = 1792; diff --git a/src/main/java/com/github/jarnaud/republican/RDate.java b/src/main/java/com/github/jarnaud/republican/RDate.java index 85075dd..63c0e36 100644 --- a/src/main/java/com/github/jarnaud/republican/RDate.java +++ b/src/main/java/com/github/jarnaud/republican/RDate.java @@ -70,10 +70,31 @@ public static RDate of(int year, int month, int day) { return new RDate(year, RMonth.values()[month - 1], day); } + /** + * Construct a new Republican date from a Gregorian date. + * Throws a RuntimeException if the given date is invalid (ie. before first day of Republican calendar). + * + * @param date the Gregorian date. + * @return the Republican date. + */ + public static RDate of(LocalDate date) { + return new GRConverter().convert(date); + } + + /** + * Return the Republican year for this date. + * + * @return the year. + */ public int getYear() { return year; } + /** + * Return the Republican month for this date. + * + * @return the month. + */ public RMonth getMonth() { return month; } @@ -90,6 +111,11 @@ public int getDecade() { return decade; } + /** + * Return the day of the month of this date. + * + * @return the day of the month. + */ public int getDay() { return day; } @@ -132,6 +158,15 @@ public int hashCode() { return Objects.hash(year, month, decade, day); } + /** + * Convert this Republican date into a Gregorian local date. + * + * @return the local date. + */ + public LocalDate toLocalDate() { + return new RGConverter().convert(this); + } + /** * Return true if the given date is strictly before this date. * diff --git a/src/main/java/com/github/jarnaud/republican/RGConverter.java b/src/main/java/com/github/jarnaud/republican/RGConverter.java index 75d0141..d2602c5 100644 --- a/src/main/java/com/github/jarnaud/republican/RGConverter.java +++ b/src/main/java/com/github/jarnaud/republican/RGConverter.java @@ -6,7 +6,7 @@ /** * Converter from Republican to Gregorian dates. */ -public class RGConverter { +class RGConverter { /** * Officially the Republican calendar started on 1792-09-22 (An I Vendemiaire 1). diff --git a/src/main/java/com/github/jarnaud/republican/RMonth.java b/src/main/java/com/github/jarnaud/republican/RMonth.java index 9033358..f095c60 100644 --- a/src/main/java/com/github/jarnaud/republican/RMonth.java +++ b/src/main/java/com/github/jarnaud/republican/RMonth.java @@ -39,7 +39,7 @@ public enum RMonth { /** * Return the month number (eg. 1 for Vendemiaire, 12 for Fructidor, 13 for Sanculottide...) * - * @return the month number. + * @return the month number, between 1 and 13. */ public int getMonth() { return month; diff --git a/src/test/java/com/github/jarnaud/republican/ConverterTest.java b/src/test/java/com/github/jarnaud/republican/ConverterTest.java index 86a4eb9..732d466 100644 --- a/src/test/java/com/github/jarnaud/republican/ConverterTest.java +++ b/src/test/java/com/github/jarnaud/republican/ConverterTest.java @@ -183,6 +183,19 @@ private void compare(int rYear, RMonth rMonth, int rDay, int gYear, int gMonth, LocalDate.of(gYear, gMonth, gDay), rgConverter.convert(RDate.of(rYear, rMonth, rDay)) ); + + // Test RDate methods: + + assertEquals( + RDate.of(rYear, rMonth, rDay), + RDate.of(LocalDate.of(gYear, gMonth, gDay)) + ); + + assertEquals( + LocalDate.of(gYear, gMonth, gDay), + RDate.of(rYear, rMonth, rDay).toLocalDate() + ); + } } diff --git a/src/test/java/com/github/jarnaud/republican/RDateTest.java b/src/test/java/com/github/jarnaud/republican/RDateTest.java index eb7c87e..7afe45c 100644 --- a/src/test/java/com/github/jarnaud/republican/RDateTest.java +++ b/src/test/java/com/github/jarnaud/republican/RDateTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; +import java.time.LocalDate; + import static org.junit.jupiter.api.Assertions.*; public class RDateTest { @@ -21,6 +23,12 @@ public void testOf2() { assertEquals(RDate.of(58, RMonth.Floreal, 1), RDate.of(58, 8, 1)); } + @Test + public void testOf3() { + assertThrows(RuntimeException.class, () -> RDate.of(LocalDate.of(1650, 1, 1))); + // Normal cases are handled by the converter test. + } + @Test public void testDecade() { assertEquals(1, RDate.of(6, RMonth.Floreal, 1).getDecade()); @@ -34,6 +42,14 @@ public void testDecade() { assertEquals(3, RDate.of(6, RMonth.Floreal, 30).getDecade()); } + @Test + public void testEquals_symmetric() { + RDate rd1 = RDate.of(12, RMonth.Brumaire, 18); + RDate rd2 = RDate.of(12, 2, 18); + assertEquals(rd1, rd2); + assertEquals(rd2.hashCode(), rd1.hashCode()); + } + @Test public void testIsBefore() { RDate d1 = RDate.of(6, RMonth.Floreal, 4);