01f8461af542d2cc42c2fd567f6e39f84f91b542
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractBooleanStatementSupport.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
24  * Specialization of {@link AbstractStatementSupport} for statements which carry a Boolean argument and are essentially
25  * context-independent.
26  *
27  * @param <D> Declared Statement representation
28  * @param <E> Effective Statement representation
29  */
30 @Beta
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;
37
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());
46     }
47
48     @Override
49     public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
50         if ("true".equals(value)) {
51             return Boolean.TRUE;
52         } else if ("false".equals(value)) {
53             return Boolean.FALSE;
54         } else {
55             throw new SourceException(ctx, "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
56                 getStatementName(), getArgumentDefinition().get().getArgumentName(), value);
57         }
58     }
59
60     @Override
61     public final String internArgument(final String rawArgument) {
62         if ("true".equals(rawArgument)) {
63             return "true";
64         } else if ("false".equals(rawArgument)) {
65             return "false";
66         } else {
67             return rawArgument;
68         }
69     }
70
71     @Override
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;
77         }
78         return createDeclared(argument, substatements);
79     }
80
81     protected abstract @NonNull D createDeclared(@NonNull Boolean argument,
82         @NonNull ImmutableList<DeclaredStatement<?>> substatements);
83
84     @Override
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);
88     }
89
90     protected abstract @NonNull E createEffective(@NonNull D declared,
91         ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
92
93     protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
94
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;
101         } else {
102             return createEmptyEffective(declared);
103         }
104     }
105 }