Java List Transform transformNumberedPropertiesToList(Properties props)

Here you can find the source of transformNumberedPropertiesToList(Properties props)

Description

transform Numbered Properties To List

License

Educational Community License

Declaration

public static void transformNumberedPropertiesToList(Properties props) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w  . j  a  v a  2s.c  om*/
 * Copyright 2005-2013 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Main {
    public static void transformNumberedPropertiesToList(Properties props) {
        String key = null;
        String unnumberedKey = null;
        List<String> keyList = null;
        List<String> removeKeys = new LinkedList<String>();

        // unnumber keys and place their values in a list
        Iterator keys = props.keySet().iterator();
        Map<String, List<String>> keysLists = new HashMap<String, List<String>>();
        while (keys.hasNext()) {
            key = (String) keys.next();
            if (Character.isDigit(key.charAt(key.length() - 1))) {
                unnumberedKey = removeNumber(key);
                if (keysLists.get(unnumberedKey) == null) {
                    keyList = new ArrayList<String>();
                    keyList.add(props.getProperty(key));
                    keysLists.put(unnumberedKey, keyList);
                    removeKeys.add(key);
                } else {
                    keyList = keysLists.get(unnumberedKey);
                    keyList.add(props.getProperty(key));
                    keysLists.put(unnumberedKey, keyList);
                    removeKeys.add(key);
                }
            }
        }

        // remove keys that where unnumbered
        Iterator removeKey = removeKeys.iterator();
        while (removeKey.hasNext()) {
            key = (String) removeKey.next();
            props.remove(key);
        }

        // put new unnumbered key values mapped by unnumber key with an s appended to it.
        Iterator newKeys = keysLists.keySet().iterator();
        String newKey = null;
        while (newKeys.hasNext()) {
            newKey = (String) newKeys.next();
            props.put(newKey + "s", keysLists.get(newKey));
        }
    }

    public static String removeNumber(final String numberedKey) {
        String unnumberedKey = numberedKey;
        int firstNumberIndex = unnumberedKey.length() - 1;
        while (Character.isDigit(unnumberedKey.charAt(firstNumberIndex))) {
            firstNumberIndex--;
        }
        unnumberedKey = unnumberedKey.substring(0, firstNumberIndex + 1);

        return unnumberedKey;
    }
}

Related

  1. transform(final List l)
  2. transform(List> values)
  3. transformCollectionTOList(Collection collection)
  4. transformContinuouslyVariablesByThreshold(List> data, int index, int threshold)
  5. transformIntoAList(String... elements)
  6. transformStringListToString(List fileContentAsList)
  7. transformStringToListOfStrings(String line)
  8. transformToDoublePrimitiveArray( List values)
  9. transformToJsonArray(List objectsWithJsonToString)