All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.core.util.IterableStream Maven / Gradle / Ivy

There is a newer version: 1.54.1
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util;

import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * This class provides utility to iterate over values using standard 'for-each' style loops or to convert them into a
 * {@link Stream} and operate in that fashion.
 *
 * 

Code sample using Stream

* * *
 * // process the stream
 * myIterableStream.stream().forEach(resp -> {
 *     if (resp.getStatusCode() == HttpURLConnection.HTTP_OK) {
 *         System.out.printf("Response headers are %s. Url %s%n", resp.getDeserializedHeaders(),
 *             resp.getRequest().getUrl());
 *         resp.getElements().forEach(value -> System.out.printf("Response value is %d%n", value));
 *     }
 * });
 * 
* * *

Code sample using Iterator

* * *
 * // Iterate over iterator
 * for (PagedResponseBase<String, Integer> resp : myIterableStream) {
 *     if (resp.getStatusCode() == HttpURLConnection.HTTP_OK) {
 *         System.out.printf("Response headers are %s. Url %s%n", resp.getDeserializedHeaders(),
 *             resp.getRequest().getUrl());
 *         resp.getElements().forEach(value -> System.out.printf("Response value is %d%n", value));
 *     }
 * }
 * 
* * *

Code sample using Stream and filter

* * *
 * // process the stream
 * myIterableStream.stream().filter(resp -> resp.getStatusCode() == HttpURLConnection.HTTP_OK)
 *     .limit(10)
 *     .forEach(resp -> {
 *         System.out.printf("Response headers are %s. Url %s%n", resp.getDeserializedHeaders(),
 *             resp.getRequest().getUrl());
 *         resp.getElements().forEach(value -> System.out.printf("Response value is %d%n", value));
 *     });
 * 
* * * @param The type of value in this {@link Iterable}. * @see Iterable */ public class IterableStream implements Iterable { /* * This is the default batch size that will be requested when using stream or iterable by page, this will indicate * to Reactor how many elements should be prefetched before another batch is requested. */ private static final int DEFAULT_BATCH_SIZE = 1; private static final IterableStream EMPTY = new IterableStream<>(new ArrayList<>()); private final Flux flux; private final Iterable iterable; /** * Creates an instance with the given {@link Flux}. * * @param flux Flux of items to iterate over. * @throws NullPointerException If {@code flux} is {@code null}. */ public IterableStream(Flux flux) { this.flux = Objects.requireNonNull(flux, "'flux' cannot be null."); this.iterable = null; } /** * Creates an instance with the given {@link Iterable}. * * @param iterable Collection of items to iterate over. * @throws NullPointerException If {@code iterable} is {@code null}. */ public IterableStream(Iterable iterable) { this.iterable = Objects.requireNonNull(iterable, "'iterable' cannot be null."); this.flux = null; } /** * Utility function to provide {@link Stream} of value {@code T}. * * @return {@link Stream} of value {@code T}. */ public Stream stream() { return (flux != null) ? flux.toStream(DEFAULT_BATCH_SIZE) : StreamSupport.stream(iterable.spliterator(), false); } /** * Utility function to provide {@link Iterator} of value {@code T}. * * @return {@link Iterator} of value {@code T}. */ @Override public Iterator iterator() { return (flux != null) ? flux.toIterable(DEFAULT_BATCH_SIZE).iterator() : iterable.iterator(); } /** * Creates an {@link IterableStream} from an {@link Iterable}. *

* An empty {@link IterableStream} will be returned if the input iterable is {@code null}. * * @param iterable Collection of items to iterate over. * @param The type of value in this {@link Iterable}. * @return An {@link IterableStream} based on the passed collection. */ @SuppressWarnings("unchecked") public static IterableStream of(Iterable iterable) { if (iterable == null) { return (IterableStream) EMPTY; } else { return new IterableStream(iterable); } } }