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

io.github.pellse.assembler.akkastream.AkkaFlowAdapter Maven / Gradle / Ivy

Go to download

Small library allowing to efficiently assemble entities from querying/merging external datasources or aggregating microservices

There is a newer version: 0.5.0
Show newest version
/*
 * Copyright 2018 Sebastien Pelletier
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.pellse.assembler.akkastream;

import akka.NotUsed;
import akka.stream.FlowShape;
import akka.stream.Inlet;
import akka.stream.UniformFanInShape;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.GraphDSL;
import akka.stream.javadsl.Source;
import akka.stream.javadsl.ZipN;
import io.github.pellse.assembler.AssemblerAdapter;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static akka.stream.javadsl.Source.*;
import static java.util.stream.Collectors.toList;

public class AkkaFlowAdapter implements AssemblerAdapter, R, NotUsed>> {

    private final UnaryOperator, ?>> sourceTransformer;

    private AkkaFlowAdapter(UnaryOperator, ?>> sourceTransformer) {
        this.sourceTransformer = sourceTransformer;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Flow, R, NotUsed> convertMapperSources(Stream>> mapperSources,
                                                          Function>, Stream> domainObjectStreamBuilder) {

        List>> sourceList = mapperSources.collect(toList());

        return Flow.fromGraph(GraphDSL.create(builder -> {

            Inlet> in = Inlet.create("assembler-inflow");
            UniformFanInShape, List>> zipN = builder.add(ZipN.create(sourceList.size()));

            IntStream.range(0, sourceList.size())
                    .forEach(i -> builder.from(builder.add(createAkkaSource(sourceList.get(i)))).toInlet(zipN.in(i)));

            return FlowShape.of(in, zipN.out());

        })).flatMapConcat(mapperResults -> from(domainObjectStreamBuilder.apply(mapperResults)::iterator));
    }

    private Source, ?> createAkkaSource(Supplier> mappingSupplier) {
        return sourceTransformer.apply(lazily(() -> single(mappingSupplier.get())));
    }

    public static  AkkaFlowAdapter akkaFlowAdapter() {
        return akkaFlowAdapter(false);
    }

    public static  AkkaFlowAdapter akkaFlowAdapter(boolean async) {
        return akkaFlowAdapter(source -> async ? source.async() : source);
    }

    public static  AkkaFlowAdapter akkaFlowAdapter(UnaryOperator, ?>> sourceTransformer) {
        return new AkkaFlowAdapter<>(sourceTransformer);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy