Un-deprecate CopyableNode, AddedByUsesAware
[yangtools.git] / yang / 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.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.source.SourceException;
20
21 /**
22  * Specialization of {@link AbstractStatementSupport} for statements which carry a Boolean argument and are essentially
23  * context-independent.
24  *
25  * @param <D> Declared Statement representation
26  * @param <E> Effective Statement representation
27  */
28 @Beta
29 public abstract class AbstractBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
30         E extends EffectiveStatement<Boolean, D>> extends AbstractStatementSupport<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;
35
36     protected AbstractBooleanStatementSupport(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());
43     }
44
45     @Override
46     public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47         if ("true".equals(value)) {
48             return Boolean.TRUE;
49         } else if ("false".equals(value)) {
50             return Boolean.FALSE;
51         } else {
52             throw new SourceException(ctx, "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
53                 getStatementName(), getArgumentDefinition().get().getArgumentName(), value);
54         }
55     }
56
57     @Override
58     public final String internArgument(final String rawArgument) {
59         if ("true".equals(rawArgument)) {
60             return "true";
61         } else if ("false".equals(rawArgument)) {
62             return "false";
63         } else {
64             return rawArgument;
65         }
66     }
67
68     @Override
69     protected final D createEmptyDeclared(final StmtContext<Boolean, D, ?> ctx) {
70         return ctx.getArgument() ? emptyDeclaredTrue : emptyDeclaredFalse;
71     }
72
73     @Override
74     protected final E createEffective(final Current<Boolean, D> stmt,
75             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
76         return substatements.isEmpty() ? createEmptyEffective(stmt) : createEffective(stmt.declared(), substatements);
77     }
78
79     protected abstract @NonNull E createEffective(@NonNull D declared,
80         ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
81
82     protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
83
84     private @NonNull E createEmptyEffective(final Current<Boolean, D> stmt) {
85         final D declared = stmt.declared();
86         if (emptyDeclaredTrue.equals(declared)) {
87             return emptyEffectiveTrue;
88         } else if (emptyDeclaredFalse.equals(declared)) {
89             return emptyEffectiveFalse;
90         } else {
91             return createEmptyEffective(declared);
92         }
93     }
94 }