public static String camelCaseToConstantCase(String value) {
    if (value == null || "".equals(value)) return value;

    StringBuilder result = new StringBuilder();
    Matcher matcher = LOWER_UPPER.matcher(value);

    while (matcher.find()) {
      String lowers = matcher.group(1);
      String uppers = matcher.group(2);

      if (uppers.length() == 0) {
        result.append(lowers.toUpperCase(Locale.ROOT));
      } else {
        if (lowers.length() > 0) {
          result.append(lowers.toUpperCase(Locale.ROOT));
          result.append('_');
        }
        if (uppers.length() > 1 && !matcher.hitEnd()) {
          result.append(uppers.substring(0, uppers.length() - 1));
          result.append('_');
          result.append(uppers.charAt(uppers.length() - 1));
        } else {
          result.append(uppers);
        }
      }
    }

    return result.toString();
  }
Returns the number of whitespace characters at the end of the given line.

@param line
@return
spock-core/src/main/java/org/spockframework/util/TextUtil.java