CaseSyntax { public static CaseSyntax ofExample(String example, boolean standardize) { Objects.requireNonNull(example, "example"); CharIterator charIterator = new SequenceCharIterator(example); Character separator = null; CaseConversion other = null; CaseConversion wordStart = null; char c = charIterator.next(); if (c == '_') { separator = KEEP_SPECIAL_CHARS; c = charIterator.next(); } CaseConversion first = CaseConversion.ofExample(c, false); CharClass previousClass = CharClass.of(c); CaseConversion previousCase = first; char previousChar = c; int index = 0; int letterOrDollarCount = previousClass.isLetterOrDollar() ? 1 : 0; while (charIterator.hasNext()) { index++; c = charIterator.next(); CharClass currentClass = CharClass.of(c); CaseConversion currentCase = CaseConversion.ofExample(c, false); switch (currentClass) { case DOLLAR: letterOrDollarCount++; if ((other == null) && (index < 3)) { other = CaseConversion.ORIGINAL_CASE; } else if ((wordStart == null) && (index < 3)) { wordStart = CaseConversion.ORIGINAL_CASE; } else { throw new IllegalArgumentException(example + ": the special character '" + CaseConversion.EXAMPLE_CHAR_FOR_ORIGINAL_CASE + "' is only allowed twice within the first three characters or three times at the beginning!"); } break; case LETTER: letterOrDollarCount++; boolean wordStarted = false; if (previousClass == CharClass.SEPARATOR) { if (wordStart == null) { wordStart = currentCase; } wordStarted = true; } else if ((other == null) && (wordStart == null)) { other = currentCase; } else if ((currentCase != previousCase) && (currentCase != other)) { if (wordStart == null) { wordStart = currentCase; } wordStarted = true; } else if (CaseConversion.areIncompatible(currentCase, other)) { throw new IllegalArgumentException( example + ": the two characters '" + previousChar + c + "' both have case " + currentCase + " but other case was detected as " + other + "!"); } if (wordStarted && CaseConversion.areIncompatible(currentCase, wordStart)) { throw new IllegalArgumentException(example + ": different word start cases " + wordStart + " and " + currentCase + "!"); } break; case SEPARATOR: if (separator == null) { if (wordStart != null) { throw new IllegalArgumentException(example + ": word separator '" + c + "' detected but word start was already detected via case change!"); } separator = Character.valueOf(c); } else if (c != separator.charValue()) { String sep; if (separator == KEEP_SPECIAL_CHARS) { sep = "'\0' (_)"; } else { sep = "'" + separator + "'"; } throw new IllegalArgumentException(example + ": different word separators " + sep + " and '" + c + "'!"); } else if (previousClass == CharClass.SEPARATOR) { throw new IllegalArgumentException(example + ": duplicate word separators '" + separator + separator + "'!"); } break; case DIGIT: default : break; } if (currentClass != CharClass.DIGIT) { previousCase = currentCase; previousClass = currentClass; } previousChar = c; } if (letterOrDollarCount < 3) { throw new IllegalArgumentException(example + ": at least 3 letters are required!"); } if (wordStart == null) { wordStart = other; } else if (other == null) { other = wordStart; } CaseSyntax syntax = null; if (standardize) { syntax = ofStandardized(separator, first, wordStart, other); } if (syntax == null) { syntax = new CaseSyntax(separator, first, wordStart, other, example, false); } return syntax; } private  CaseSyntax(Character separator, CaseConversion allCharCase, String example); private  CaseSyntax(Character separator, CaseConversion firstCharCase, CaseConversion wordStartCharCase, String example); private  CaseSyntax(Character separator, CaseConversion firstCharCase, CaseConversion wordStartCharCase, CaseConversion otherCharCase, String example); private  CaseSyntax(Character separator, CaseConversion firstCharCase, CaseConversion wordStartCharCase, CaseConversion otherCharCase, String example,
      boolean register); Character getWordSeparator(); boolean hasWordSeparator(); CaseConversion getFirstCase(); CaseConversion getWordStartCase(); CaseConversion getOtherCase(); String convert(String string); String convert(String string, Locale locale); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); static CaseSyntax of(Character separator, CaseConversion allCharCase); static CaseSyntax of(Character separator, CaseConversion firstCharCase, CaseConversion wordStartCharCase); static CaseSyntax of(Character separator, CaseConversion firstCharCase, CaseConversion wordStartCharCase, CaseConversion otherCharCase); static String normalizeExample(String example); static CaseSyntax ofExample(String example, boolean standardize); static final Character KEEP_SPECIAL_CHARS; static final CaseSyntax LOWERCASE; static final CaseSyntax UPPERCASE; static final CaseSyntax CAML_CASE; static final CaseSyntax PASCAL_CASE; static final CaseSyntax TRAIN_CASE; static final CaseSyntax UPPER_TRAIN_CASE; static final CaseSyntax PASCAL_TRAIN_CASE; static final CaseSyntax CAML_TRAIN_CASE; static final CaseSyntax LOWER_SNAKE_CASE; static final CaseSyntax UPPER_SNAKE_CASE; static final CaseSyntax PASCAL_SNAKE_CASE; static final CaseSyntax CAML_SNAKE_CASE; static final CaseSyntax LOWER_SPACE_CASE; static final CaseSyntax UPPER_SPACE_CASE; static final CaseSyntax PASCAL_SPACE_CASE; static final CaseSyntax CAML_SPACE_CASE; static final CaseSyntax UNMODIFIED; static final CaseSyntax CAPITALIZED; static final CaseSyntax UNCAPITALIZED; static final CaseSyntax CAPITALIZED_LOWER; static final CaseSyntax UNCAPITALIZED_UPPER; }
@Test public void testEqualsAndHashCode() { ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.ofExample("myCase", false), true); ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.PASCAL_CASE, false); ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.TRAIN_CASE, false); ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.UPPERCASE, false); ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.LOWERCASE, false); ObjectHelper.checkEqualsAndHashCode(CaseSyntax.CAML_CASE, CaseSyntax.UNCAPITALIZED_UPPER, false); }