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

org.cloudfoundry.identity.uaa.util.UaaMapUtils Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
/*
 * *****************************************************************************
 *      Cloud Foundry
 *      Copyright (c) [2009-2015] Pivotal Software, Inc. All Rights Reserved.
 *      This product is licensed to you under the Apache License, Version 2.0 (the "License").
 *      You may not use this product except in compliance with the License.
 *
 *      This product includes a number of subcomponents with
 *      separate copyright notices and license terms. Your use of these
 *      subcomponents is subject to the terms and conditions of the
 *      subcomponent's license, as noted in the LICENSE file.
 * *****************************************************************************
 */

package org.cloudfoundry.identity.uaa.util;


import org.cloudfoundry.identity.uaa.impl.config.NestedMapPropertySource;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class UaaMapUtils {

    public static Map flatten(Map map) {
        HashMap result = new HashMap<>();
        if (map == null || map.isEmpty()) {
            return result;
        }
        NestedMapPropertySource properties = new NestedMapPropertySource("map", map);
        for (String property : properties.getPropertyNames()) {
            if (properties.getProperty(property) != null) {
                result.put(property, properties.getProperty(property));
            }
        }
        return result;
    }


    public static Map getPropertiesStartingWith(ConfigurableEnvironment aEnv,
                                                                String aKeyPrefix) {
        Map result = new HashMap<>();
        Map map = getAllProperties(aEnv);
        for (Entry entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.startsWith(aKeyPrefix)) {
                result.put(key, entry.getValue());
            }
        }
        return result;
    }

    public static Map getAllProperties(ConfigurableEnvironment aEnv) {
        Map result = new HashMap<>();
        aEnv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
        return result;
    }

    public static Map getAllProperties(PropertySource aPropSource) {
        Map result = new HashMap<>();

        if (aPropSource instanceof CompositePropertySource) {
            CompositePropertySource cps = (CompositePropertySource) aPropSource;
            cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
            return result;
        }

        if (aPropSource instanceof EnumerablePropertySource) {
            EnumerablePropertySource ps = (EnumerablePropertySource) aPropSource;
            Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
            return result;
        }

        //unable to iterate over it

        return result;
    }

    private static void addAll(Map aBase, Map aToBeAdded) {
        for (Entry entry : aToBeAdded.entrySet()) {
            if (aBase.containsKey(entry.getKey())) {
                continue;
            }
            aBase.put(entry.getKey(), entry.getValue());
        }
    }

    @SafeVarargs
    public static > Map map(E... entries) {
        Map map = new HashMap<>();
        for (E entry : entries) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }

    public static  Map.Entry entry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy