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.spi.meta;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 * Specialization of {@link AbstractStatementSupport} for statements which carry a Boolean argument and are essentially
26 * context-independent.
28 * @param <D> Declared Statement representation
29 * @param <E> Effective Statement representation
32 public abstract class AbstractBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
33 E extends EffectiveStatement<Boolean, D>> extends AbstractStatementSupport<Boolean, D, E> {
34 private final @NonNull E emptyEffectiveFalse;
35 private final @NonNull E emptyEffectiveTrue;
36 private final @NonNull D emptyDeclaredFalse;
37 private final @NonNull D emptyDeclaredTrue;
39 protected AbstractBooleanStatementSupport(final StatementDefinition publicDefinition,
40 final E emptyEffectiveFalse, final E emptyEffectiveTrue, final StatementPolicy<Boolean, D> policy,
41 final YangParserConfiguration config, final @Nullable SubstatementValidator validator) {
42 super(publicDefinition, policy, config, validator);
43 this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
44 this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
45 emptyDeclaredFalse = verifyNotNull(emptyEffectiveFalse.getDeclared());
46 emptyDeclaredTrue = verifyNotNull(emptyEffectiveTrue.getDeclared());
50 public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51 if ("true".equals(value)) {
53 } else if ("false".equals(value)) {
56 throw new SourceException(ctx, "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
57 getStatementName(), getArgumentDefinition().get().getArgumentName(), value);
62 public final String internArgument(final String rawArgument) {
63 if ("true".equals(rawArgument)) {
65 } else if ("false".equals(rawArgument)) {
73 protected final D createDeclared(final BoundStmtCtx<Boolean> ctx,
74 final ImmutableList<DeclaredStatement<?>> substatements) {
75 final Boolean argument = ctx.getArgument();
76 if (substatements.isEmpty()) {
77 return argument ? emptyDeclaredTrue : emptyDeclaredFalse;
79 return createDeclared(argument, substatements);
82 protected abstract @NonNull D createDeclared(@NonNull Boolean argument,
83 @NonNull ImmutableList<DeclaredStatement<?>> substatements);
86 protected final E createEffective(final Current<Boolean, D> stmt,
87 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
88 return substatements.isEmpty() ? createEmptyEffective(stmt) : createEffective(stmt.declared(), substatements);
91 protected abstract @NonNull E createEffective(@NonNull D declared,
92 ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
94 protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
96 private @NonNull E createEmptyEffective(final Current<Boolean, D> stmt) {
97 final D declared = stmt.declared();
98 if (emptyDeclaredTrue.equals(declared)) {
99 return emptyEffectiveTrue;
100 } else if (emptyDeclaredFalse.equals(declared)) {
101 return emptyEffectiveFalse;
103 return createEmptyEffective(declared);