From f8ba2d1bc871548b3c0fe323b77847b73e761110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Thu, 3 Jun 2021 19:28:32 +0200 Subject: [PATCH] MoonIllumination.getClosestPhase() returns the closest phase of the enum --- .../shredzone/commons/suncalc/MoonIllumination.java | 13 +++++++++++++ .../commons/suncalc/MoonIlluminationTest.java | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java b/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java index 31fcfc5..fc1d9e8 100644 --- a/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java +++ b/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java @@ -88,6 +88,9 @@ public double getFraction() { /** * Moon phase. Starts at {@code -180.0} (new moon, waxing), passes {@code 0.0} (full * moon) and moves toward {@code 180.0} (waning, new moon). + *

+ * Note that for historical reasons, the range of this phase is different to the + * moon phase angle used in {@link MoonPhase}. */ public double getPhase() { return phase; @@ -106,6 +109,16 @@ public double getAngle() { return angle; } + /** + * The closest {@link MoonPhase.Phase} that is matching the moon's angle. + * + * @return Closest {@link MoonPhase.Phase} + * @since 2.12 + */ + public MoonPhase.Phase getClosestPhase() { + return MoonPhase.Phase.toPhase(phase + 180.0); + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java index 5946260..96eb5bb 100644 --- a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java @@ -18,6 +18,7 @@ import org.assertj.core.data.Offset; import org.junit.Test; +import org.shredzone.commons.suncalc.MoonPhase.Phase; /** * Unit tests for {@link MoonIllumination}. @@ -35,6 +36,7 @@ public void testNewMoon() { assertThat(mi.getFraction()).as("fraction").isCloseTo(0.0, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(176.0, ERROR); // -180.0 assertThat(mi.getAngle()).as("angle").isCloseTo(1.8, ERROR); + assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.NEW_MOON); } @Test @@ -46,6 +48,7 @@ public void testWaxingHalfMoon() { assertThat(mi.getFraction()).as("fraction").isCloseTo(0.5, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(-90.0, ERROR); assertThat(mi.getAngle()).as("angle").isCloseTo(-66.9, ERROR); + assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.FIRST_QUARTER); } @Test @@ -57,6 +60,7 @@ public void testFullMoon() { assertThat(mi.getFraction()).as("fraction").isCloseTo(1.0, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(-3.2, ERROR); // 0.0 assertThat(mi.getAngle()).as("angle").isCloseTo(-7.0, ERROR); + assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.FULL_MOON); } @Test @@ -68,6 +72,7 @@ public void testWaningHalfMoon() { assertThat(mi.getFraction()).as("fraction").isCloseTo(0.5, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(90.0, ERROR); assertThat(mi.getAngle()).as("angle").isCloseTo(68.1, ERROR); + assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.LAST_QUARTER); } }