diff --git a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClient.java b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClient.java index e5e0f57a135a..93d000ed3923 100644 --- a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClient.java +++ b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClient.java @@ -509,11 +509,7 @@ private Flux listSettings(String nextPageLink) { } private Publisher extractAndFetchConfigurationSettings(PagedResponse page) { - String nextPageLink = page.nextLink(); - if (nextPageLink == null) { - return Flux.fromIterable(page.items()); - } - return Flux.fromIterable(page.items()).concatWith(listSettings(nextPageLink)); + return ImplUtils.extractAndFetch(page, this::listSettings); } /* diff --git a/core/azure-core/src/main/java/com/azure/core/implementation/util/ImplUtils.java b/core/azure-core/src/main/java/com/azure/core/implementation/util/ImplUtils.java index 939131bffa60..cfcc38d8976b 100644 --- a/core/azure-core/src/main/java/com/azure/core/implementation/util/ImplUtils.java +++ b/core/azure-core/src/main/java/com/azure/core/implementation/util/ImplUtils.java @@ -3,6 +3,10 @@ package com.azure.core.implementation.util; +import com.azure.core.http.rest.PagedResponse; +import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; + import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -19,7 +23,7 @@ private ImplUtils() { // Exists only to defeat instantiation. } - /** + /* * Creates a copy of the source byte array. * @param source Array to make copy of * @return A copy of the array, or null if source was null. @@ -33,7 +37,7 @@ public static byte[] clone(byte[] source) { return copy; } - /** + /* * Creates a copy of the source int array. * @param source Array to make copy of * @return A copy of the array, or null if source was null. @@ -47,7 +51,7 @@ public static int[] clone(int[] source) { return copy; } - /** + /* * Creates a copy of the source array. * @param source Array being copied. * @param Generic representing the type of the source array. @@ -61,7 +65,7 @@ public static T[] clone(T[] source) { return Arrays.copyOf(source, source.length); } - /** + /* * Checks if the array is null or empty. * @param array Array being checked for nullness or emptiness. * @return True if the array is null or empty, false otherwise. @@ -70,7 +74,7 @@ public static boolean isNullOrEmpty(Object[] array) { return array == null || array.length == 0; } - /** + /* * Checks if the collection is null or empty. * @param collection Collection being checked for nullness or emptiness. * @return True if the collection is null or empty, false otherwise. @@ -79,7 +83,7 @@ public static boolean isNullOrEmpty(Collection collection) { return collection == null || collection.isEmpty(); } - /** + /* * Checks if the map is null or empty. * @param map Map being checked for nullness or emptiness. * @return True if the map is null or empty, false otherwise. @@ -88,7 +92,7 @@ public static boolean isNullOrEmpty(Map map) { return map == null || map.isEmpty(); } - /** + /* * Turns an array into a string mapping each element to a string and delimits them using a coma. * @param array Array being formatted to a string. * @param mapper Function that maps each element to a string. @@ -103,7 +107,7 @@ public static String arrayToString(T[] array, Function mapper) { return Arrays.stream(array).map(mapper).collect(Collectors.joining(COMMA)); } - /** + /* * Returns the first instance of the given class from an array of Objects. * @param args Array of objects to search through to find the first instance of the given `clazz` type. * @param clazz The type trying to be found. @@ -123,4 +127,29 @@ public static T findFirstOfType(Object[] args, Class clazz) { return null; } + + + /* + * Checks if the character sequence is null or empty. + * @param charSequence Character sequence being checked for nullness or emptiness. + * @return True if the character sequence is null or empty, false otherwise. + */ + public static boolean isNullOrEmpty(CharSequence charSequence) { + return charSequence == null || charSequence.length() == 0; + } + + /* + * Extracts and combines the generic items from all the pages linked together. + * @param page The paged response from server holding generic items. + * @param content The function which fetches items from the next page. + * @param The type of the item being returned in the paged response. + * @return The publisher holding all the generic items combined. + */ + public static Publisher extractAndFetch(PagedResponse page, Function> content) { + String nextPageLink = page.nextLink(); + if (nextPageLink == null) { + return Flux.fromIterable(page.items()); + } + return Flux.fromIterable(page.items()).concatWith(content.apply(nextPageLink)); + } }