Turn ArgumentContextUtils into an abstract class 96/87696/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 02:54:28 +0000 (03:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 23:23:04 +0000 (00:23 +0100)
Having this as a non-enum is actually better, as we can defer
loading of version-specific subclasses as needed.

JIRA: YANGTOOLS-1079
Change-Id: Ie6024b9f551823cf8f3ac5b51d2563c0c816ab36
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/YangModelDependencyInfo.java

index 9e6bbcf0226330db61f770d127a3f60e872376ef..89cabc1d851a3bec4c568c67b3565529bfb70079 100644 (file)
@@ -25,11 +25,14 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReferenc
 /**
  * Utilities for dealing with YANG statement argument strings, encapsulated in ANTLR grammar's ArgumentContext.
  */
-enum ArgumentContextUtils {
+abstract class ArgumentContextUtils {
     /**
-     * YANG 1.0 version of strings, which were not completely clarified in RFC6020.
+     * YANG 1.0 version of strings, which were not completely clarified in
+     * <a href="https://tools.ietf.org/html/rfc6020#section-6.1.3">RFC6020</a>.
      */
-    RFC6020 {
+    private static final class RFC6020 extends ArgumentContextUtils {
+        private static final @NonNull RFC6020 INSTANCE = new RFC6020();
+
         @Override
         void checkDoubleQuoted(final String str, final StatementSourceReference ref) {
             // No-op
@@ -39,13 +42,18 @@ enum ArgumentContextUtils {
         void checkUnquoted(final String str, final StatementSourceReference ref) {
             // No-op
         }
-    },
+    }
+
     /**
-     * YANG 1.1 version of strings, which were clarified in RFC7950.
+     * YANG 1.1 version of strings, which were clarified in
+     * <a href="https://tools.ietf.org/html/rfc7950#section-6.1.3">RFC7950</a>.
      */
     // NOTE: the differences clarified lead to a proper ability to delegate this to ANTLR lexer, but that does not
     //       understand versions and needs to work with both.
-    RFC7950 {
+    private static final class RFC7950 extends ArgumentContextUtils {
+        private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
+        private static final @NonNull RFC7950 INSTANCE = new RFC7950();
+
         @Override
         void checkDoubleQuoted(final String str, final StatementSourceReference ref) {
             // FIXME: YANGTOOLS-1079: we should forward backslash to this method, so that it does not start from the
@@ -75,26 +83,34 @@ enum ArgumentContextUtils {
             SourceException.throwIf(ANYQUOTE_MATCHER.matchesAnyOf(str), ref,
                 "YANG 1.1: unquoted string (%s) contains illegal characters", str);
         }
-    };
+    }
 
     private static final CharMatcher WHITESPACE_MATCHER = CharMatcher.whitespace();
-    private static final CharMatcher ANYQUOTE_MATCHER = CharMatcher.anyOf("'\"");
     private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
     private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
     private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
     private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
 
+    private ArgumentContextUtils() {
+        // Hidden on purpose
+    }
+
     static @NonNull ArgumentContextUtils forVersion(final YangVersion version) {
         switch (version) {
             case VERSION_1:
-                return RFC6020;
+                return RFC6020.INSTANCE;
             case VERSION_1_1:
-                return RFC7950;
+                return RFC7950.INSTANCE;
             default:
                 throw new IllegalStateException("Unhandled version " + version);
         }
     }
 
+    // TODO: teach the only caller about versions, or provide common-enough idioms for its use case
+    static @NonNull ArgumentContextUtils rfc6020() {
+        return RFC6020.INSTANCE;
+    }
+
     /*
      * NOTE: this method we do not use convenience methods provided by generated parser code, but instead are making
      *       based on the grammar assumptions. While this is more verbose, it cuts out a number of unnecessary code,
index 7069616755790e6fab31b8a08b4f75f73986a3de..7e8d21345f7d07dc2c3d707283a50412d45719f2 100644 (file)
@@ -330,7 +330,7 @@ public abstract class YangModelDependencyInfo {
         final ArgumentContext arg = stmt.argument();
         checkArgument(arg != null, "Missing %s at %s", desc, ref);
         // TODO: we probably need to understand yang version first....
-        return ArgumentContextUtils.RFC6020.stringFromStringContext(arg, ref);
+        return ArgumentContextUtils.rfc6020().stringFromStringContext(arg, ref);
     }
 
     private static StatementSourceReference getReference(final SourceIdentifier source,