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