improve camelcache and underscore conversion
This commit is contained in:
parent
2fd36fdbf8
commit
daac94092c
|
@ -1167,28 +1167,54 @@ public class Strings {
|
|||
|
||||
public static String toCamelCase(String value) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char[] values = value.toCharArray();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == '_') {
|
||||
sb.append(Character.toUpperCase(values[++i]));
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c == '_') {
|
||||
if (!changed) {
|
||||
// copy it over here
|
||||
for (int j = 0; j < i; j++) {
|
||||
sb.append(value.charAt(j));
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
sb.append(Character.toUpperCase(value.charAt(++i)));
|
||||
} else {
|
||||
sb.append(values[i]);
|
||||
if (changed) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!changed) {
|
||||
return value;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String toUnderscoreCase(String value) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char[] values = value.toCharArray();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (Character.isUpperCase(values[i])) {
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (Character.isUpperCase(c)) {
|
||||
if (!changed) {
|
||||
// copy it over here
|
||||
for (int j = 0; j < i; j++) {
|
||||
sb.append(value.charAt(j));
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
sb.append('_');
|
||||
sb.append(Character.toLowerCase(values[i]));
|
||||
sb.append(Character.toLowerCase(c));
|
||||
} else {
|
||||
sb.append(values[i]);
|
||||
if (changed) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!changed) {
|
||||
return value;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -118,7 +118,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, char[] value, int offset, int length) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
if (value == null) {
|
||||
generator.writeNull();
|
||||
} else {
|
||||
|
@ -128,7 +128,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, String value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
if (value == null) {
|
||||
generator.writeNull();
|
||||
} else {
|
||||
|
@ -142,7 +142,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, int value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeNumber(value);
|
||||
return builder;
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, long value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeNumber(value);
|
||||
return builder;
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, float value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeNumber(value);
|
||||
return builder;
|
||||
}
|
||||
|
@ -173,7 +173,7 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, double value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeNumber(value);
|
||||
return builder;
|
||||
}
|
||||
|
@ -209,34 +209,34 @@ public abstract class JsonBuilder<T extends JsonBuilder> {
|
|||
}
|
||||
|
||||
public T field(String name, boolean value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeBoolean(value);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public T field(String name, byte[] value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
generator.writeBinary(value);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public T field(String name, ReadableInstant date) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
return value(date);
|
||||
}
|
||||
|
||||
public T field(String name, ReadableInstant date, DateTimeFormatter formatter) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
return value(date, formatter);
|
||||
}
|
||||
|
||||
public T field(String name, Date date) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
return value(date);
|
||||
}
|
||||
|
||||
public T field(String name, Date date, DateTimeFormatter formatter) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
field(name);
|
||||
return value(date, formatter);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,10 +34,16 @@ public class StringsTests {
|
|||
|
||||
@Test public void testCamelCase() {
|
||||
assertThat(toCamelCase("test_value"), equalTo("testValue"));
|
||||
String testValue = "testValue";
|
||||
assertThat(toCamelCase(testValue), equalTo(testValue));
|
||||
assertThat(toCamelCase(testValue), sameInstance(testValue));
|
||||
}
|
||||
|
||||
@Test public void testUnderscoreCase() {
|
||||
assertThat(toUnderscoreCase("testValue"), equalTo("test_value"));
|
||||
String testValue = "test_value";
|
||||
assertThat(toUnderscoreCase(testValue), equalTo(testValue));
|
||||
assertThat(toUnderscoreCase(testValue), sameInstance(testValue));
|
||||
}
|
||||
|
||||
// @Test public void testHasTextBlank() throws Exception {
|
||||
|
|
Loading…
Reference in New Issue