Make getOriginalCtx() give out an Optional
[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 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
24
25 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement> implements
26         LeafSchemaNode, DerivableSchemaNode {
27     private final LeafSchemaNode original;
28     private final TypeDefinition<?> type;
29     private final String defaultStr;
30     private final String unitsStr;
31
32     public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
33         super(ctx);
34         this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
35
36         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
37                 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
38                 "Leaf is missing a 'type' statement");
39
40         String dflt = null;
41         String units = null;
42         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
43             ctx.getSchemaPath().get());
44         for (final 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         try {
61             SourceException.throwIf(TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
62                     ctx.getStatementSourceReference(),
63                     "Leaf '%s' has default value '%s' marked with an if-feature statement.",
64                     ctx.getStatementArgument(), dflt);
65         } catch (final IllegalStateException e) {
66             throw new SourceException(ctx.getStatementSourceReference(), e,
67                     "Unable to find a default value for leaf '%s'", ctx.getStatementArgument());
68         }
69
70         defaultStr = dflt;
71         unitsStr = units;
72         type = builder.build();
73     }
74
75     @Override
76     public Optional<LeafSchemaNode> getOriginal() {
77         return Optional.fromNullable(original);
78     }
79
80     @Override
81     public TypeDefinition<?> getType() {
82         return type;
83     }
84
85     @Override
86     public String getDefault() {
87         return defaultStr;
88     }
89
90     @Override
91     public String getUnits() {
92         return unitsStr;
93     }
94
95     @Override
96     public int hashCode() {
97         final int prime = 31;
98         int result = 1;
99         result = prime * result + Objects.hashCode(getQName());
100         result = prime * result + Objects.hashCode(getPath());
101         return result;
102     }
103
104     @Override
105     public boolean equals(final Object obj) {
106         if (this == obj) {
107             return true;
108         }
109         if (!(obj instanceof LeafEffectiveStatementImpl)) {
110             return false;
111         }
112         final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
113         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
114     }
115
116     @Override
117     public String toString() {
118         return LeafEffectiveStatementImpl.class.getSimpleName() + "[" +
119                 "qname=" + getQName() +
120                 ", path=" + getPath() +
121                 "]";
122     }
123 }