Java Properties extractFromPropertiesAsList(String prefix, Properties properties)

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

Description

Extract from given properties a list of string values.

License

Apache License

Parameter

Parameter Description
prefix for selecting the properties from which the list should be extracted
properties properties from which to extract from

Return

parsed list or null if no element with prefixes exists

Declaration

public static List<String> extractFromPropertiesAsList(String prefix, Properties properties) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from  w  ww . jav a  2  s  .  c  om*/
     * Extract from given properties a list of string values. The prefix is used to determine the subset of the
     * given properties from which the list should be extracted, the rest is used as a numeric index. If the rest
     * is not numeric, the order is not determined (all those props are appended to the end of the list)
     *
     * @param prefix for selecting the properties from which the list should be extracted
     * @param properties properties from which to extract from
     * @return parsed list or null if no element with prefixes exists
     */
    public static List<String> extractFromPropertiesAsList(String prefix, Properties properties) {
        TreeMap<Integer, String> orderedMap = new TreeMap<>();
        List<String> rest = new ArrayList<>();
        Enumeration names = properties.propertyNames();
        String prefixP = prefix + ".";
        while (names.hasMoreElements()) {
            String key = (String) names.nextElement();
            if (propMatchesPrefix(prefixP, key)) {
                String index = key.substring(prefixP.length());
                String value = properties.getProperty(key);
                try {
                    Integer nrIndex = Integer.parseInt(index);
                    orderedMap.put(nrIndex, value);
                } catch (NumberFormatException exp) {
                    rest.add(value);
                }
            }
        }
        List<String> ret = new ArrayList<>(orderedMap.values());
        ret.addAll(rest);
        return ret.size() > 0 ? ret : null;
    }

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

Related

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