BUG-582: optimize replaceWithCamelCase 15/7815/3
authorRobert Varga <rovarga@cisco.com>
Sun, 8 Jun 2014 10:23:13 +0000 (12:23 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 18 Jun 2014 09:43:47 +0000 (11:43 +0200)
Optimize replaceWithCamelCase() by not instantiating a string just to do
a toUppserCase.

Also the initial character lookup is done on the original string, such
that we can elide any object instantiation if the character to be
removed is not found.

Change-Id: Iee81f1967d47a63668bf14b319a2f46715e6866b
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java

index 0a8be971858304b3a21d60bafc8dfa32fa6e029e..20a19a713bf879243edcf4c93b7b3403b0f1c2a4 100644 (file)
@@ -353,21 +353,25 @@ public final class BindingGeneratorUtil {
      *             if the length of the returning string has length 0
      */
     private static String replaceWithCamelCase(final String text, final char removalChar) {
+        int toBeRemovedPos = text.indexOf(removalChar);
+        if (toBeRemovedPos == -1) {
+            return text;
+        }
+
         StringBuilder sb = new StringBuilder(text);
         String toBeRemoved = String.valueOf(removalChar);
-
-        int toBeRemovedPos = sb.indexOf(toBeRemoved);
-        while (toBeRemovedPos != -1) {
+        do {
             sb.replace(toBeRemovedPos, toBeRemovedPos + 1, "");
             // check if 'toBeRemoved' character is not the only character in
             // 'text'
             if (sb.length() == 0) {
                 throw new IllegalArgumentException("The resulting string can not be empty");
             }
-            String replacement = String.valueOf(sb.charAt(toBeRemovedPos)).toUpperCase();
-            sb.setCharAt(toBeRemovedPos, replacement.charAt(0));
+            char replacement = Character.toUpperCase(sb.charAt(toBeRemovedPos));
+            sb.setCharAt(toBeRemovedPos, replacement);
             toBeRemovedPos = sb.indexOf(toBeRemoved);
-        }
+        } while (toBeRemovedPos != -1);
+
         return sb.toString();
     }