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 Boolean statement arguments.
24 * @param <D> Declared Statement representation
25 * @param <E> Effective Statement representation
28 public abstract class BaseBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
29 E extends EffectiveStatement<Boolean, D>> extends BaseStatementSupport<Boolean, D, E> {
30 private final @NonNull E emptyEffectiveFalse;
31 private final @NonNull E emptyEffectiveTrue;
32 private final @NonNull D emptyDeclaredFalse;
33 private final @NonNull D emptyDeclaredTrue;
35 protected BaseBooleanStatementSupport(final StatementDefinition publicDefinition,
36 final E emptyEffectiveFalse, final E emptyEffectiveTrue) {
37 super(publicDefinition, CopyPolicy.CONTEXT_INDEPENDENT);
38 this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
39 this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
40 emptyDeclaredFalse = requireNonNull(emptyEffectiveFalse.getDeclared());
41 emptyDeclaredTrue = requireNonNull(emptyEffectiveTrue.getDeclared());
45 public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
46 return ArgumentUtils.parseBoolean(ctx, value);
50 public final String internArgument(final String rawArgument) {
51 return ArgumentUtils.internBoolean(rawArgument);
55 protected final D createEmptyDeclared(final StmtContext<Boolean, D, ?> ctx) {
56 return ctx.getArgument() ? emptyDeclaredTrue : emptyDeclaredFalse;
60 protected final E createEffective(final Current<Boolean, D> stmt,
61 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62 return substatements.isEmpty() ? createEmptyEffective(stmt) : createEffective(stmt.declared(), substatements);
65 protected abstract @NonNull E createEffective(@NonNull D declared,
66 ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
68 protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
70 private @NonNull E createEmptyEffective(final Current<Boolean, D> stmt) {
71 final D declared = stmt.declared();
72 if (emptyDeclaredTrue.equals(declared)) {
73 return emptyEffectiveTrue;
74 } else if (emptyDeclaredFalse.equals(declared)) {
75 return emptyEffectiveFalse;
77 return createEmptyEffective(declared);