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

com.aeontronix.enhancedmule.tools.util.JacksonFlattener Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha4
Show newest version
/*
 * Copyright (c) Aeontronix 2023
 */

package com.aeontronix.enhancedmule.propertiesprovider.utils;

import com.aeontronix.commons.StringUtils;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

@SuppressWarnings("unchecked")
public class JacksonFlattener {
    /**
     * Flatten a hierarchical {@link Map} into a flat {@link Map} with key names using
     * property dot notation.
     *
     * @param inputMap must not be {@literal null}.
     * @return the resulting {@link Map}.
     * @since 2.0
     */
    public static Map flattenToStringMap(Map inputMap) {
        Map resultMap = new LinkedHashMap<>();
        doFlatten("", inputMap.entrySet().iterator(), resultMap, it -> it == null ? null : it.toString());
        return resultMap;
    }

    private static void doFlatten(String propertyPrefix, Iterator> inputMap,
                                  Map resultMap, Function valueTransformer) {

        if (StringUtils.isNotBlank(propertyPrefix)) {
            propertyPrefix = propertyPrefix + ".";
        }

        while (inputMap.hasNext()) {

            Map.Entry entry = inputMap.next();
            flattenElement(propertyPrefix.concat(entry.getKey()), entry.getValue(), resultMap, valueTransformer);
        }
    }

    private static void flattenElement(String propertyPrefix, Object source, Map resultMap,
                                       Function valueTransformer) {

        if (source instanceof Iterable) {
            flattenCollection(propertyPrefix, (Iterable) source, resultMap, valueTransformer);
            return;
        }

        if (source instanceof Map) {
            doFlatten(propertyPrefix, ((Map) source).entrySet().iterator(), resultMap, valueTransformer);
            return;
        }
        ((Map) resultMap).put(propertyPrefix, valueTransformer.apply(source));
    }

    private static void flattenCollection(String propertyPrefix, Iterable iterable, Map resultMap,
                                          Function valueTransformer) {
        int counter = 0;
        for (Object element : iterable) {
            flattenElement(propertyPrefix + "[" + counter + "]", element, resultMap, valueTransformer);
            counter++;
        }
    }
}