Java String Replace replaceEmitKeys(String map, Collection newKeys)

Here you can find the source of replaceEmitKeys(String map, Collection newKeys)

Description

replace Emit Keys

License

LGPL

Declaration

public static String replaceEmitKeys(String map, Collection<String> newKeys) 

Method Source Code


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

import java.util.Arrays;
import java.util.Collection;

public class Main {
    private static final String EMIT_TOKEN = "emit";
    private static final char COMA_SEPARATOR = ',';
    private static final char OPEN_TABLE_SEPARATOR = '[';
    private static final char CLOSE_TABLE_SEPARATOR = ']';
    private static final char OPEN_PARENTHESES_SEPARATOR = '(';

    public static String replaceEmitKeys(String map, Collection<String> newKeys) {
        Collection<String> oldKeys = getEmitKeys(map);
        return map.replace(buildString(oldKeys), buildString(newKeys));
    }/* w w w .j  a  v  a 2  s  . co  m*/

    public static Collection<String> getEmitKeys(String map) {
        String source = map;
        source = source.substring(source.indexOf(EMIT_TOKEN));
        if (firstSeparator(source) == OPEN_TABLE_SEPARATOR) {
            source = source.substring(source.indexOf(OPEN_TABLE_SEPARATOR) + 1,
                    source.indexOf(CLOSE_TABLE_SEPARATOR));
        } else {
            source = source.substring(source.indexOf(OPEN_PARENTHESES_SEPARATOR) + 1,
                    source.indexOf(COMA_SEPARATOR));
        }
        String[] properties = source.split(",");
        int i = 0;
        for (String property : properties) {
            properties[i++] = property.trim();
        }
        return Arrays.asList(properties);
    }

    private static String buildString(Collection<String> keys) {
        String build = keys.toString();
        if (keys.size() == 1) {
            build = build.replace("" + OPEN_TABLE_SEPARATOR, "").replace("" + CLOSE_TABLE_SEPARATOR, "");
        }
        return build;
    }

    private static char firstSeparator(String s) {
        int tbsIndex = s.indexOf(OPEN_TABLE_SEPARATOR);
        if (tbsIndex < 0)
            return COMA_SEPARATOR;
        int csIndex = s.indexOf(COMA_SEPARATOR);
        return tbsIndex < csIndex ? OPEN_TABLE_SEPARATOR : COMA_SEPARATOR;
    }
}

Related

  1. replace(String str)
  2. replace(String string, String pattern, String value)
  3. replace(String string, String pattern, String value)
  4. replace(String target, String replacement, String... args)
  5. replaceAll(String source, String toReplace, String replacement)
  6. replaceFirst(String input, String search, String replacement)
  7. replaceIgnoreCase(String line, String oldString, String newString)
  8. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)
  9. replaceNonPrintableAsciiCharacters(String str)