|
|
@@ -2,35 +2,48 @@ package net.ranides.assira.time2;
|
|
|
|
|
|
import net.ranides.assira.collection.sets.AVLTreeSet;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZonedDateTime;
|
|
|
import java.util.Collection;
|
|
|
import java.util.SortedSet;
|
|
|
|
|
|
+import static java.time.LocalDateTime.MAX;
|
|
|
+import static java.time.LocalDateTime.MIN;
|
|
|
+
|
|
|
+// @todo ZonedDateSet
|
|
|
public abstract class ZonedDateSet {
|
|
|
|
|
|
- public abstract boolean contains(ZonedDate date);
|
|
|
+ public boolean contains(ZonedDate date) {
|
|
|
+ return !contains(ZonedRange.of(date)).isZero();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean containsFully(ZonedDate date) {
|
|
|
+ return contains(ZonedRange.of(date)).equals(Duration.ofDays(1));
|
|
|
+ }
|
|
|
|
|
|
public abstract boolean contains(ZonedDateTime datetime);
|
|
|
|
|
|
- public static class EmptyHolidaySchedule extends ZonedDateSet {
|
|
|
+ public abstract Duration contains(ZonedRange range);
|
|
|
+
|
|
|
+ public static class Empty extends ZonedDateSet {
|
|
|
|
|
|
@Override
|
|
|
- public boolean contains(ZonedDate date) {
|
|
|
+ public boolean contains(ZonedDateTime datetime) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean contains(ZonedDateTime datetime) {
|
|
|
- return false;
|
|
|
+ public Duration contains(ZonedRange range) {
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class SimpleHolidaySchedule extends ZonedDateSet {
|
|
|
+ public static class Simple extends ZonedDateSet {
|
|
|
|
|
|
private final SortedSet<ZonedDateTime> set;
|
|
|
|
|
|
- public SimpleHolidaySchedule(Collection<ZonedDate> input) {
|
|
|
+ public Simple(Collection<ZonedDate> input) {
|
|
|
this.set = new AVLTreeSet<>();
|
|
|
for(ZonedDate item : input) {
|
|
|
set.add(DateTimeUtils.withZoneUtc(item.withZero()));
|
|
|
@@ -39,31 +52,27 @@ public abstract class ZonedDateSet {
|
|
|
|
|
|
@ZoneHazard
|
|
|
@Override
|
|
|
- public boolean contains(ZonedDate date) {
|
|
|
- ZonedDateTime lower = DateTimeUtils.withZoneUtc(date.withZero());
|
|
|
- ZonedDateTime upper = lower.plusDays(1);
|
|
|
+ public Duration contains(ZonedRange range) {
|
|
|
+ ZonedDateTime lower = range.begin();
|
|
|
+ ZonedDateTime upper = range.end();
|
|
|
|
|
|
- for(ZonedDateTime mid : set.tailSet(lower.with(LocalDateTime.MIN))) {
|
|
|
+ for(ZonedDateTime mid : set.subSet(lower.with(MIN), upper.with(MAX))) {
|
|
|
if(mid.isEqual(lower) || (mid.isBefore(upper) && mid.plusDays(1).isAfter(lower))) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- if(mid.plusDays(1).isAfter(upper)) {
|
|
|
- return false;
|
|
|
+ return ZonedRange.of(mid, mid.plusDays(1)).intersection(range).duration();
|
|
|
}
|
|
|
}
|
|
|
- return false;
|
|
|
+ return Duration.ZERO;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean contains(ZonedDateTime datetime) {
|
|
|
// @todo test, may not work at all
|
|
|
- for(ZonedDateTime mid : set.tailSet(datetime.minusDays(1))) {
|
|
|
+ ZonedDateTime lowerMin = datetime.minusDays(1);
|
|
|
+ ZonedDateTime upperMax = datetime.plusDays(1);
|
|
|
+ for(ZonedDateTime mid : set.subSet(lowerMin, upperMax)) {
|
|
|
if(mid.isEqual(datetime) || (mid.isBefore(datetime) && mid.plusDays(1).isAfter(datetime))) {
|
|
|
return true;
|
|
|
}
|
|
|
- if(mid.plusDays(1).isAfter(datetime)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
}
|
|
|
return false;
|
|
|
}
|