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 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.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 * Specialization of {@link AbstractStatementSupport} for statements which carry a Boolean argument and are essentially
25 * context-independent.
27 * @param <D> Declared Statement representation
28 * @param <E> Effective Statement representation
31 public abstract class AbstractBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
32 E extends EffectiveStatement<Boolean, D>> extends AbstractStatementSupport<Boolean, D, E> {
33 private final @NonNull E emptyEffectiveFalse;
34 private final @NonNull E emptyEffectiveTrue;
35 private final @NonNull D emptyDeclaredFalse;
36 private final @NonNull D emptyDeclaredTrue;
38 protected AbstractBooleanStatementSupport(final StatementDefinition publicDefinition,
39 final E emptyEffectiveFalse, final E emptyEffectiveTrue, final StatementPolicy<Boolean, D> policy,
40 final YangParserConfiguration config, final @Nullable SubstatementValidator validator) {
41 super(publicDefinition, policy, config, validator);
42 this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
43 this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
44 emptyDeclaredFalse = requireNonNull(emptyEffectiveFalse.getDeclared());
45 emptyDeclaredTrue = requireNonNull(emptyEffectiveTrue.getDeclared());
49 public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
50 if ("true".equals(value)) {
52 } else if ("false".equals(value)) {
55 throw new SourceException(ctx, "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
56 getStatementName(), getArgumentDefinition().get().getArgumentName(), value);
61 public final String internArgument(final String rawArgument) {
62 if ("true".equals(rawArgument)) {
64 } else if ("false".equals(rawArgument)) {
72 protected final D createDeclared(final BoundStmtCtx<Boolean> ctx,
73 final ImmutableList<DeclaredStatement<?>> substatements) {
74 final Boolean argument = ctx.getArgument();
75 if (substatements.isEmpty()) {
76 return argument ? emptyDeclaredTrue : emptyDeclaredFalse;
78 return createDeclared(argument, substatements);
81 protected abstract @NonNull D createDeclared(@NonNull Boolean argument,
82 @NonNull ImmutableList<DeclaredStatement<?>> substatements);
85 protected final E createEffective(final Current<Boolean, D> stmt,
86 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
87 return substatements.isEmpty() ? createEmptyEffective(stmt) : createEffective(stmt.declared(), substatements);
90 protected abstract @NonNull E createEffective(@NonNull D declared,
91 ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
93 protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
95 private @NonNull E createEmptyEffective(final Current<Boolean, D> stmt) {
96 final D declared = stmt.declared();
97 if (emptyDeclaredTrue.equals(declared)) {
98 return emptyEffectiveTrue;
99 } else if (emptyDeclaredFalse.equals(declared)) {
100 return emptyEffectiveFalse;
102 return createEmptyEffective(declared);