Refactor uppercaseFirstLetter, add additional field to test (#1515)

This commit is contained in:
Degubi 2019-04-27 00:49:22 +02:00 committed by inder123
parent c5a3f21fba
commit 63ee47cb64
2 changed files with 17 additions and 27 deletions

View File

@ -159,31 +159,20 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* Ensures the JSON field names begins with an upper case letter. * Ensures the JSON field names begins with an upper case letter.
*/ */
static String upperCaseFirstLetter(String name) { static String upperCaseFirstLetter(String name) {
StringBuilder fieldNameBuilder = new StringBuilder(); int firstLetterIndex = 0;
int index = 0; int limit = name.length() - 1;
char firstCharacter = name.charAt(index); for(; !Character.isLetter(name.charAt(firstLetterIndex)) && firstLetterIndex < limit; ++firstLetterIndex);
int length = name.length();
while (index < length - 1) { char firstLetter = name.charAt(firstLetterIndex);
if (Character.isLetter(firstCharacter)) { if(Character.isUpperCase(firstLetter)) { //The letter is already uppercased, return the original
break;
}
fieldNameBuilder.append(firstCharacter);
firstCharacter = name.charAt(++index);
}
if (!Character.isUpperCase(firstCharacter)) {
String modifiedTarget = modifyString(Character.toUpperCase(firstCharacter), name, ++index);
return fieldNameBuilder.append(modifiedTarget).toString();
} else {
return name; return name;
} }
}
private static String modifyString(char firstCharacter, String srcString, int indexOfSubstring) { char uppercased = Character.toUpperCase(firstLetter);
return (indexOfSubstring < srcString.length()) if(firstLetterIndex == 0) { //First character in the string is the first letter, saves 1 substring
? firstCharacter + srcString.substring(indexOfSubstring) return uppercased + name.substring(1);
: String.valueOf(firstCharacter); }
return name.substring(0, firstLetterIndex) + uppercased + name.substring(firstLetterIndex + 1);
} }
} }

View File

@ -33,7 +33,7 @@ public final class FieldNamingTest extends TestCase {
Gson gson = getGsonWithNamingPolicy(IDENTITY); Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'lowerId':8}", "'annotatedName':7,'lowerId':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
@ -41,7 +41,7 @@ public final class FieldNamingTest extends TestCase {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'LowerId':8}", "'annotatedName':7,'LowerId':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
@ -49,7 +49,7 @@ public final class FieldNamingTest extends TestCase {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," +
"'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," + "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
"'annotatedName':7,'Lower Id':8}", "'annotatedName':7,'Lower Id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
@ -57,7 +57,7 @@ public final class FieldNamingTest extends TestCase {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," +
"'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," + "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
"'annotatedName':7,'lower_id':8}", "'annotatedName':7,'lower_id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
@ -65,7 +65,7 @@ public final class FieldNamingTest extends TestCase {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," +
"'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," + "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
"'annotatedName':7,'lower-id':8}", "'annotatedName':7,'lower-id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
@ -85,5 +85,6 @@ public final class FieldNamingTest extends TestCase {
int UPPER_WORDS = 6; int UPPER_WORDS = 6;
@SerializedName("annotatedName") int annotated = 7; @SerializedName("annotatedName") int annotated = 7;
int lowerId = 8; int lowerId = 8;
int _9 = 9;
} }
} }