Skip to content

Commit

Permalink
Simplify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jarnaud committed Dec 5, 2020
1 parent d989f01 commit 79bf70c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 9 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/github/jarnaud/republican/RDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/jarnaud/republican/RMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/github/jarnaud/republican/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

}

}
16 changes: 16 additions & 0 deletions src/test/java/com/github/jarnaud/republican/RDateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.*;

public class RDateTest {
Expand All @@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit 79bf70c

Please sign in to comment.