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