2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
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;
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;
31 public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
33 this.original = ctx.getOriginalCtx() == null ? null : (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
35 final TypeEffectiveStatement<?> typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
36 if (typeStmt == null) {
37 throw new SourceException("Leaf is missing a 'type' statement", ctx.getStatementSourceReference());
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);
62 type = builder.build();
66 public Optional<LeafSchemaNode> getOriginal() {
67 return Optional.fromNullable(original);
71 public TypeDefinition<?> getType() {
76 public String getDefault() {
81 public String getUnits() {
86 public int hashCode() {
89 result = prime * result + Objects.hashCode(getQName());
90 result = prime * result + Objects.hashCode(getPath());
95 public boolean equals(final Object obj) {
99 if (!(obj instanceof LeafEffectiveStatementImpl)) {
102 LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
103 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
107 public String toString() {
108 StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
110 sb.append("qname=").append(getQName());
111 sb.append(", path=").append(getPath());
113 return sb.toString();