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

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

There is a newer version: 3.2.0
Show newest version
package com.pivovarit.collectors;

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;

import static com.pivovarit.collectors.AsyncParallelCollector.collectingWithCollector;

/**
 * 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 on a custom {@link Executor}
     * and returning them as a {@link CompletableFuture} containing a result of the application of the user-provided {@link Collector}.
     *
     * 

* The parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *
* Example: *
{@code
     * CompletableFuture> result = Stream.of(1, 2, 3)
     *   .collect(parallel(i -> foo(i), toList(), executor));
     * }
* * @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} * * @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) { return collectingWithCollector(collector, mapper, executor); } /** * 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 parallelism the 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 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 parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *

* 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));
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @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) { return AsyncParallelCollector.collectingToStream(mapper, executor); } /** * 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 parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *

* 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 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 on a custom {@link Executor} * and returning a {@link Stream} instance returning results in completion order * *

* The parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *

* Instances should not be reused. * *
* Example: *
{@code
     * List result = Stream.of(1, 2, 3)
     *   .collect(parallelToStream(i -> foo(), executor))
     *   .collect(toList());
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @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) { return ParallelStreamCollector.streaming(mapper, executor); } /** * 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. * *
* 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 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 on a custom {@link Executor} * and returning a {@link Stream} instance returning results as they arrive while maintaining the initial order. * *

* The parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *

* Instances should not be reused. *

* Example: *
{@code
     * Stream.of(1, 2, 3)
     *   .collect(parallelToOrderedStream(i -> foo(), executor))
     *   .forEach(System.out::println);
     * }
* * @param mapper a transformation to be performed in parallel * @param executor the {@code Executor} to use for asynchronous execution * @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) { return ParallelStreamCollector.streamingOrdered(mapper, executor); } /** * 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. * *
* 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 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 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) */ static class 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}. * * @implNote this collector processed elements in batches and not separately * *
* 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 parallelism the parallelism level * * @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.collectingWithCollectorInBatches(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. * * @implNote this collector processed elements in batches and not separately * *

* The parallelism level defaults to {@code Runtime.availableProcessors() - 1} * *

* 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 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.collectingToStreamInBatches(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. * * @implNote this collector processed elements in batches and not separately * *
* 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 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.streamingInBatches(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. * * @implNote this collector processed elements in batches and not separately * *
* 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 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.streamingOrderedInBatches(mapper, executor, parallelism); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy