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

org.babyfish.jimmer.sql.loader.Utils Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.sql.loader;

import org.babyfish.jimmer.sql.ast.tuple.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

class Utils {

    private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);

    @SafeVarargs
    static  Map mergeMap(Map ... maps) {
        List> nonNullMaps = Arrays
                .stream(maps)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if (nonNullMaps.isEmpty()) {
            return Collections.emptyMap();
        }
        if (nonNullMaps.size() == 1) {
            return nonNullMaps.get(0);
        }
        int totalCount = nonNullMaps.stream().mapToInt(Map::size).sum();
        Map finalMap = new LinkedHashMap<>((totalCount * 4 + 2) / 3);
        for (Map map : nonNullMaps) {
            finalMap.putAll(map);
        }
        return finalMap;
    }

    static  Map joinMaps(Map map1, Map map2) {
        Map map = new LinkedHashMap<>((map1.size() * 4 + 2) / 3);
        for (Map.Entry e : map1.entrySet()) {
            V v = map2.get(e.getValue());
            if (v != null) {
                map.put(e.getKey(), v);
            }
        }
        return map;
    }

    static  Map> joinMultiMapAndMap(
            Map> map1,
            Map map2
    ) {
        Map> map = new LinkedHashMap<>((map1.size() * 4 + 2) / 3);
        for (Map.Entry> e : map1.entrySet()) {
            List originalValues = e.getValue();
            if (originalValues != null && !originalValues.isEmpty()) {
                List values = new ArrayList<>();
                for (T t : originalValues) {
                    values.add(map2.get(t));
                }
                map.put(e.getKey(), values);
            }
        }
        return map;
    }

    static  Map joinCollectionAndMap(
            Collection c,
            Function middleKeyExtractor,
            Map map
    ) {
        Map resultMap = new LinkedHashMap<>((c.size() * 4 + 2) / 3);
        for (K k : c) {
            T t = middleKeyExtractor.apply(k);
            V v = map.get(t);
            if (v != null) {
                resultMap.put(k, v);
            }
        }
        return resultMap;
    }

    static  Map toMap(
            Function keyExtractor,
            Collection values
    ) {
        Map map = new LinkedHashMap<>((values.size() * 4 + 2) / 3);
        for (V value : values) {
            K key = keyExtractor.apply(value);
            map.put(key, value);
        }
        if (map.size() < values.size()) {
            LOGGER.warn("Utils.toMap() meet duplicated keys, original collection: {}", values);
        }
        return map;
    }

    static  Map> toMultiMap(
            Function keyExtractor,
            Collection values
    ) {
        return values.stream().collect(
                Collectors.groupingBy(
                        keyExtractor,
                        Collectors.toList()
                )
        );
    }

    static  List> toTuples(K key, Collection values) {
        if (values.isEmpty()) {
            return Collections.emptyList();
        }
        List> tuples = new ArrayList<>(values.size());
        for (V value : values) {
            tuples.add(new Tuple2<>(key, value));
        }
        return tuples;
    }

    static  Map toMap(K key, Collection values) {
        if (values.isEmpty()) {
            return Collections.emptyMap();
        }
        Map map = new LinkedHashMap<>((values.size() * 4 + 2) / 3);
        for (V value : values) {
            map.put(key, value);
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy