Skip to content

Commit 1a4bc09

Browse files
authored
NOJIRA utvidet compress som tillater custom likhet + ny verdi (#19)
1 parent ee1db8b commit 1a4bc09

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/java/no/nav/fpsak/tidsserie/LocalDateTimeline.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.TreeSet;
1717
import java.util.concurrent.atomic.AtomicReference;
1818
import java.util.function.BiFunction;
19+
import java.util.function.BiPredicate;
1920
import java.util.function.Consumer;
2021
import java.util.function.Function;
2122
import java.util.function.Predicate;
@@ -214,8 +215,22 @@ public <T, R> LocalDateTimeline<R> combine(final LocalDateTimeline<T> other, fin
214215
* for å 'redusere' tidslinjen ned til enkleste form før lagring etc.
215216
*/
216217
public LocalDateTimeline<V> compress() {
218+
var factory = new CompressorFactory<V>(Objects::equals, LocalDateSegment::new);
217219
TimelineCompressor<V> compressor = segments.stream()
218-
.collect(TimelineCompressor::new, TimelineCompressor::accept, TimelineCompressor::combine);
220+
.collect(factory::get, TimelineCompressor::accept, TimelineCompressor::combine);
221+
222+
return new LocalDateTimeline<>(compressor.segmenter);
223+
}
224+
225+
/**
226+
* Fikser opp tidslinjen ved å slå sammen sammenhengende intervall med "like" verider utover periode.
227+
* @param e - likhetspredikat for å sammenligne to segment som vurderes slått sammen
228+
* @param c - konstruktør som tar nytt intervall og gammel verdi og lager et nytt segment
229+
*/
230+
public LocalDateTimeline<V> compress(BiPredicate<V, V> e, BiFunction<LocalDateInterval, V, LocalDateSegment<V>> c) {
231+
var factory = new CompressorFactory<>(e, c);
232+
TimelineCompressor<V> compressor = segments.stream()
233+
.collect(factory::get, TimelineCompressor::accept, TimelineCompressor::combine);
219234

220235
return new LocalDateTimeline<>(compressor.segmenter);
221236
}
@@ -757,6 +772,13 @@ public LocalDateSegment<V> apply(LocalDateInterval di, LocalDateSegment<V> seg)
757772
static class TimelineCompressor<V> implements Consumer<LocalDateSegment<V>> {
758773

759774
private final NavigableSet<LocalDateSegment<V>> segmenter = new TreeSet<>();
775+
private final BiPredicate<V, V> equals;
776+
private final BiFunction<LocalDateInterval, V, LocalDateSegment<V>> construct;
777+
778+
TimelineCompressor(BiPredicate<V, V> e, BiFunction<LocalDateInterval, V, LocalDateSegment<V>> c) {
779+
this.equals = e;
780+
this.construct = c;
781+
}
760782

761783
@Override
762784
public void accept(LocalDateSegment<V> t) {
@@ -765,11 +787,11 @@ public void accept(LocalDateSegment<V> t) {
765787
} else {
766788
LocalDateSegment<V> last = segmenter.last();
767789
if (last.getLocalDateInterval().abuts(t.getLocalDateInterval())
768-
&& Objects.equals(last.getValue(), t.getValue())) {
790+
&& equals.test(last.getValue(), t.getValue())) {
769791
// bytt ut og ekspander intervall for siste
770792
segmenter.remove(last);
771793
LocalDateInterval expandedInterval = last.getLocalDateInterval().expand(t.getLocalDateInterval());
772-
segmenter.add(new LocalDateSegment<V>(expandedInterval, last.getValue()));
794+
segmenter.add(construct.apply(expandedInterval, last.getValue()));
773795
} else {
774796
segmenter.add(t);
775797
}
@@ -782,4 +804,18 @@ public void combine(@SuppressWarnings("unused") TimelineCompressor<V> other) {
782804

783805
}
784806

807+
static class CompressorFactory<V> {
808+
private final BiPredicate<V, V> equals;
809+
private final BiFunction<LocalDateInterval, V, LocalDateSegment<V>> construct;
810+
811+
CompressorFactory(BiPredicate<V, V> e, BiFunction<LocalDateInterval, V, LocalDateSegment<V>> c) {
812+
this.equals = e;
813+
this.construct = c;
814+
}
815+
816+
TimelineCompressor<V> get() {
817+
return new TimelineCompressor<>(equals, construct);
818+
}
819+
}
820+
785821
}

src/test/java/no/nav/fpsak/tidsserie/LocalDateTimelineTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ public void skal_compress_en_tidsserie_med_sammenhengende_intervaller_med_samme_
135135
assertThat(timeline.size()).isEqualTo(2);
136136

137137
// Act
138-
LocalDateTimeline<String> compressedTimeline = timeline.compress();
138+
LocalDateTimeline<String> compressedTimeline = timeline.compress(String::equals, (i, v) -> new LocalDateSegment<>(i, v + "*2"));
139139

140140
// Assert
141141
assertThat(compressedTimeline.size()).isEqualTo(1);
142-
assertThat(compressedTimeline).isEqualTo(new LocalDateTimeline<>(d1, d4, "hello"));
142+
assertThat(compressedTimeline).isEqualTo(new LocalDateTimeline<>(d1, d4, "hello*2"));
143143

144144
}
145145

0 commit comments

Comments
 (0)