Speed up whitespace replacement 29/68029/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 7 Feb 2018 15:50:05 +0000 (16:50 +0100)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 8 Feb 2018 23:07:41 +0000 (23:07 +0000)
Rather than compiling a pattern, pre-compile it once and keep it
around.

Change-Id: Ibb1879d2546f731daa25e536ef45f48bde671f4f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/util/YangTextTemplate.java

index bbb1463f6113df9c4eaa2dcc18559cecd1358140..8fe8c49aeeca12c360080b0c0798cc0be98f71ff 100644 (file)
@@ -12,6 +12,8 @@ import com.google.common.base.CharMatcher;
 import com.google.common.base.Strings;
 import com.google.common.collect.Iterables;
 import java.util.StringTokenizer;
+import java.util.regex.Pattern;
+import javax.annotation.RegEx;
 import org.opendaylight.yangtools.yang.common.QName;
 
 /**
@@ -20,6 +22,9 @@ import org.opendaylight.yangtools.yang.common.QName;
 @Beta
 public final class YangTextTemplate {
     private static final CharMatcher NEWLINE_OR_TAB = CharMatcher.anyOf("\n\t");
+    @RegEx
+    private static final String SPACES_REGEX = " +";
+    private static final Pattern SPACES_PATTERN = Pattern.compile(SPACES_REGEX);
 
     private YangTextTemplate() {
         throw new UnsupportedOperationException("Util class");
@@ -58,7 +63,7 @@ public final class YangTextTemplate {
         final StringBuilder lineBuilder = new StringBuilder();
         final String lineIndent = Strings.repeat(" ", nextLineIndent);
         final String textToFormat = NEWLINE_OR_TAB.removeFrom(text);
-        final String formattedText = textToFormat.replaceAll(" +", " ");
+        final String formattedText = SPACES_PATTERN.matcher(textToFormat).replaceAll(" ");
         final StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
 
         while (tokenizer.hasMoreElements()) {