Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve comparison and overlap of the temporal API #411

Merged
merged 9 commits into from
Jul 20, 2022
Prev Previous commit
Next Next commit
Add overlapStatus(x, y) to TemporalInterval instances.
Signed-off-by: Daniel Danis <daniel.gordon.danis@protonmail.com>
  • Loading branch information
ielis committed Jul 18, 2022
commit 7a9682b770cafec45e07daea70234d171dfaa45d
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,31 @@ default TemporalInterval intersection(TemporalInterval other) {
return TemporalInterval.of(start, end);
}

/**
* @return {@code true} if {@code this} and {@code other} overlap.
*/
default boolean overlapsWith(TemporalInterval other) {
// TODO - calculate without allocating
return !intersection(other).isEmpty();
switch (overlapStatus(other)) {
case UPSTREAM:
case DOWNSTREAM:
return false;
case OVERLAPS_UPSTREAM:
case CONTAINS:
case CONTAINED_IN:
case OVERLAPS_DOWNSTREAM:
return true;
default:
throw new RuntimeException(String.format("Unknown item %s", overlapStatus(other)));
}
}

/**
* Get {@link OverlapStatus} for {@link TemporalInterval}s {@code this} and {@code other}.
* <p>
* Note: the method returns {@link OverlapStatus#CONTAINED_IN} if {@code this} and {@code other} are equal.
*/
default OverlapStatus overlapStatus(TemporalInterval other) {
return overlapStatus(this, other);
}

default boolean contains(PointInTime age) {
Expand All @@ -150,7 +172,7 @@ static int compare(TemporalInterval x, TemporalInterval y) {
/**
* Get {@link OverlapStatus} for {@link TemporalInterval}s {@code x} and {@code y}.
* <p>
* Note: the method returns {@link OverlapStatus#CONTAINED_IN} if {@code x} and {@code y} are equal.
* Note: the method returns {@link OverlapStatus#CONTAINED_IN} if {@code x} and {@code y} are equal.
*/
static OverlapStatus overlapStatus(TemporalInterval x, TemporalInterval y) {
if (x.end().isAtOrBefore(y.start())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void overlapStatus(int leftStartDays, int leftEndDays,
TemporalInterval right = TemporalInterval.of(rightStart, rightEnd);

assertThat(TemporalInterval.overlapStatus(left, right), equalTo(expected));
assertThat(left.overlapStatus(right), equalTo(expected));
}

}