From e1660c4b12f8091582f90eb41ff00196aa1dcebc Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 25 Sep 2024 21:06:54 +0200 Subject: [PATCH] Add TwilightCalendar helper class and add a default getter to CacheableTwilightTimeProvider --- .../m724/wtapi/object/TwilightCalendar.java | 58 +++++++++++++++++++ .../CacheableTwilightTimeProvider.java | 5 ++ 2 files changed, 63 insertions(+) create mode 100644 src/main/java/eu/m724/wtapi/object/TwilightCalendar.java diff --git a/src/main/java/eu/m724/wtapi/object/TwilightCalendar.java b/src/main/java/eu/m724/wtapi/object/TwilightCalendar.java new file mode 100644 index 0000000..65c35da --- /dev/null +++ b/src/main/java/eu/m724/wtapi/object/TwilightCalendar.java @@ -0,0 +1,58 @@ +package eu.m724.wtapi.object; + +import eu.m724.wtapi.provider.twilight.CacheableTwilightTimeProvider; +import eu.m724.wtapi.provider.twilight.TwilightTimeCache; +import eu.m724.wtapi.provider.twilight.impl.approximate.ApproximateTwilightTimeProvider; + +import java.time.LocalDate; + +// TODO add tests +public class TwilightCalendar { + private final LocalDate date; + private final CacheableTwilightTimeProvider provider; + private final TwilightTimeCache cache; + + public TwilightCalendar(LocalDate date, CacheableTwilightTimeProvider provider) { + this.date = date; + this.provider = (CacheableTwilightTimeProvider) provider; + this.cache = provider.initializeCache(date); + } + + /** + * Gets the {@link Twilight} for the specified {@link Coordinates} + * + * @param coordinates the coordinates + * @return the {@link Twilight} + */ + public Twilight twilight(Coordinates coordinates) { + return provider.calculateTwilightTime(cache, coordinates); + } + + /** + * Gets a {@link TwilightCalendar} with the same provider for the specified date + * + * @param date the date + * @return a {@link TwilightCalendar} + */ + public TwilightCalendar withDate(LocalDate date) { + return new TwilightCalendar(date, provider); + } + + /** + * Gets a {@link TwilightCalendar} for the day after this {@link TwilightCalendar}'s day + * + * @return a {@link TwilightCalendar} + */ + public TwilightCalendar tomorrow() { + return withDate(date.plusDays(1)); + } + + /** + * Gets a {@link TwilightCalendar} for the day before this {@link TwilightCalendar}'s day + * + * @return a {@link TwilightCalendar} + */ + public TwilightCalendar yesterday() { + return withDate(date.minusDays(1)); + } +} diff --git a/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java b/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java index 649d8d1..5904cc0 100644 --- a/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java +++ b/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java @@ -2,6 +2,7 @@ package eu.m724.wtapi.provider.twilight; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Twilight; +import eu.m724.wtapi.object.TwilightCalendar; import java.time.LocalDate; @@ -37,4 +38,8 @@ public abstract class CacheableTwilightTimeProvider coordinates ); } + + public TwilightCalendar getCalendar(LocalDate date) { + return new TwilightCalendar(date, this); + } }