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

com.ly.mybatis.mapperservice.service.join.inject.InjectStream Maven / Gradle / Ivy

The newest version!
package com.ly.mybatis.mapperservice.service.join.inject;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class InjectStream {
    private final Queue queue = new LinkedList<>();


    public  InjectStream filter(Predicate predicate) {
        queue.add(Operation.of(1, predicate));
        return this;
    }


    public  InjectStream map(Function mapper) {
        queue.add(Operation.of(2, mapper));
        return this;
    }


    public  InjectStream flatMap(Function> mapper) {
        queue.add(Operation.of(3, mapper));
        return this;
    }


    public  InjectStream collect(Collector collector) {
        queue.add(Operation.of(4, collector));
        return this;
    }

    public  InjectStream set(BiConsumer set) {
        queue.add(Operation.of(5, set));
        return this;
    }

    public void process(Collection ts) {
        if (ts.isEmpty()) {
            return;
        }

        Stream stream = ts.parallelStream()
                .map(Model::copy);
        for (Operation operation : queue) {
            switch (operation.getType()) {
                case 1:
                    stream = stream.filter(m -> {
                        try {
                            return operation.>getOperation().test(m.join());
                        } catch (Exception e) {
                            e.printStackTrace();
                            return false;
                        }
                    });
                    break;
                case 2:
                    stream = stream.peek(m -> {
                        try {
                            m.join(operation.>getOperation().apply(m.join()));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                    break;
                case 3:
                    stream = stream.flatMap(m -> {
                        try {
                            return operation.>>getOperation()
                                    .apply(m.join())
                                    .parallelStream()
                                    .map(j -> m.copy().join(j));
                        } catch (Exception e) {
                            e.printStackTrace();
                            return Stream.empty();
                        }
                    });
                    break;
                case 4:
                    stream = stream.collect(Collectors.groupingBy(Model::fromId))
                            .entrySet().parallelStream()
                            .map(entry -> {
                                try {
                                    return entry.getValue().stream()
                                            .findFirst()
                                            .get()
                                            .copy()
                                            .join(entry.getValue().parallelStream()
                                                    .map(Model::join)
                                                    .collect(operation.getOperation()));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    return null;
                                }
                            })
                            .filter(Objects::nonNull);
                    break;
                case 5:
                    stream = stream.peek(m -> {
                        try {
                            operation.>getOperation()
                                    .accept(m.source(), m.join());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                    break;
            }
        }
        stream.forEach(model -> {
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy