2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 * Specialization of {@link BaseStatementSupport} for statements which carry a Boolean argument and are essentially
23 * context-independent.
25 * @param <D> Declared Statement representation
26 * @param <E> Effective Statement representation
29 public abstract class BaseBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
30 E extends EffectiveStatement<Boolean, D>> extends BaseStatementSupport<Boolean, D, E> {
31 private final @NonNull E emptyEffectiveFalse;
32 private final @NonNull E emptyEffectiveTrue;
33 private final @NonNull D emptyDeclaredFalse;
34 private final @NonNull D emptyDeclaredTrue;
36 protected BaseBooleanStatementSupport(final StatementDefinition publicDefinition,
37 final E emptyEffectiveFalse, final E emptyEffectiveTrue, final StatementPolicy<Boolean, D> policy) {
38 super(publicDefinition, policy);
39 this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
40 this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
41 emptyDeclaredFalse = requireNonNull(emptyEffectiveFalse.getDeclared());
42 emptyDeclaredTrue = requireNonNull(emptyEffectiveTrue.getDeclared());
46 public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47 return ArgumentUtils.parseBoolean(ctx, value);
51 public final String internArgument(final String rawArgument) {
52 return ArgumentUtils.internBoolean(rawArgument);
56 protected final D createEmptyDeclared(final StmtContext<Boolean, D, ?> ctx) {
57 return ctx.getArgument() ? emptyDeclaredTrue : emptyDeclaredFalse;
61 protected final E createEffective(final Current<Boolean, D> stmt,
62 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
63 return substatements.isEmpty() ? createEmptyEffective(stmt) : createEffective(stmt.declared(), substatements);
66 protected abstract @NonNull E createEffective(@NonNull D declared,
67 ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
69 protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
71 private @NonNull E createEmptyEffective(final Current<Boolean, D> stmt) {
72 final D declared = stmt.declared();
73 if (emptyDeclaredTrue.equals(declared)) {
74 return emptyEffectiveTrue;
75 } else if (emptyDeclaredFalse.equals(declared)) {
76 return emptyEffectiveFalse;
78 return createEmptyEffective(declared);