Java String Split splitTypeArguments(final String nestedTypes)

Here you can find the source of splitTypeArguments(final String nestedTypes)

Description

Splits the provided string of nested types into separate top-level instances.

License

Apache License

Parameter

Parameter Description
nestedTypes a parameter

Declaration

private static String[] splitTypeArguments(final String nestedTypes) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**// w ww  . ja va  2s  .  c o m
     * Splits the provided string of nested types into separate top-level
     * instances.
     *
     * @param nestedTypes
     *
     * @return
     */
    private static String[] splitTypeArguments(final String nestedTypes) {
        StringBuilder string = new StringBuilder(nestedTypes.replaceAll("\\s*", ""));
        List<String> arguments = new ArrayList<String>();
        while (string.length() > 0) {
            int nextComma = string.indexOf(",");
            int nextOpen = string.indexOf("<");
            if (nextComma == -1) {
                arguments.add(string.toString());
                string.setLength(0);
            } else if (nextOpen == -1 || nextComma < nextOpen) {
                arguments.add(string.substring(0, nextComma));
                string.replace(0, nextComma + 1, "");
            } else { // nextOpen < nextComma
                int depth = 1;
                int index = nextOpen;
                while (depth > 0 && index < string.length() - 1) {
                    char nextChar = string.charAt(++index);
                    if ('<' == nextChar) {
                        ++depth;
                    } else if ('>' == nextChar) {
                        --depth;
                    }
                }
                arguments.add(string.substring(0, index + 1));
                string.replace(0, index + 1, "");
            }
        }
        return arguments.toArray(new String[arguments.size()]);
    }
}

Related

  1. splitTokens(String text)
  2. splitToList(final String listString, final String split)
  3. splitToList(String s)
  4. splitToList(String valueString)
  5. splitToTopLevelJsonObjects(String s)
  6. splitTypes(String fullName)
  7. splitValues(String strValues)
  8. splitValues(String value)
  9. splitVersion(String version)