Java Properties extractFromPropertiesAsMap(String prefix, Properties properties)

Here you can find the source of extractFromPropertiesAsMap(String prefix, Properties properties)

Description

Extract part of given properties as a map.

License

Apache License

Parameter

Parameter Description
prefix prefix which specifies the part which should be extracted as map
properties properties to extract from

Return

the extracted map or null if no such map exists

Declaration

public static Map<String, String> extractFromPropertiesAsMap(String prefix, Properties properties) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*from  w w  w.  j  a  va 2 s.  co m*/
     * Extract part of given properties as a map. The given prefix is used to find the properties,
     * the rest of the property name is used as key for the map.
     *
     * @param prefix prefix which specifies the part which should be extracted as map
     * @param properties properties to extract from
     * @return the extracted map or null if no such map exists
     */
    public static Map<String, String> extractFromPropertiesAsMap(String prefix, Properties properties) {
        Map<String, String> ret = new HashMap<>();
        Enumeration names = properties.propertyNames();
        String prefixP = prefix + ".";
        while (names.hasMoreElements()) {
            String propName = (String) names.nextElement();
            if (propMatchesPrefix(prefixP, propName)) {
                String mapKey = propName.substring(prefixP.length());
                ret.put(mapKey, properties.getProperty(propName));
            }
        }
        return ret.size() > 0 ? ret : null;
    }

    private static boolean propMatchesPrefix(String prefix, String key) {
        return key.startsWith(prefix) && key.length() >= prefix.length();
    }
}

Related

  1. convertResourceBundleToProperties(ResourceBundle resource)
  2. createScriptBodyFromScriptSet(Properties scriptSet)
  3. differentKeys(Properties p1, Properties p2)
  4. equalProps(Properties p1, Properties p2)
  5. extractFromPropertiesAsList(String prefix, Properties properties)
  6. isKeySame(String key, Properties p1, Properties p2)
  7. isSupportedJVM(Map jdkProperties)
  8. maskApplicationEnvProperties(Map environmentVariables, Set variablesToMask)
  9. mergePropertiesIntoMap(Properties props, Map map)