|
1 | 1 | package com.fasterxml.jackson.databind;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * A container class for implementations of the {@link EnumNamingStrategy} interface. |
| 5 | + * |
| 6 | + * @since 2.15 |
| 7 | + */ |
3 | 8 | public class EnumNamingStrategies {
|
4 | 9 |
|
5 |
| - public static final EnumNamingStrategy CAMEL_CASE = CamelCaseStrategy.INSTANCE; |
6 |
| - |
7 |
| - /** |
8 |
| - * no-op naming. Does nothing |
9 |
| - */ |
10 |
| - public static class NoOpEnumNamingStrategy implements EnumNamingStrategy { |
11 |
| - |
12 |
| - @Override |
13 |
| - public String translate(String value) { |
14 |
| - return value; |
15 |
| - } |
16 |
| - |
17 |
| - } |
| 10 | + private EnumNamingStrategies() {} |
18 | 11 |
|
19 | 12 | /**
|
20 | 13 | * <p>
|
21 |
| - * Used when external value is in conventional CamelCase. Examples are "numberValue", "namingStrategy", "theDefiniteProof". |
22 |
| - * First underscore prefix will always be removed. |
| 14 | + * An implementation of {@link EnumNamingStrategy} that converts enum names in the typical upper |
| 15 | + * snake case format to camel case format. This implementation follows three rules |
| 16 | + * described below. |
| 17 | + * |
| 18 | + * <ol> |
| 19 | + * <li>converts any character preceded by an underscore into upper case character, |
| 20 | + * regardless of its original case (upper or lower).</li> |
| 21 | + * <li>converts any character NOT preceded by an underscore into a lower case character, |
| 22 | + * regardless of its original case (upper or lower).</li> |
| 23 | + * <li>converts contiguous sequence of underscores into a single underscore.</li> |
| 24 | + * </ol> |
| 25 | + * |
| 26 | + * WARNING: Naming conversion conflicts caused by underscore usage should be handled by client. |
| 27 | + * e.g. Both <code>PEANUT_BUTTER</code>, <code>PEANUT__BUTTER</code> are converted into "peanutButter". |
| 28 | + * And "peanutButter" will be deserialized into enum with smaller <code>Enum.ordinal()</code> value. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * These rules result in the following example conversions from upper snakecase names |
| 32 | + * to camelcase names. |
| 33 | + * <ul> |
| 34 | + * <li>"USER_NAME" is converted into "userName"</li> |
| 35 | + * <li>"USER______NAME" is converted into "userName"</li> |
| 36 | + * <li>"USERNAME" is converted into "username"</li> |
| 37 | + * <li>"User__Name" is converted into "userName"</li> |
| 38 | + * <li>"_user_name" is converted into "UserName"</li> |
| 39 | + * <li>"_user_name_s" is converted into "UserNameS"</li> |
| 40 | + * <li>"__Username" is converted into "Username"</li> |
| 41 | + * <li>"__username" is converted into "Username"</li> |
| 42 | + * <li>"username" is converted into "username"</li> |
| 43 | + * <li>"Username" is converted into "username"</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * @since 2.15 |
23 | 47 | */
|
24 | 48 | public static class CamelCaseStrategy implements EnumNamingStrategy {
|
25 | 49 |
|
26 | 50 | /**
|
| 51 | + * An intance of {@link CamelCaseStrategy} for reuse. |
| 52 | + * |
27 | 53 | * @since 2.15
|
28 | 54 | */
|
29 |
| - public final static CamelCaseStrategy INSTANCE |
30 |
| - = new CamelCaseStrategy(); |
| 55 | + public static final CamelCaseStrategy INSTANCE = new CamelCaseStrategy(); |
31 | 56 |
|
| 57 | + /** |
| 58 | + * @since 2.15 |
| 59 | + */ |
32 | 60 | @Override
|
33 |
| - public String translate(String input) { |
34 |
| - if (input == null) { |
35 |
| - return input; |
| 61 | + public String convertEnumToExternalName(String enumName) { |
| 62 | + if (enumName == null) { |
| 63 | + return null; |
36 | 64 | }
|
37 | 65 |
|
38 |
| - int length = input.length(); |
39 |
| - StringBuilder result = new StringBuilder(length * 2); |
40 |
| - int resultLength = 0; |
41 |
| - boolean wasPrevTranslated = false; |
42 |
| - for (int i = 0; i < length; i++) { |
43 |
| - char c = input.charAt(i); |
44 |
| - if (i > 0 || c != '_') { |
45 |
| - if (Character.isUpperCase(c)) { |
46 |
| - if (!wasPrevTranslated && resultLength > 0 && result.charAt(resultLength - 1) != '_') { |
47 |
| - result.append('_'); |
48 |
| - resultLength++; |
49 |
| - } |
50 |
| - c = Character.toLowerCase(c); |
51 |
| - wasPrevTranslated = true; |
| 66 | + final String UNDERSCORE = "_"; |
| 67 | + StringBuilder out = null; |
| 68 | + int iterationCnt = 0; |
| 69 | + int lastSeparatorIdx = -1; |
| 70 | + |
| 71 | + do { |
| 72 | + lastSeparatorIdx = indexIn(enumName, lastSeparatorIdx + 1); |
| 73 | + if (lastSeparatorIdx != -1) { |
| 74 | + if (iterationCnt == 0) { |
| 75 | + out = new StringBuilder(enumName.length() + 4 * UNDERSCORE.length()); |
| 76 | + out.append(toLowerCase(enumName.substring(iterationCnt, lastSeparatorIdx))); |
52 | 77 | } else {
|
53 |
| - wasPrevTranslated = false; |
| 78 | + out.append(normalizeWord(enumName.substring(iterationCnt, lastSeparatorIdx))); |
54 | 79 | }
|
55 |
| - result.append(c); |
56 |
| - resultLength++; |
| 80 | + iterationCnt = lastSeparatorIdx + UNDERSCORE.length(); |
| 81 | + } |
| 82 | + } while (lastSeparatorIdx != -1); |
| 83 | + |
| 84 | + if (iterationCnt == 0) { |
| 85 | + return toLowerCase(enumName); |
| 86 | + } |
| 87 | + out.append(normalizeWord(enumName.substring(iterationCnt))); |
| 88 | + return out.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + private static int indexIn(CharSequence sequence, int start) { |
| 92 | + int length = sequence.length(); |
| 93 | + for (int i = start; i < length; i++) { |
| 94 | + if ('_' == sequence.charAt(i)) { |
| 95 | + return i; |
57 | 96 | }
|
58 | 97 | }
|
59 |
| - String output = resultLength > 0 ? result.toString() : input; |
60 |
| - return output.toUpperCase(); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + private static String normalizeWord(String word) { |
| 102 | + int length = word.length(); |
| 103 | + if (length == 0) { |
| 104 | + return word; |
| 105 | + } |
| 106 | + return new StringBuilder(length) |
| 107 | + .append(charToUpperCaseIfLower(word.charAt(0))) |
| 108 | + .append(toLowerCase(word.substring(1))) |
| 109 | + .toString(); |
| 110 | + } |
| 111 | + |
| 112 | + private static String toLowerCase(String string) { |
| 113 | + int length = string.length(); |
| 114 | + StringBuilder builder = new StringBuilder(length); |
| 115 | + for (int i = 0; i < length; i++) { |
| 116 | + builder.append(charToLowerCaseIfUpper(string.charAt(i))); |
| 117 | + } |
| 118 | + return builder.toString(); |
| 119 | + } |
| 120 | + |
| 121 | + private static char charToUpperCaseIfLower(char c) { |
| 122 | + return Character.isLowerCase(c) ? Character.toUpperCase(c) : c; |
| 123 | + } |
| 124 | + |
| 125 | + private static char charToLowerCaseIfUpper(char c) { |
| 126 | + return Character.isUpperCase(c) ? Character.toLowerCase(c) : c; |
61 | 127 | }
|
62 | 128 | }
|
63 | 129 | }
|
0 commit comments