Reformulate StatementContextFactory.createEffective()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / status / StatusStatementSupport.java
index b63f94d44a781b891ec4630f040fe24373dc9fe3..10acc671f3bcfe0219836d3c6fbb8d7e72aa4f2b 100644 (file)
@@ -7,24 +7,46 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.status;
 
+import com.google.common.collect.ImmutableList;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.model.api.Status;
 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.StatusEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public final class StatusStatementSupport
-        extends AbstractStatementSupport<Status, StatusStatement, EffectiveStatement<Status, StatusStatement>> {
+        extends BaseStatementSupport<Status, StatusStatement, StatusEffectiveStatement> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .STATUS)
         .build();
     private static final StatusStatementSupport INSTANCE = new StatusStatementSupport();
 
+    /*
+     * status has low argument cardinality, hence we can reuse them in case declaration does not have any
+     * substatements (which is the usual case). Yeah, we could consider an EnumMap, but this is not too bad, either.
+     */
+    private static final @NonNull EmptyStatusStatement EMPTY_CURRENT_DECL =
+            new EmptyStatusStatement(Status.CURRENT);
+    private static final @NonNull EmptyStatusStatement EMPTY_DEPRECATED_DECL =
+            new EmptyStatusStatement(Status.DEPRECATED);
+    private static final @NonNull EmptyStatusStatement EMPTY_OBSOLETE_DECL =
+            new EmptyStatusStatement(Status.OBSOLETE);
+    private static final @NonNull EmptyStatusEffectiveStatement EMPTY_CURRENT_EFF =
+            new EmptyStatusEffectiveStatement(EMPTY_CURRENT_DECL);
+    private static final @NonNull EmptyStatusEffectiveStatement EMPTY_DEPRECATED_EFF =
+            new EmptyStatusEffectiveStatement(EMPTY_DEPRECATED_DECL);
+    private static final @NonNull EmptyStatusEffectiveStatement EMPTY_OBSOLETE_EFF =
+            new EmptyStatusEffectiveStatement(EMPTY_OBSOLETE_DECL);
+
     private StatusStatementSupport() {
-        super(YangStmtMapping.STATUS);
+        super(YangStmtMapping.STATUS, CopyPolicy.CONTEXT_INDEPENDENT);
     }
 
     public static StatusStatementSupport getInstance() {
@@ -46,18 +68,6 @@ public final class StatusStatementSupport
         }
     }
 
-    @Override
-    public StatusStatement createDeclared(
-            final StmtContext<Status, StatusStatement, ?> ctx) {
-        return new StatusStatementImpl(ctx);
-    }
-
-    @Override
-    public EffectiveStatement<Status, StatusStatement> createEffective(
-            final StmtContext<Status, StatusStatement, EffectiveStatement<Status, StatusStatement>> ctx) {
-        return new StatusEffectiveStatementImpl(ctx);
-    }
-
     @Override
     public String internArgument(final String rawArgument) {
         if ("current".equals(rawArgument)) {
@@ -75,4 +85,49 @@ public final class StatusStatementSupport
     protected SubstatementValidator getSubstatementValidator() {
         return SUBSTATEMENT_VALIDATOR;
     }
-}
\ No newline at end of file
+
+    @Override
+    protected StatusStatement createDeclared(final StmtContext<Status, StatusStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularStatusStatement(ctx.coerceStatementArgument(), substatements);
+    }
+
+    @Override
+    protected StatusStatement createEmptyDeclared(final StmtContext<Status, StatusStatement, ?> ctx) {
+        final Status argument = ctx.coerceStatementArgument();
+        switch (argument) {
+            case CURRENT:
+                return EMPTY_CURRENT_DECL;
+            case DEPRECATED:
+                return EMPTY_DEPRECATED_DECL;
+            case OBSOLETE:
+                return EMPTY_OBSOLETE_DECL;
+            default:
+                throw new IllegalStateException("Unhandled argument " + argument);
+        }
+    }
+
+    @Override
+    protected StatusEffectiveStatement createEffective(final Current<Status, StatusStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return substatements.isEmpty() ? createEmptyEffective(stmt.declared())
+            : new RegularStatusEffectiveStatement(stmt.declared(), substatements);
+    }
+
+    private static @NonNull StatusEffectiveStatement createEmptyEffective(final StatusStatement declared) {
+        // Aggressively reuse effective instances which are backed by the corresponding empty declared instance, as this
+        // is the case unless there is a weird extension in use.
+        if (EMPTY_DEPRECATED_DECL.equals(declared)) {
+            // Most likely to be seen (as current is the default)
+            return EMPTY_DEPRECATED_EFF;
+        } else if (EMPTY_OBSOLETE_DECL.equals(declared)) {
+            // less likely
+            return EMPTY_OBSOLETE_EFF;
+        } else if (EMPTY_CURRENT_DECL.equals(declared)) {
+            // ... okay, why is this there? :)
+            return EMPTY_CURRENT_EFF;
+        } else {
+            return new EmptyStatusEffectiveStatement(declared);
+        }
+    }
+}