*/
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() {
}
}
- @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)) {
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);
+ }
+ }
+}