Do not force materialization when not needed
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / BaseBooleanStatementSupport.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.rfc7950.stmt;
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.meta.StmtContext;
20
21 /**
22  * Specialization of {@link BaseStatementSupport} for Boolean statement arguments.
23  *
24  * @param <D> Declared Statement representation
25  * @param <E> Effective Statement representation
26  */
27 @Beta
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;
34
35     protected BaseBooleanStatementSupport(final StatementDefinition publicDefinition,
36             final E emptyEffectiveFalse, final E emptyEffectiveTrue, final CopyPolicy copyPolicy) {
37         super(publicDefinition, copyPolicy);
38         this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
39         this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
40         emptyDeclaredFalse = requireNonNull(emptyEffectiveFalse.getDeclared());
41         emptyDeclaredTrue = requireNonNull(emptyEffectiveTrue.getDeclared());
42     }
43
44     @Override
45     public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
46         return ArgumentUtils.parseBoolean(ctx, value);
47     }
48
49     @Override
50     public final String internArgument(final String rawArgument) {
51         return ArgumentUtils.internBoolean(rawArgument);
52     }
53
54     @Override
55     protected final D createEmptyDeclared(final StmtContext<Boolean, D, ?> ctx) {
56         return ctx.getArgument() ? emptyDeclaredTrue : emptyDeclaredFalse;
57     }
58
59     @Override
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);
63     }
64
65     protected abstract @NonNull E createEffective(@NonNull D declared,
66         ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
67
68     protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
69
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;
76         } else {
77             return createEmptyEffective(declared);
78         }
79     }
80 }