From 7930e4dd0a44ce020b6eecd5f934739dc7ddace6 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 24 Sep 2024 18:57:55 +0200 Subject: [PATCH] Add cache for time provider The improvement is surely negligible, perhaps harming performance, so more of a gimmick --- .../CacheableTwilightTimeProvider.java | 40 +++++++++++++++++++ .../provider/twilight/TwilightTimeCache.java | 4 ++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java create mode 100644 src/main/java/eu/m724/wtapi/provider/twilight/TwilightTimeCache.java diff --git a/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java b/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java new file mode 100644 index 0000000..649d8d1 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/twilight/CacheableTwilightTimeProvider.java @@ -0,0 +1,40 @@ +package eu.m724.wtapi.provider.twilight; + +import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.object.Twilight; + +import java.time.LocalDate; + +// TODO reconsider if this is necessary and doesn't actually harm performance +/** + * A {@link TwilightTimeProvider} that can cache some calculations to be faster + * + * @param An implementation of {@link TwilightTimeCache} where you store whatever you need + */ +public abstract class CacheableTwilightTimeProvider extends TwilightTimeProvider { + /** + * Calculates sunrise and sunset for provided coordinates + * + * @param cache {@link T}, implementation of {@link TwilightTimeCache} + * @param coordinates Coordinates of the observer + * @return {@link Twilight} + * @see #initializeCache(LocalDate) + */ + public abstract Twilight calculateTwilightTime(T cache, Coordinates coordinates); + + /** + * Initializes cache for provided date + * + * @param date The UTC date + * @return ready to use cache + */ + public abstract T initializeCache(LocalDate date); + + @Override + public Twilight calculateTwilightTime(LocalDate date, Coordinates coordinates) { + return calculateTwilightTime( + initializeCache(date), + coordinates + ); + } +} diff --git a/src/main/java/eu/m724/wtapi/provider/twilight/TwilightTimeCache.java b/src/main/java/eu/m724/wtapi/provider/twilight/TwilightTimeCache.java new file mode 100644 index 0000000..fe6304c --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/twilight/TwilightTimeCache.java @@ -0,0 +1,4 @@ +package eu.m724.wtapi.provider.twilight; + +public interface TwilightTimeCache { +}