70d80394699d89810ef8465ad303bd5cd33b12ea
[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.model.api.stmt.TypeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
20 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23
24 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement> implements
25         LeafSchemaNode, DerivableSchemaNode {
26     private final LeafSchemaNode original;
27     private final TypeDefinition<?> type;
28     private final String defaultStr;
29     private final String unitsStr;
30
31     public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
32         super(ctx);
33         this.original = ctx.getOriginalCtx() == null ? null : (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
34
35         final TypeEffectiveStatement<?> typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
36         if (typeStmt == null) {
37             throw new SourceException("Leaf is missing a 'type' statement", ctx.getStatementSourceReference());
38         }
39
40         String dflt = null;
41         String units = null;
42         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
43             ctx.getSchemaPath().get());
44         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
45             if (stmt instanceof DefaultEffectiveStatementImpl) {
46                 dflt = ((DefaultEffectiveStatementImpl)stmt).argument();
47                 builder.setDefaultValue(stmt.argument());
48             } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
49                 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
50             } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
51                 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
52             } else if (stmt instanceof StatusEffectiveStatementImpl) {
53                 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
54             } else if (stmt instanceof UnitsEffectiveStatementImpl) {
55                 units = ((UnitsEffectiveStatementImpl)stmt).argument();
56                 builder.setUnits(units);
57             }
58         }
59
60         defaultStr = dflt;
61         unitsStr = units;
62         type = builder.build();
63     }
64
65     @Override
66     public Optional<LeafSchemaNode> getOriginal() {
67         return Optional.fromNullable(original);
68     }
69
70     @Override
71     public TypeDefinition<?> getType() {
72         return type;
73     }
74
75     @Override
76     public String getDefault() {
77         return defaultStr;
78     }
79
80     @Override
81     public String getUnits() {
82         return unitsStr;
83     }
84
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result + Objects.hashCode(getQName());
90         result = prime * result + Objects.hashCode(getPath());
91         return result;
92     }
93
94     @Override
95     public boolean equals(final Object obj) {
96         if (this == obj) {
97             return true;
98         }
99         if (!(obj instanceof LeafEffectiveStatementImpl)) {
100             return false;
101         }
102         LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
103         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
104     }
105
106     @Override
107     public String toString() {
108         StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
109         sb.append("[");
110         sb.append("qname=").append(getQName());
111         sb.append(", path=").append(getPath());
112         sb.append("]");
113         return sb.toString();
114     }
115 }