String (@NotNull String enumValue) { int n = enumValue.length(); StringBuilder builder = new StringBuilder(n + 4); boolean prevIsLowerCase = false; for (int i = 0; i < n; i++) { char c = enumValue.charAt(i); if (c == '-' || c == ' ') { builder.append('_'); continue; } if (Character.isUpperCase(c)) { // second check handle "CSPViolation" (transform to CSP_VIOLATION) if (prevIsLowerCase || (i != 0 && (i + 1) < n && Character.isLowerCase(enumValue.charAt(i + 1)))) { builder.append('_'); } builder.append(c); prevIsLowerCase = false; } else { builder.append(Character.toUpperCase(c)); prevIsLowerCase = true; } } return builder.toString(); }