Add TwilightCalendar helper class and add a default getter to CacheableTwilightTimeProvider

This commit is contained in:
Minecon724 2024-09-25 21:06:54 +02:00
parent 915ff65738
commit e1660c4b12
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 63 additions and 0 deletions

View file

@ -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<TwilightTimeCache> provider;
private final TwilightTimeCache cache;
public TwilightCalendar(LocalDate date, CacheableTwilightTimeProvider<?> provider) {
this.date = date;
this.provider = (CacheableTwilightTimeProvider<TwilightTimeCache>) 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));
}
}

View file

@ -2,6 +2,7 @@ package eu.m724.wtapi.provider.twilight;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Twilight; import eu.m724.wtapi.object.Twilight;
import eu.m724.wtapi.object.TwilightCalendar;
import java.time.LocalDate; import java.time.LocalDate;
@ -37,4 +38,8 @@ public abstract class CacheableTwilightTimeProvider<T extends TwilightTimeCache>
coordinates coordinates
); );
} }
public TwilightCalendar getCalendar(LocalDate date) {
return new TwilightCalendar(date, this);
}
} }