BUG-6522: share instances of Config(Effective)Statement
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / EmptyConfigStatement.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Collection;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
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.model.api.meta.StatementSource;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
19
20 abstract class EmptyConfigStatement implements ConfigStatement {
21     static final ConfigStatement FALSE = new EmptyConfigStatement() {
22         @Override
23         public boolean getValue() {
24             return false;
25         }
26
27         @Override
28         EffectiveStatement<Boolean, ConfigStatement> toEffective() {
29             return EmptyConfigEffectiveStatement.FALSE;
30         }
31     };
32
33     static final ConfigStatement TRUE = new EmptyConfigStatement() {
34         @Override
35         public boolean getValue() {
36             return true;
37         }
38
39         @Override
40         EffectiveStatement<Boolean, ConfigStatement> toEffective() {
41             return EmptyConfigEffectiveStatement.TRUE;
42         }
43     };
44
45     private EmptyConfigStatement() {
46         // Invisible on purpose
47     }
48
49     abstract EffectiveStatement<Boolean, ConfigStatement> toEffective();
50
51     @Override
52     public final Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
53         return ImmutableList.of();
54     }
55
56     @Override
57     public final StatementDefinition statementDefinition() {
58         return Rfc6020Mapping.CONFIG;
59     }
60
61     @Override
62     public final String rawArgument() {
63         return Boolean.toString(getValue());
64     }
65
66     @Override
67     public final Boolean argument() {
68         return Boolean.valueOf(getValue());
69     }
70
71     @Override
72     public final StatementSource getStatementSource() {
73         return StatementSource.DECLARATION;
74     }
75
76     @Override
77     public final int hashCode() {
78         return Objects.hash(statementDefinition(), getStatementSource(), argument(),
79             rawArgument(), declaredSubstatements(), getValue());
80     }
81
82     @Override
83     public final boolean equals(final Object obj) {
84         if (this == obj) {
85             return true;
86         }
87         if (!(obj instanceof ConfigStatement)) {
88             return false;
89         }
90
91         final ConfigStatement other = (ConfigStatement) obj;
92
93         return  getValue() == other.getValue() && argument().equals(other.argument()) &&
94                 rawArgument().equals(other.rawArgument()) &&
95                 statementDefinition().equals(other.statementDefinition()) &&
96                 getStatementSource().equals(other.getStatementSource()) &&
97                 declaredSubstatements().equals(other.declaredSubstatements());
98     }
99 }