From 79bcc338af9665d1668e7f40df6cae04a8c2a7d1 Mon Sep 17 00:00:00 2001 From: Ludovic DEHON Date: Fri, 21 Jan 2022 09:58:36 +0100 Subject: [PATCH] chore(refactor): add some helpers tools --- .../kestra/core/repositories/ArrayListTotal.java | 14 ++++++++++++++ .../kestra/webserver/utils/AutocompleteUtils.java | 12 +++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/io/kestra/core/repositories/ArrayListTotal.java b/core/src/main/java/io/kestra/core/repositories/ArrayListTotal.java index 1342b45fd1..de1f3963ba 100644 --- a/core/src/main/java/io/kestra/core/repositories/ArrayListTotal.java +++ b/core/src/main/java/io/kestra/core/repositories/ArrayListTotal.java @@ -6,6 +6,9 @@ import java.util.ArrayList; import java.util.List; +import java.util.function.Function; + +import static java.util.stream.Collectors.toCollection; @Getter @NoArgsConstructor @@ -25,8 +28,19 @@ public static ArrayListTotal of(Pageable pageable, List list) { return new ArrayListTotal(list.subList(from, to), size); } + public ArrayListTotal(long total) { + this.total = total; + } + public ArrayListTotal(List list, long total) { super(list); this.total = total; } + + public ArrayListTotal map(Function map) { + return this + .stream() + .map(map) + .collect(toCollection(() -> new ArrayListTotal(this.total))); + } } diff --git a/webserver/src/main/java/io/kestra/webserver/utils/AutocompleteUtils.java b/webserver/src/main/java/io/kestra/webserver/utils/AutocompleteUtils.java index 80f8c4fe34..99a765457a 100644 --- a/webserver/src/main/java/io/kestra/webserver/utils/AutocompleteUtils.java +++ b/webserver/src/main/java/io/kestra/webserver/utils/AutocompleteUtils.java @@ -1,16 +1,15 @@ package io.kestra.webserver.utils; import io.micronaut.http.exceptions.HttpStatusException; -import io.kestra.core.repositories.ArrayListTotal; -import java.util.ArrayList; import java.util.List; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; public class AutocompleteUtils { @SafeVarargs - public static List from(List... lists) throws HttpStatusException { + public static List map(Function map, List... lists) throws HttpStatusException { Stream stream = Stream.empty(); for (List list : lists) { @@ -22,6 +21,13 @@ public static List from(List... lists) throws HttpStatusException { return stream .distinct() + .map(map) .collect(Collectors.toList()); } + + @SafeVarargs + public static List from(List... lists) throws HttpStatusException { + return AutocompleteUtils + .map(o -> o, lists); + } }