diff --git a/src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java b/src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java index d8df058..3db9646 100644 --- a/src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java +++ b/src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java @@ -476,23 +476,32 @@ public LocalDateTimeline union(LocalDateTimeline 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 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; } /** diff --git a/src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java b/src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java index ca2300e..11474a3 100644 --- a/src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java +++ b/src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java @@ -26,12 +26,19 @@ class LocalDateTimelineTest { void skal_opprette_kontinuerlig_tidslinje() { LocalDateTimeline tidslinje = basicContinuousTimeline(); assertThat(tidslinje.isContinuous()).isTrue(); + assertThat(tidslinje.firstDiscontinuity()).isNull(); } @Test void skal_opprette_ikke_kontinuerlig_tidslinje() { LocalDateTimeline 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