Remove YangTextSnippetIterator.Quoting 31/101331/10
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 29 May 2022 18:52:07 +0000 (20:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 29 May 2022 21:38:02 +0000 (23:38 +0200)
Eliminate internal enum and inline the dispatch, eliminating the need
for a default case.

Change-Id: I1b728b5b1652bde5f044a9a4e57b30d53f27bfc2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-export/src/main/java/org/opendaylight/yangtools/yang/model/export/YangTextSnippetIterator.java

index 78a5e61f4dfbc36986d744396cc2c1f4c00f493c..9f974db2d5893f77b60aa621ad7d2710c6a46508 100644 (file)
@@ -82,25 +82,6 @@ final class YangTextSnippetIterator extends AbstractIterator<@NonNull String> {
         }
     }
 
-    private enum Quoting {
-        /**
-         * No quoting necessary.
-         */
-        NONE,
-        /**
-         * Argument is empty, quote an empty string.
-         */
-        EMPTY,
-        /**
-         * Quote on the same line.
-         */
-        SIMPLE,
-        /**
-         * Quote starting on next line.
-         */
-        MULTILINE;
-    }
-
     /*
      * We normally have up to 10 strings:
      *               <indent>
@@ -227,60 +208,41 @@ final class YangTextSnippetIterator extends AbstractIterator<@NonNull String> {
     private void addArgument(final StatementDefinition def, final @Nullable String arg) {
         if (arg == null) {
             // No argument, nothing to do
-            return;
-        }
+        } else if (arg.isEmpty()) {
+            // Argument is empty, quote an empty string.
+            strings.add(" \"\"");
+        } else if (QUOTE_MULTILINE_STATEMENTS.contains(def) || arg.indexOf('\n') != -1) {
+            // Quote starting on next line.
+            strings.add("\n");
+            addIndent();
+            strings.add(INDENT + '\"');
+
+            final Iterator<String> it = NEWLINE_SPLITTER.split(DQUOT_MATCHER.replaceFrom(arg, "\\\"")).iterator();
+            final String first = it.next();
+            if (!first.isEmpty()) {
+                strings.add(first);
+            }
 
-        switch (quoteKind(def, arg)) {
-            case EMPTY:
-                strings.add(" \"\"");
-                break;
-            case NONE:
-                strings.add(" ");
-                strings.add(arg);
-                break;
-            case SIMPLE:
-                strings.add(" \"");
-                strings.add(DQUOT_MATCHER.replaceFrom(arg, "\\\""));
-                strings.add("\"");
-                break;
-            case MULTILINE:
+            while (it.hasNext()) {
                 strings.add("\n");
-                addIndent();
-                strings.add(INDENT + '\"');
-
-                final Iterator<String> it = NEWLINE_SPLITTER.split(DQUOT_MATCHER.replaceFrom(arg, "\\\"")).iterator();
-                final String first = it.next();
-                if (!first.isEmpty()) {
-                    strings.add(first);
-                }
-
-                while (it.hasNext()) {
-                    strings.add("\n");
-                    final String str = it.next();
-                    if (!str.isEmpty()) {
-                        addIndent();
-                        strings.add(INDENT + ' ');
-                        strings.add(str);
-                    }
+                final String str = it.next();
+                if (!str.isEmpty()) {
+                    addIndent();
+                    strings.add(INDENT + ' ');
+                    strings.add(str);
                 }
-                strings.add("\"");
-                break;
-            default:
-                throw new IllegalStateException("Illegal quoting for " + def + " argument \"" + arg + "\"");
-        }
-    }
-
-    private static Quoting quoteKind(final StatementDefinition def, final String str) {
-        if (str.isEmpty()) {
-            return Quoting.EMPTY;
-        }
-        if (QUOTE_MULTILINE_STATEMENTS.contains(def) || str.indexOf('\n') != -1) {
-            return Quoting.MULTILINE;
-        }
-        if (NEED_QUOTE_MATCHER.matchesAnyOf(str) || str.contains("//") || str.contains("/*") || str.contains("*/")) {
-            return Quoting.SIMPLE;
+            }
+            strings.add("\"");
+        } else if (NEED_QUOTE_MATCHER.matchesAnyOf(arg) || arg.contains("//") || arg.contains("/*")
+            || arg.contains("*/")) {
+            // Quote on the same line.
+            strings.add(" \"");
+            strings.add(DQUOT_MATCHER.replaceFrom(arg, "\\\""));
+            strings.add("\"");
+        } else {
+            // No quoting necessary.
+            strings.add(" ");
+            strings.add(arg);
         }
-
-        return Quoting.NONE;
     }
 }