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

com.pivovarit.collectors.ParallelCollectors Maven / Gradle / Ivy

The newest version!
package com.pivovarit.collectors;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;

/**
 * An umbrella class exposing static factory methods for instantiating parallel {@link Collector}s
 *
 * @author Grzegorz Piwowarek
 */
public final class ParallelCollectors {

    private ParallelCollectors() {
    }

    /**
     * A convenience {@link Collector} used for executing parallel computations using Virtual Threads
     * and returning them as a {@link CompletableFuture} containing a result of the application of the user-provided {@link Collector}.
     *
     * 
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo(i), toList()));
     * }
* * @param mapper a transformation to be performed in parallel * @param collector the {@code Collector} describing the reduction * @param the type of the collected elements * @param the result returned by {@code mapper} * @param the reduction result {@code collector} * * @return a {@code Collector} which collects all processed elements into a user-provided mutable {@code Collection} in parallel * * @since 3.0.0 */ public static Collector> parallel(Function mapper, Collector collector) { return AsyncParallelCollector.collectingWithCollector(collector, mapper); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning them as a {@link CompletableFuture} containing a result of the application of the user-provided {@link Collector}. * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo(i), toList(), executor, 2));
     * }
* * @param mapper a transformation to be performed in parallel * @param collector the {@code Collector} describing the reduction * @param the type of the collected elements * @param the result returned by {@code mapper} * @param the reduction result {@code collector} * @param parallelism the max parallelism level * * @return a {@code Collector} which collects all processed elements into a user-provided mutable {@code Collection} in parallel * * @since 3.2.0 */ public static Collector> parallel(Function mapper, Collector collector, int parallelism) { return AsyncParallelCollector.collectingWithCollector(collector, mapper, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning them as a {@link CompletableFuture} containing a result of the application of the user-provided {@link Collector}. * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo(i), toList(), executor, 2));
     * }
* * @param mapper a transformation to be performed in parallel * @param collector the {@code Collector} describing the reduction * @param executor the {@code Executor} to use for asynchronous execution * @param the type of the collected elements * @param the result returned by {@code mapper} * @param the reduction result {@code collector} * @param parallelism the max parallelism level * * @return a {@code Collector} which collects all processed elements into a user-provided mutable {@code Collection} in parallel * * @since 2.0.0 */ public static Collector> parallel(Function mapper, Collector collector, Executor executor, int parallelism) { return AsyncParallelCollector.collectingWithCollector(collector, mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning them as {@link CompletableFuture} containing a {@link Stream} of these elements. * *

* The collector maintains the order of processed {@link Stream}. Instances should not be reused. * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo()));
     * }
* * @param mapper a transformation to be performed in parallel * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.0.0 */ public static Collector>> parallel(Function mapper) { return AsyncParallelCollector.collectingToStream(mapper); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning them as {@link CompletableFuture} containing a {@link Stream} of these elements. * *

* The collector maintains the order of processed {@link Stream}. Instances should not be reused. * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo()));
     * }
