Skip to content

Commit

Permalink
Utility to find first discontinuity in timeline (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen authored Jun 2, 2022
1 parent 393dad7 commit 28273b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,23 +476,32 @@ public <T, R> LocalDateTimeline<R> union(LocalDateTimeline<T> other, final Local
/**
* Whether this timeline is continuous and not empty for its entire interval.
*
* @return true if continuous and not empty for whole matchInterval
* @return true if continuous
*/
public boolean isContinuous() {
return firstDiscontinuity() == null;
}

/**
* Return the first interval, if any, not covered by the timeline. Utility when full disjoint is not needed
*
* @return null if continuous, first interval
*/
public LocalDateInterval firstDiscontinuity() {
if (segments.size() == 1) {
return true;
return null;
}

LocalDateInterval lastInterval = null;
for (LocalDateSegment<V> entry : segments) {
if (lastInterval != null) {
if (!lastInterval.abuts(entry.getLocalDateInterval())) {
return false;
return new LocalDateInterval(lastInterval.getTomDato().plusDays(1), entry.getLocalDateInterval().getFomDato().minusDays(1));
}
}
lastInterval = entry.getLocalDateInterval();
}
return true;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ class LocalDateTimelineTest {
void skal_opprette_kontinuerlig_tidslinje() {
LocalDateTimeline<String> tidslinje = basicContinuousTimeline();
assertThat(tidslinje.isContinuous()).isTrue();
assertThat(tidslinje.firstDiscontinuity()).isNull();
}

@Test
void skal_opprette_ikke_kontinuerlig_tidslinje() {
LocalDateTimeline<String> tidslinje = basicDiscontinuousTimeline();
assertThat(tidslinje.isContinuous()).isFalse();
var firstDiscontinuedInterval = tidslinje.firstDiscontinuity();
assertThat(firstDiscontinuedInterval).isNotNull();
// tidslinje = Timeline of two intervals A,C. Verify that full timeline A,B,C - A,C = B = discontinuity
var continuousTimeline = new LocalDateTimeline<>(tidslinje.getMinLocalDate(), tidslinje.getMaxLocalDate(), "hello world");
assertThat(continuousTimeline.disjoint(tidslinje).getLocalDateIntervals().stream().findFirst().orElseThrow()).isEqualTo(firstDiscontinuedInterval);

}

@Test
Expand Down

0 comments on commit 28273b2

Please sign in to comment.