From daac94092cb33fdb38595c1564b93613d3c5e01d Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 9 Apr 2010 03:29:58 +0300 Subject: [PATCH] improve camelcache and underscore conversion --- .../java/org/elasticsearch/util/Strings.java | 46 +++++++++++++++---- .../elasticsearch/util/json/JsonBuilder.java | 24 +++++----- .../org/elasticsearch/util/StringsTests.java | 6 +++ 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/Strings.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/Strings.java index 84053d90bc6..42d0eecd608 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/Strings.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/Strings.java @@ -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(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java index cc7abdd8dbb..9dc48baa513 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java @@ -118,7 +118,7 @@ public abstract class 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 { } 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 { } 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 { } 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 { } 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 { } 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 { } 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); } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/util/StringsTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/util/StringsTests.java index 1f63c305113..1c03b02bdee 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/util/StringsTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/util/StringsTests.java @@ -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 {