* * @param mapper a transformation to be performed in parallel * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.2.0 */ public static Collector>> parallel(Function mapper, int parallelism) { return AsyncParallelCollector.collectingToStream(mapper, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning them as {@link CompletableFuture} containing a {@link Stream} of these elements. * *

* The collector maintains the order of processed {@link Stream}. Instances should not be reused. * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo(), executor, 2));
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.0.0 */ public static Collector>> parallel(Function mapper, Executor executor, int parallelism) { return AsyncParallelCollector.collectingToStream(mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning a {@link Stream} instance returning results as they arrive. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToStream(i -> foo()))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.0.0 */ public static Collector> parallelToStream(Function mapper) { return ParallelStreamCollector.streaming(mapper); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning a {@link Stream} instance returning results as they arrive. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToStream(i -> foo(), executor, 2))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.2.0 */ public static Collector> parallelToStream(Function mapper, int parallelism) { return ParallelStreamCollector.streaming(mapper, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning a {@link Stream} instance returning results as they arrive. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToStream(i -> foo(), executor, 2))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.0.0 */ public static Collector> parallelToStream(Function mapper, Executor executor, int parallelism) { return ParallelStreamCollector.streaming(mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning a {@link Stream} instance returning results as they arrive while maintaining the initial order. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToOrderedStream(i -> foo()))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.0.0 */ public static Collector> parallelToOrderedStream(Function mapper) { return ParallelStreamCollector.streamingOrdered(mapper); } /** * A convenience {@link Collector} used for executing parallel computations using Virtual Threads * and returning a {@link Stream} instance returning results as they arrive while maintaining the initial order. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToOrderedStream(i -> foo(), executor, 2))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 3.2.0 */ public static Collector> parallelToOrderedStream(Function mapper, int parallelism) { return ParallelStreamCollector.streamingOrdered(mapper, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning a {@link Stream} instance returning results as they arrive while maintaining the initial order. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToOrderedStream(i -> foo(), executor, 2))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.0.0 */ public static Collector> parallelToOrderedStream(Function mapper, Executor executor, int parallelism) { return ParallelStreamCollector.streamingOrdered(mapper, executor, parallelism); } /** * A convenience {@code Collector} for collecting a {@code Stream>} * into a {@code CompletableFuture} using a provided {@code Collector} * * @param collector the {@code Collector} describing the reduction * @param the type of the collected elements * @param the result of the transformation * * @return a {@code Collector} which collects all futures and combines them into a single future * using the provided downstream {@code Collector} * * @since 2.3.0 */ public static Collector, ?, CompletableFuture> toFuture(Collector collector) { return FutureCollectors.toFuture(collector); } /** * A convenience {@code Collector} for collecting a {@code Stream>} into a {@code CompletableFuture>} * * @param the type of the collected elements * * @return a {@code Collector} which collects all futures and combines them into a single future * returning a list of results * * @since 2.3.0 */ public static Collector, ?, CompletableFuture>> toFuture() { return FutureCollectors.toFuture(); } /** * A subset of collectors which perform operations in batches and not separately (one object in a thread pool's worker queue represents a batch of operations to be performed by a single thread) */ public static final class Batching { private Batching() { } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning them as a {@link CompletableFuture} containing a result of the application of the user-provided {@link Collector}. * *
* Example: *
{@code
         * CompletableFuture> result = Stream.of(1, 2, 3)
         *   .collect(parallel(i -> foo(i), toList(), executor, 2));
         * }
* * @param mapper a transformation to be performed in parallel * @param collector the {@code Collector} describing the reduction * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * @param the reduction result {@code collector} * * @return a {@code Collector} which collects all processed elements into a user-provided mutable {@code Collection} in parallel * * @since 2.1.0 */ public static Collector> parallel(Function mapper, Collector collector, Executor executor, int parallelism) { return AsyncParallelCollector.BatchingCollectors .collectingWithCollector(collector, mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning them as {@link CompletableFuture} containing a {@link Stream} of these elements. * *

* The collector maintains the order of processed {@link Stream}. Instances should not be reused. * *
* Example: *
{@code
         * CompletableFuture> result = Stream.of(1, 2, 3)
         *   .collect(parallel(i -> foo(), executor, 2));
         * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.1.0 */ public static Collector>> parallel(Function mapper, Executor executor, int parallelism) { return AsyncParallelCollector.BatchingCollectors.collectingToStream(mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning a {@link Stream} instance returning results as they arrive. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
         * Stream.of(1, 2, 3)
         *   .collect(parallelToStream(i -> foo(), executor, 2))
         *   .forEach(System.out::println);
         * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.1.0 */ public static Collector> parallelToStream(Function mapper, Executor executor, int parallelism) { return ParallelStreamCollector.BatchingCollectors.streaming(mapper, executor, parallelism); } /** * A convenience {@link Collector} used for executing parallel computations on a custom {@link Executor} * and returning a {@link Stream} instance returning results as they arrive while maintaining the initial order. *

* For the parallelism of 1, the stream is executed by the calling thread. * *
* Example: *

{@code
         * Stream.of(1, 2, 3)
         *   .collect(parallelToOrderedStream(i -> foo(), executor, 2))
         *   .forEach(System.out::println);
         * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @param parallelism the max parallelism level * @param the type of the collected elements * @param the result returned by {@code mapper} * * @return a {@code Collector} which collects all processed elements into a {@code Stream} in parallel * * @since 2.1.0 */ public static Collector> parallelToOrderedStream(Function mapper, Executor executor, int parallelism) { return ParallelStreamCollector.BatchingCollectors.streamingOrdered(mapper, executor, parallelism); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy