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

io.github.pellse.assembler.akkastream.AkkaSourceAdapter 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.javadsl.Source;
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.Stream;

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

public class AkkaSourceAdapter implements AssemblerAdapter> {

    private final UnaryOperator, ?>> sourceTransformer;

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

    @Override
    public Source convertMapperSources(Stream>> mapperSources,
                                                   Function>, Stream> domainObjectStreamBuilder) {

        List, ?>> akkaSources = mapperSources
                .map(this::createAkkaSource)
                .collect(toList());

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

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

    public static  AkkaSourceAdapter akkaSourceAdapter() {
        return akkaSourceAdapter(false);
    }

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

    public static  AkkaSourceAdapter akkaSourceAdapter(UnaryOperator, ?>> sourceTransformer) {
        return new AkkaSourceAdapter<>(sourceTransformer);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy