Improve KeyStatement implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / key / KeyStatementSupport.java
index 8a089f77620964b8a648cc3daaa882896dd344fc..fbdd5784a2ef63709c9a25f078f1269c29872d59 100644 (file)
@@ -7,26 +7,31 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.key;
 
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet.Builder;
 import java.util.Collection;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
-import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public final class KeyStatementSupport
-        extends AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
-                EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
+        extends BaseStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> {
     private static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.KEY)
@@ -46,7 +51,7 @@ public final class KeyStatementSupport
         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
         int tokens = 0;
         for (String keyToken : LIST_KEY_SPLITTER.split(value)) {
-            builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.qnameFromArgument(ctx, keyToken)));
+            builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.parseNodeIdentifier(ctx, keyToken)));
             tokens++;
         }
 
@@ -59,15 +64,14 @@ public final class KeyStatementSupport
 
     @Override
     public Collection<SchemaNodeIdentifier> adaptArgumentValue(
-            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
-                EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
+            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
             final QNameModule targetModule) {
         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
         boolean replaced = false;
-        for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
+        for (final SchemaNodeIdentifier arg : ctx.coerceStatementArgument()) {
             final QName qname = arg.getLastComponent();
             if (!targetModule.equals(qname.getModule())) {
-                final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class, qname.withModule(targetModule));
+                final QName newQname = qname.withModule(targetModule).intern();
                 builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
                 replaced = true;
             } else {
@@ -81,19 +85,50 @@ public final class KeyStatementSupport
     }
 
     @Override
-    public KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
-        return new KeyStatementImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
-            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
-                    EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
-        return new KeyEffectiveStatementImpl(ctx);
+    protected KeyStatement createDeclared(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularKeyStatement(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected KeyStatement createEmptyDeclared(
+            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> ctx) {
+        return new EmptyKeyStatement(ctx);
+    }
+
+    @Override
+    protected KeyEffectiveStatement createEffective(
+            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
+            final KeyStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
+        return arg.equals(declared.argument()) ? new RegularLocalKeyEffectiveStatement(declared, substatements)
+                : new RegularForeignKeyEffectiveStatement(declared, arg, substatements);
+    }
+
+    @Override
+    protected KeyEffectiveStatement createEmptyEffective(
+            final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, KeyEffectiveStatement> ctx,
+            final KeyStatement declared) {
+        final Collection<SchemaNodeIdentifier> arg = ctx.coerceStatementArgument();
+        return arg.equals(declared.argument()) ? new EmptyLocalKeyEffectiveStatement(declared)
+                : new EmptyForeignKeyEffectiveStatement(declared, arg);
+    }
+
+    static @NonNull Object maskCollection(final @NonNull Collection<SchemaNodeIdentifier> coll) {
+        return coll.size() == 1 ? verifyNotNull(coll.iterator().next()) : coll;
+    }
+
+    @SuppressWarnings("unchecked")
+    static @NonNull Collection<SchemaNodeIdentifier> unmaskCollection(final @NonNull Object masked) {
+        if (masked instanceof Collection) {
+            return (Collection<SchemaNodeIdentifier>) masked;
+        }
+        verify(masked instanceof SchemaNodeIdentifier, "Unexpected argument %s", masked);
+        return ImmutableSet.of((SchemaNodeIdentifier) masked);
     }
-}
\ No newline at end of file
+}