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

Standardcombinators min+max for comparable + concatLists + setOps #116

Merged
merged 7 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/main/java/no/nav/fpsak/tidsserie/StandardCombinators.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import no.nav.fpsak.tidsserie.LocalDateTimeline.JoinStyle;

Expand Down Expand Up @@ -57,8 +58,7 @@ public static <V> LocalDateSegment<List<V>> allValues(LocalDateInterval dateInte
* Basic combinator som alltid returnerer Boolean.TRUE for angitt interval. Greit å bruke når verdi ikke betyr
* noe, kun intervaller. Merk hvilket intervall som benyttes avhenger av {@link JoinStyle} og "includeGaps". Alle som passer får True.
*/
public static LocalDateSegment<Boolean> alwaysTrueForMatch(
LocalDateInterval dateInterval,
public static LocalDateSegment<Boolean> alwaysTrueForMatch(LocalDateInterval dateInterval,
@SuppressWarnings("unused") LocalDateSegment<?> lhs, // NOSONAR
@SuppressWarnings("unused") LocalDateSegment<?> rhs // NOSONAR
) {
Expand All @@ -78,6 +78,14 @@ public static <T, V> LocalDateSegment<List> bothValues(LocalDateInterval dateInt
}
}

/** Basic combinator som returnerer første (Left-Hand Side) verdi hvis begge finnes og er like. */
jolarsen marked this conversation as resolved.
Show resolved Hide resolved
public static <V> LocalDateSegment<V> leftIfEqualsRight(LocalDateInterval dateInterval,
jolarsen marked this conversation as resolved.
Show resolved Hide resolved
LocalDateSegment<V> lhs,
LocalDateSegment<V> rhs) {
return lhs != null && rhs != null && Objects.equals(lhs.getValue(), rhs.getValue()) ?
new LocalDateSegment<>(dateInterval, lhs.getValue()) : null;
}

/** Basic combinator som alltid returnerer verdi fra første (Left-Hand Side) timeline hvis finnes, ellers andre. */
public static <V> LocalDateSegment<V> coalesceLeftHandSide(LocalDateInterval dateInterval,
LocalDateSegment<V> lhs, LocalDateSegment<V> rhs) {
Expand Down Expand Up @@ -137,6 +145,48 @@ public static LocalDateSegment<String> concat(LocalDateInterval dateInterval,
return new LocalDateSegment<>(dateInterval, (lv == null ? "" : lv) + (rv == null ? "" : rv));
}

/**
* Basic combinator som tar minste verdi, evt lhs dersom like
*/
public static <V extends Comparable<? super V>> LocalDateSegment<V> min(LocalDateInterval dateInterval,
LocalDateSegment<V> lhs,
LocalDateSegment<V> rhs) {
if (lhs != null && rhs != null) {
var least = lhs.getValue().compareTo(rhs.getValue()) <= 0 ? lhs.getValue() : rhs.getValue();
return new LocalDateSegment<>(dateInterval, least);
}
return lhs == null ? new LocalDateSegment<>(dateInterval, rhs.getValue()) : new LocalDateSegment<>(dateInterval, lhs.getValue());
}

/**
* Basic combinator som tar største verdi, evt lhs dersom like
*/
public static <V extends Comparable<? super V>> LocalDateSegment<V> max(LocalDateInterval dateInterval,
LocalDateSegment<V> lhs,
LocalDateSegment<V> rhs) {
if (lhs != null && rhs != null) {
var greatest = lhs.getValue().compareTo(rhs.getValue()) >= 0 ? lhs.getValue() : rhs.getValue();
return new LocalDateSegment<>(dateInterval, greatest);
}
return lhs == null ? new LocalDateSegment<>(dateInterval, rhs.getValue()) : new LocalDateSegment<>(dateInterval, lhs.getValue());
}

/**
* Basic combinator som slår sammen Liste-verdier til en liste
*/
public static <V> LocalDateSegment<List<V>> concatLists(LocalDateInterval dateInterval,
jolarsen marked this conversation as resolved.
Show resolved Hide resolved
LocalDateSegment<List<V>> lhs,
LocalDateSegment<List<V>> rhs) {
if (lhs != null && rhs != null) {
return new LocalDateSegment<>(dateInterval, Stream.concat(lhs.getValue().stream(), rhs.getValue().stream()).toList());
} else if (lhs == null && rhs == null) {
return null;
}
return lhs == null ? new LocalDateSegment<>(dateInterval, rhs.getValue()) : new LocalDateSegment<>(dateInterval, lhs.getValue());
}



@SuppressWarnings("unchecked")
private static <L extends Number, R extends Number> L sum(L lhs, R rhs) {
if (lhs == null && rhs == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,73 @@ public void eksempel_splitt_av_tidsserie_ved_period_week_1() throws Exception {
assertThat(mappedTimeline).isEqualTo(expectedTimeline);
}

@Test
public void min_comparable_test() {

var tidligDato = LocalDate.of(2019,11,15);
var senereDato = LocalDate.of(2020,6,1);

var timelineA = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", tidligDato),
toSegment("2020-02-03", "2020-02-16", tidligDato),
toSegment("2020-03-01", "2020-04-30", tidligDato)));

var timelineB = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", senereDato),
toSegment("2020-02-03", "2020-02-29", senereDato),
toSegment("2020-04-01", "2020-06-01", senereDato)));

var timelineBMin = timelineB.combine(timelineA, StandardCombinators::min, JoinStyle.LEFT_JOIN);

var expectedTimeline = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", tidligDato),
toSegment("2020-02-03", "2020-02-16", tidligDato),
toSegment("2020-02-17", "2020-02-29", senereDato),
toSegment("2020-04-01", "2020-04-30", tidligDato),
toSegment("2020-05-01", "2020-06-01", senereDato)));

assertThat(timelineBMin).isEqualTo(expectedTimeline);
}

@Test
public void likhets_inner_join_test() {

var tidlig = new ForEqualsTest("type1", LocalDate.of(2019,11,15));
var senere = new ForEqualsTest("type1", LocalDate.of(2020,6,1));
var annentype = new ForEqualsTest("type2", LocalDate.of(2020,6,1));

var timelineA = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", tidlig),
toSegment("2020-02-03", "2020-02-16", tidlig),
toSegment("2020-03-01", "2020-04-30", tidlig)));

var timelineB = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", tidlig),
toSegment("2020-02-03", "2020-02-29", annentype),
toSegment("2020-04-01", "2020-06-01", senere)));

var kombinert = timelineB.combine(timelineA, StandardCombinators::leftIfEqualsRight, JoinStyle.INNER_JOIN);

var expectedTimeline = new LocalDateTimeline<>(
List.of(
toSegment("2019-12-01", "2020-01-02", tidlig),
toSegment("2020-04-01", "2020-04-30", senere)));

assertThat(kombinert).isEqualTo(expectedTimeline);
}

private record ForEqualsTest(String type, LocalDate dato) {
@Override
public boolean equals(Object o) {
return this == o || o instanceof ForEqualsTest that && Objects.equals(type, that.type);
}
}

private static <V> LocalDateSegment<V> toSegment(String dt1, String dt2, V val) {
return new LocalDateSegment<>(LocalDate.parse(dt1), LocalDate.parse(dt2), val);
}
Expand Down