class ParsingUtils {

    public static String parseJavaName(final String token, final int startIndex, final char[] stopCharacters) throws ParseException {
        if (startIndex < 0 || startIndex >= token.length()) {
            throw new IndexOutOfBoundsException("startIndex: " + startIndex + " exceeds token bounds: " + token);
        }
        final StringBuilder javaName = new StringBuilder();
        char currentChar;
        int currentIndex = startIndex;
        while (currentIndex < token.length()) {
            currentChar = token.charAt(currentIndex);
            if (ArrayUtils.contains(stopCharacters, currentChar)) {
                break;
            } else {
                javaName.append(currentChar);
            }
            currentIndex++;
        }

        if (javaName.length() == 0) {
            throw new ParseException("Expected java name was not found at position: " + startIndex, startIndex);
        } else if (!SourceVersion.isName(javaName)) {
            throw new ParseException("Invalid java name was found at position: " + startIndex, startIndex);
        }
        return javaName.toString();
    }

}