Skip to content

Commit

Permalink
Lager ny liste ved splitt for å ikkje endre på samme liste i to ulike…
Browse files Browse the repository at this point in the history
… segmenter (#149)
  • Loading branch information
espenjv authored Apr 14, 2023
1 parent b957c91 commit 50ed51a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private static <X> LocalDateSegment<X> tilpassSegment(boolean harSegment, LocalD
if (!harSegment) {
return null;
}
if (segment.getLocalDateInterval().equals(ønsketIntervall)){
if (segment.getLocalDateInterval().equals(ønsketIntervall)) {
return segment;
}
return segmentSplitter.apply(ønsketIntervall, segment);
Expand Down Expand Up @@ -307,7 +307,7 @@ public LocalDateTimeline<V> compress(BiPredicate<V, V> e, LocalDateSegmentCombin
public LocalDateTimeline<V> compress(BiPredicate<LocalDateInterval, LocalDateInterval> a, BiPredicate<V, V> e, LocalDateSegmentCombinator<V, V, V> c) {
var factory = new CompressorFactory<>(a, e, c);
TimelineCompressor<V> compressor = segments.stream()
.collect(factory::get, TimelineCompressor::accept, TimelineCompressor::combine);
.collect(factory::get, TimelineCompressor::accept, TimelineCompressor::combine);

return new LocalDateTimeline<>(compressor.segmenter);
}
Expand Down Expand Up @@ -609,7 +609,8 @@ public static <V> LocalDateTimeline<List<V>> buildGroupOverlappingSegments(Colle
@SuppressWarnings({"cast"})
var uniqueSegments = segmentsWithPossibleOverlaps.stream().map(s -> new LocalDateSegment<>(s.getLocalDateInterval(), (List<V>) new ArrayList<V>()))
.collect(Collectors.toList());
var uniqueIntervalTimeline = new LocalDateTimeline<>(uniqueSegments, (interval, lhs, rhs) -> new LocalDateSegment<>(interval, new ArrayList<V>()));

var uniqueIntervalTimeline = new LocalDateTimeline<>(uniqueSegments, (interval, lhs, rhs) -> new LocalDateSegment<>(interval, new ArrayList<V>()), LocalDateTimeline::createNewListOnSplit);

for (var per : uniqueIntervalTimeline.toSegments()) {
for (var seg : segmentsWithPossibleOverlaps) {
Expand All @@ -621,6 +622,13 @@ public static <V> LocalDateTimeline<List<V>> buildGroupOverlappingSegments(Colle
return uniqueIntervalTimeline;
}

private static <V> LocalDateSegment<List<V>> createNewListOnSplit(LocalDateInterval di, LocalDateSegment<List<V>> seg) {
if (di.equals(seg.getLocalDateInterval())) {
return seg;
}
return new LocalDateSegment<>(di, new ArrayList<>(seg.getValue()));
}

@Override
public String toString() {
return getClass().getSimpleName() + "<" + //$NON-NLS-1$
Expand Down Expand Up @@ -816,7 +824,7 @@ private NavigableMap<LocalDateInterval, Integer> joinLocalDateIntervals(Navigabl
* Finner alle knekkpunkter fra to tidslinjer, i sekvens.
* <p>
* Knekkpunkter er 'start av et intervall' og 'dagen etter slutt av et intervall'. Sistnevnte fordi det da kan være starten på et nytt intervall
*
* <p>
* Hvis slutt av intervall er LocalDate.MAX, er dagen etter ikke representerbar i LocalDate, derfor bruker denne klassen epochDay istedet
*/
private static class KnekkpunktIterator<V, T> {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,31 @@ void skal_gruppere_per_segment_periode() {

}

@Test
void skal_gruppere_per_segment_periode_med_overlapp_midt_i_periode() {
LocalDate d1 = LocalDate.now();
LocalDate d2 = d1.plusDays(2);
LocalDate d3 = d2.plusDays(1);
LocalDate d4 = d3.plusDays(2);


List<LocalDateSegment<String>> segmenterMedOverlapp = List.of(
new LocalDateSegment<>(d1, d4, "A"),
new LocalDateSegment<>(d2, d3, "B"));

var timeline = LocalDateTimeline.buildGroupOverlappingSegments(segmenterMedOverlapp).compress();
List<LocalDateInterval> intervaller = List.copyOf(timeline.getLocalDateIntervals());
assertThat(intervaller).hasSize(3);

assertThat(timeline.intersection(intervaller.get(0))).isEqualTo(new LocalDateTimeline<>(intervaller.get(0), List.of("A")));

assertThat(timeline.intersection(intervaller.get(1))).isEqualTo(new LocalDateTimeline<>(intervaller.get(1), List.of("A", "B")));

assertThat(timeline.intersection(intervaller.get(2))).isEqualTo(new LocalDateTimeline<>(intervaller.get(2), List.of("A")));


}

@Test
void skal_håndtere_overlapp_når_flere_perioder_overlapper_med_hverandre() {
Set<LocalDateSegment<Boolean>> segementer = new HashSet<>();
Expand Down

0 comments on commit 50ed51a

Please sign in to comment.