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

io.quarkus.devtools.codestarts.utils.NestedMaps Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.devtools.codestarts.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public final class NestedMaps {

    private NestedMaps() {
    }

    @SuppressWarnings("unchecked")
    public static  Optional getValue(Map data, String path) {
        if (!path.contains(".")) {
            return Optional.ofNullable((T) data.get(path));
        }
        int index = path.indexOf(".");
        String key = path.substring(0, index);
        if (data.get(key) instanceof Map) {
            return getValue((Map) data.get(key), path.substring(index + 1));
        } else {
            return Optional.empty();
        }

    }

    public static  Map deepMerge(final Stream> mapStream) {
        final Map out = new HashMap<>();
        mapStream.forEach(m -> internalDeepMerge(out, m));
        return out;
    }

    public static Map deepMerge(final Map left, final Map right) {
        final Map out = new HashMap<>();
        internalDeepMerge(out, left);
        internalDeepMerge(out, right);
        return out;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static void internalDeepMerge(Map left, Map right) {
        for (Object key : right.keySet()) {
            Object rightValue = right.get(key);
            Object leftValue = left.get(key);

            if (rightValue instanceof Map && leftValue instanceof Map) {
                internalDeepMerge((Map) leftValue, (Map) rightValue);
            } else if (rightValue instanceof Collection && leftValue instanceof Collection) {
                Collection c = new LinkedHashSet();
                c.addAll((Collection) leftValue);
                c.addAll((Collection) rightValue);
                left.put(key, c);
            } else if (rightValue instanceof Map) {
                final Map map = new HashMap();
                internalDeepMerge(map, (Map) rightValue);
                left.put(key, map);
            } else if (rightValue instanceof Collection) {
                left.put(key, new LinkedHashSet((Collection) rightValue));
            } else if (rightValue instanceof Integer
                    || rightValue instanceof Boolean
                    || rightValue instanceof Float
                    || rightValue instanceof Long
                    || rightValue instanceof Double
                    || rightValue instanceof String
                    || rightValue == null) {
                // Override
                left.put(key, rightValue);
            } else {
                throw new IllegalArgumentException("Invalid value type for deepMerge: " + rightValue.getClass());
            }
        }
    }

    public static Map unflatten(Map flattened) {
        Map unflattened = new HashMap<>();
        for (Map.Entry entry : flattened.entrySet()) {
            doUnflatten(unflattened, entry.getKey(), entry.getValue());
        }
        return unflattened;
    }

    private static void doUnflatten(Map current, String key, Object originalValue) {
        String[] parts = key.split("\\.");
        for (int i = 0; i < parts.length; i++) {
            String part = parts[i];
            if (i == (parts.length - 1)) {
                if (current.containsKey(part)) {
                    throw new IllegalStateException("Conflicting data types for key '" + key + "'");
                }
                current.put(part, originalValue);
                return;
            }

            final Object value = current.get(part);
            if (value == null) {
                final HashMap map = new HashMap<>();
                current.put(part, map);
                current = map;
            } else if (value instanceof Map) {
                current = (Map) value;
            } else {
                throw new IllegalStateException("Conflicting data types for key '" + key + "'");
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy