Bug 4412: New yang parser effective statements cleanup
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 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.effective;
9
10 import com.google.common.base.Optional;
11 import java.util.Objects;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
20
21 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement> implements
22         LeafSchemaNode, DerivableSchemaNode {
23     private final LeafSchemaNode original;
24     private final TypeDefinition<?> type;
25     private final String defaultStr;
26     private final String unitsStr;
27
28     public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
29         super(ctx);
30         this.original = ctx.getOriginalCtx() == null ? null : (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
31
32         DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
33         this.defaultStr = (defaultStmt == null) ? null : defaultStmt.argument();
34
35         UnitsEffectiveStatementImpl unitsStmt = firstEffective(UnitsEffectiveStatementImpl.class);
36         this.unitsStr = (unitsStmt == null) ? null : unitsStmt.argument();
37
38         EffectiveStatement<?,?> typeEffectiveSubstatement = firstEffectiveSubstatementOfType(TypeDefinition.class);
39         this.type = TypeUtils.getTypeFromEffectiveStatement(typeEffectiveSubstatement);
40     }
41
42     @Override
43     public Optional<LeafSchemaNode> getOriginal() {
44         return Optional.fromNullable(original);
45     }
46
47     @Override
48     public TypeDefinition<?> getType() {
49         return type;
50     }
51
52     @Override
53     public String getDefault() {
54         return defaultStr;
55     }
56
57     @Override
58     public String getUnits() {
59         return unitsStr;
60     }
61
62     @Override
63     public int hashCode() {
64         final int prime = 31;
65         int result = 1;
66         result = prime * result + Objects.hashCode(getQName());
67         result = prime * result + Objects.hashCode(getPath());
68         return result;
69     }
70
71     @Override
72     public boolean equals(final Object obj) {
73         if (this == obj) {
74             return true;
75         }
76         if (obj == null) {
77             return false;
78         }
79         if (getClass() != obj.getClass()) {
80             return false;
81         }
82         LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
83         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
84     }
85
86     @Override
87     public String toString() {
88         StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
89         sb.append("[");
90         sb.append("qname=").append(getQName());
91         sb.append(", path=").append(getPath());
92         sb.append("]");
93         return sb.toString();
94     }
95 }