BUG-987: optimize illegal character search 12/6912/1
authorRobert Varga <rovarga@cisco.com>
Mon, 12 May 2014 22:36:44 +0000 (00:36 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 12 May 2014 22:36:44 +0000 (00:36 +0200)
As it turns out, we are not search for a substring, but rather for
characters. Move the array into static field so we do not allocate it
needlessly over and over. And make it a char array to save space and
increase checking speed.

Change-Id: I307813ad363441cf8ee63d383896e5fa6d2fb6e6
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java

index be7d194681f4457d68cb359135885da018d748d2..cfadd8f81959341f85d139e7c7715afce1672ab9 100644 (file)
@@ -57,6 +57,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile(
             "^(.+)$");
 
+    private static final char[] ILLEGAL_CHARACTERS = new char[] {'?', '(', ')', '&'};
+
     //Nullable
     private final URI namespace;
     //Mandatory
@@ -111,12 +113,12 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         if (localName.length() == 0) {
             throw new IllegalArgumentException("Parameter 'localName' must be a non-empty string.");
         }
-        String [] illegalSubstrings = new String[] {"?", "(", ")", "&"};
-        for(String illegalSubstring: illegalSubstrings) {
-            if (localName.contains(illegalSubstring)) {
+
+        for (char c: ILLEGAL_CHARACTERS) {
+            if (localName.indexOf(c) != -1) {
                 throw new IllegalArgumentException(String.format(
-                        "Parameter 'localName':'%s' contains illegal sequence '%s'",
-                        localName, illegalSubstring));
+                        "Parameter 'localName':'%s' contains illegal character '%s'",
+                        localName, c));
             }
         }
         return localName;