|
| 1 | +package io.smallrye.mutiny.groups; |
| 2 | + |
| 3 | +import static io.smallrye.mutiny.helpers.ParameterValidation.nonNull; |
| 4 | + |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.function.BiFunction; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.function.Supplier; |
| 9 | + |
| 10 | +import io.smallrye.common.annotation.CheckReturnValue; |
| 11 | +import io.smallrye.common.annotation.Experimental; |
| 12 | +import io.smallrye.mutiny.Multi; |
| 13 | + |
| 14 | +/** |
| 15 | + * A Gatherer operator transforms a stream of items by accumulating them into an accumulator and extracting |
| 16 | + * items from that accumulator when certain conditions are met. |
| 17 | + * |
| 18 | + * @param <I> the type of the items emitted by the upstream |
| 19 | + * @param <ACC> the type of the accumulator |
| 20 | + * @param <O> the type of the items emitted to the downstream |
| 21 | + */ |
| 22 | +@Experimental("This API is still being designed and may change in the future") |
| 23 | +public interface Gatherer<I, ACC, O> { |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a new accumulator. |
| 27 | + * |
| 28 | + * @return a new accumulator |
| 29 | + */ |
| 30 | + ACC accumulator(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Accumulates an item into the accumulator. |
| 34 | + * |
| 35 | + * @param accumulator the current accumulator |
| 36 | + * @param item the item to accumulate |
| 37 | + * @return the updated accumulator |
| 38 | + */ |
| 39 | + ACC accumulate(ACC accumulator, I item); |
| 40 | + |
| 41 | + /** |
| 42 | + * Extracts an item from the accumulator. |
| 43 | + * |
| 44 | + * @param accumulator the current accumulator |
| 45 | + * @param upstreamCompleted whether the upstream has completed |
| 46 | + * @return an Optional containing a Extraction with the updated accumulator and the extracted item, or an empty Optional if |
| 47 | + * no |
| 48 | + * item can be extracted |
| 49 | + */ |
| 50 | + Optional<Extraction<ACC, O>> extract(ACC accumulator, boolean upstreamCompleted); |
| 51 | + |
| 52 | + /** |
| 53 | + * Finalizes the accumulator and extracts the final item, if any. |
| 54 | + * This method is called when the upstream has completed and no more items can be extracted using the extract method. |
| 55 | + * |
| 56 | + * @param accumulator the current accumulator |
| 57 | + * @return an Optional containing the final item, or an empty Optional if no final item can be extracted |
| 58 | + */ |
| 59 | + Optional<O> finalize(ACC accumulator); |
| 60 | + |
| 61 | + /** |
| 62 | + * An extraction result containing the next accumulator and the next item to emit. |
| 63 | + * |
| 64 | + * @param nextAccumulator the next accumulator |
| 65 | + * @param nextItem the next item to emit |
| 66 | + * @param <ACC> the type of the accumulator |
| 67 | + * @param <O> the type of the item to emit |
| 68 | + */ |
| 69 | + record Extraction<ACC, O>(ACC nextAccumulator, O nextItem) { |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a new {@link Extraction} instance. |
| 73 | + * |
| 74 | + * @param nextAccumulator the next accumulator |
| 75 | + * @param nextItem the next item to emit |
| 76 | + * @return a new {@link Extraction} instance |
| 77 | + * @param <ACC> the type of the accumulator |
| 78 | + * @param <O> the type of the item to emit |
| 79 | + */ |
| 80 | + public static <ACC, O> Extraction<ACC, O> of(ACC nextAccumulator, O nextItem) { |
| 81 | + return new Extraction<>(nextAccumulator, nextItem); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Builder for creating a {@link Gatherer}. |
| 87 | + * |
| 88 | + * @param <I> the type of the items emitted by the upstream |
| 89 | + */ |
| 90 | + class Builder<I> { |
| 91 | + |
| 92 | + /** |
| 93 | + * Specifies the initial accumulator supplier. |
| 94 | + * <p> |
| 95 | + * The initial accumulator supplier is used to create a new accumulator. |
| 96 | + * |
| 97 | + * @param initialAccumulatorSupplier the supplier for the initial accumulator |
| 98 | + * @param <ACC> the type of the accumulator |
| 99 | + * @return the next step in the builder |
| 100 | + */ |
| 101 | + @CheckReturnValue |
| 102 | + public <ACC> InitialAccumulatorStep<I, ACC> into(Supplier<ACC> initialAccumulatorSupplier) { |
| 103 | + nonNull(initialAccumulatorSupplier, "initialAccumulatorSupplier"); |
| 104 | + return new InitialAccumulatorStep<>(initialAccumulatorSupplier); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * The first step in the builder to gather items emitted by a {@link Multi} into an accumulator. |
| 110 | + * |
| 111 | + * @param <I> the type of the items emitted by the upstream {@link Multi} |
| 112 | + * @param <ACC> the type of the accumulator |
| 113 | + */ |
| 114 | + class InitialAccumulatorStep<I, ACC> { |
| 115 | + private final Supplier<ACC> initialAccumulatorSupplier; |
| 116 | + |
| 117 | + private InitialAccumulatorStep(Supplier<ACC> initialAccumulatorSupplier) { |
| 118 | + this.initialAccumulatorSupplier = initialAccumulatorSupplier; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Specifies the accumulator function. |
| 123 | + * <p> |
| 124 | + * The accumulator function is used to accumulate the items emitted by the upstream. |
| 125 | + * |
| 126 | + * @param accumulator the accumulator function, which takes the current accumulator and the item emitted by the |
| 127 | + * upstream, and returns the new accumulator |
| 128 | + * @return the next step in the builder |
| 129 | + */ |
| 130 | + @CheckReturnValue |
| 131 | + public ExtractStep<I, ACC> accumulate(BiFunction<ACC, I, ACC> accumulator) { |
| 132 | + nonNull(accumulator, "accumulator"); |
| 133 | + return new ExtractStep<>(initialAccumulatorSupplier, accumulator); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * The second step in the builder to gather items emitted by a {@link Multi} into an accumulator. |
| 139 | + * |
| 140 | + * @param <I> the type of the items emitted by the upstream {@link Multi} |
| 141 | + * @param <ACC> the type of the accumulator |
| 142 | + */ |
| 143 | + class ExtractStep<I, ACC> { |
| 144 | + private final Supplier<ACC> initialAccumulatorSupplier; |
| 145 | + private final BiFunction<ACC, I, ACC> accumulator; |
| 146 | + |
| 147 | + private ExtractStep(Supplier<ACC> initialAccumulatorSupplier, BiFunction<ACC, I, ACC> accumulator) { |
| 148 | + this.initialAccumulatorSupplier = initialAccumulatorSupplier; |
| 149 | + this.accumulator = accumulator; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Specifies the extractor function. |
| 154 | + * <p> |
| 155 | + * The extractor function is used to extract the items from the accumulator. |
| 156 | + * When the extractor function returns an empty {@link Optional}, no value is emitted. |
| 157 | + * When the extractor function returns a non-empty {@link Optional}, the value is emitted, and the accumulator is |
| 158 | + * updated. |
| 159 | + * This is done by returning a {@link Extraction} containing the new accumulator and the value to emit. |
| 160 | + * |
| 161 | + * @param extractor the extractor function, which takes the current accumulator and returns an {@link Optional} |
| 162 | + * containing a {@link Extraction} with the new accumulator and the value to emit |
| 163 | + * @param <O> the type of the value to emit |
| 164 | + * @return the next step in the builder |
| 165 | + */ |
| 166 | + @CheckReturnValue |
| 167 | + public <O> FinalizerStep<I, ACC, O> extract(BiFunction<ACC, Boolean, Optional<Extraction<ACC, O>>> extractor) { |
| 168 | + nonNull(extractor, "extractor"); |
| 169 | + return new FinalizerStep<>(initialAccumulatorSupplier, accumulator, extractor); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * The third step in the builder to gather items emitted by a {@link Multi} into an accumulator. |
| 175 | + * |
| 176 | + * @param <I> the type of the items emitted by the upstream |
| 177 | + * @param <ACC> the type of the accumulator |
| 178 | + * @param <O> the type of the items emitted to the downstream |
| 179 | + */ |
| 180 | + class FinalizerStep<I, ACC, O> { |
| 181 | + private final Supplier<ACC> initialAccumulatorSupplier; |
| 182 | + private final BiFunction<ACC, I, ACC> accumulator; |
| 183 | + private final BiFunction<ACC, Boolean, Optional<Extraction<ACC, O>>> extractor; |
| 184 | + |
| 185 | + private FinalizerStep(Supplier<ACC> initialAccumulatorSupplier, |
| 186 | + BiFunction<ACC, I, ACC> accumulator, |
| 187 | + BiFunction<ACC, Boolean, Optional<Extraction<ACC, O>>> extractor) { |
| 188 | + this.initialAccumulatorSupplier = initialAccumulatorSupplier; |
| 189 | + this.accumulator = accumulator; |
| 190 | + this.extractor = extractor; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Specifies the finalizer function. |
| 195 | + * <p> |
| 196 | + * The finalizer function is used to emit the final value upon completion of the upstream and when there are no more |
| 197 | + * items that can be extracted from the accumulator. |
| 198 | + * When the finalizer function returns an empty {@link Optional}, no value is emitted before the completion signal. |
| 199 | + * When the finalizer function returns a non-empty {@link Optional}, the value is emitted before the completion signal. |
| 200 | + * |
| 201 | + * @param finalizer the finalizer function, which takes the current accumulator and returns an {@link Optional} |
| 202 | + * containing the value to emit before the completion signal, if any |
| 203 | + * @return the gathering {@link Multi} |
| 204 | + */ |
| 205 | + @CheckReturnValue |
| 206 | + public Gatherer<I, ACC, O> finalize(Function<ACC, Optional<O>> finalizer) { |
| 207 | + nonNull(finalizer, "finalizer"); |
| 208 | + return Gatherers.of(initialAccumulatorSupplier, accumulator, extractor, finalizer); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments