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 = SourceException.throwIfNull(
36 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
37 "Leaf is missing a 'type' statement");
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);
61 type = builder.build();
65 public Optional<LeafSchemaNode> getOriginal() {
66 return Optional.fromNullable(original);
70 public TypeDefinition<?> getType() {
75 public String getDefault() {
80 public String getUnits() {
85 public int hashCode() {
88 result = prime * result + Objects.hashCode(getQName());
89 result = prime * result + Objects.hashCode(getPath());
94 public boolean equals(final Object obj) {
98 if (!(obj instanceof LeafEffectiveStatementImpl)) {
101 LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
102 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
106 public String toString() {
107 StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
109 sb.append("qname=").append(getQName());
110 sb.append(", path=").append(getPath());
112 return sb.toString();