Skip to content

Commit

Permalink
MoonIllumination.getClosestPhase() returns the closest phase of the enum
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Jun 5, 2021
1 parent a901760 commit f8ba2d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java
Original file line number Diff line number Diff line change
Expand Up @@ -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).
* <p>
* 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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}

}

0 comments on commit f8ba2d1

Please sign in to comment.