如何在GWT中替换StringTokenzier(..,true)



我正在寻找一个GWT兼容的替代品,用于包含分隔符的StringTokenzier。该任务无法通过正则表达式解决,因为语法不是上下文无关的。

示例:提取泛型类型定义的第一级。所以对于List<String>, Map<Integer, Map<Character, Boolean>>, Set<List<Double>>,我想要一个包含三个项目的列表。 List<String>Map<Integer, Map<Character, Boolean>>Set<List<Double>>

精简的示例代码:

private static List<String> extractFirstLevel(String type) {
    List<String> res = new LinkedList<String>();
    StringTokenizer st = new StringTokenizer(type, "<>,", true);
    int nesting = 0;        // we are only interested in nesting 0
    String lastToken = "";
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.equals("<")) {
            nesting++;  // ignore till matching >, but keep track of additional <
            lastToken = lastToken + "<";
        } else if (token.equals(">")) {
            nesting--;  // up one level
            lastToken = lastToken + ">";
        } else if (token.equals(",")) {
            if (nesting == 0) {  // we are interested in the top level
                res.add(lastToken);
                lastToken = "";
            } else { // this is a , inside a < >, so we are not interested
                lastToken = lastToken + ", ";
            }
        } else {
            lastToken = lastToken + token.trim();
        }
    }
    res.add(lastToken);
    return res;
}

我最终迭代了字符串的字符:

private static List<String> extractFirstLevelNew(String type) {
    List<String> res = new LinkedList<String>();
    int start = 0;
    int nesting = 0;
    for (int i = 0; i < type.length(); i++) {
        char chr = type.charAt(i);
        if (chr == '<') {
            nesting++;
        } else if (chr == '>') {
            nesting--;
        } else if ((chr == ',') && (nesting == 0)) {
            res.add(type.substring(start, i).trim());
            start = i + 1;
        }
    }
    res.add(type.substring(start, type.length()).trim());
    return res;
}

相关内容

  • 没有找到相关文章

最新更新