Fixup collections return implementations
[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 org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18
19 /**
20  * Specialization of {@link BaseStatementSupport} for Boolean statement arguments.
21  *
22  * @param <D> Declared Statement representation
23  * @param <E> Effective Statement representation
24  */
25 @Beta
26 public abstract class BaseBooleanStatementSupport<D extends DeclaredStatement<Boolean>,
27         E extends EffectiveStatement<Boolean, D>> extends BaseStatementSupport<Boolean, D, E> {
28     private final @NonNull E emptyEffectiveFalse;
29     private final @NonNull E emptyEffectiveTrue;
30     private final @NonNull D emptyDeclaredFalse;
31     private final @NonNull D emptyDeclaredTrue;
32
33     protected BaseBooleanStatementSupport(final StatementDefinition publicDefinition,
34             final E emptyEffectiveFalse, final E emptyEffectiveTrue) {
35         super(publicDefinition, CopyPolicy.CONTEXT_INDEPENDENT);
36         this.emptyEffectiveFalse = requireNonNull(emptyEffectiveFalse);
37         this.emptyEffectiveTrue = requireNonNull(emptyEffectiveTrue);
38         emptyDeclaredFalse = requireNonNull(emptyEffectiveFalse.getDeclared());
39         emptyDeclaredTrue = requireNonNull(emptyEffectiveTrue.getDeclared());
40     }
41
42     @Override
43     public final Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
44         return ArgumentUtils.parseBoolean(ctx, value);
45     }
46
47     @Override
48     public final String internArgument(final String rawArgument) {
49         return ArgumentUtils.internBoolean(rawArgument);
50     }
51
52     @Override
53     protected final D createEmptyDeclared(final StmtContext<Boolean, D, ?> ctx) {
54         return ctx.coerceStatementArgument() ? emptyDeclaredTrue : emptyDeclaredFalse;
55     }
56
57     @Override
58     protected final E createEmptyEffective(final StmtContext<Boolean, D, E> ctx, final D declared) {
59         if (emptyDeclaredTrue.equals(declared)) {
60             return emptyEffectiveTrue;
61         } else if (emptyDeclaredFalse.equals(declared)) {
62             return emptyEffectiveFalse;
63         } else {
64             return createEmptyEffective(declared);
65         }
66     }
67
68     protected abstract @NonNull E createEmptyEffective(@NonNull D declared);
69 }