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.LeafListSchemaNode;
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.LeafListStatement;
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 LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement> implements
25 LeafListSchemaNode, DerivableSchemaNode {
27 private static final String ORDER_BY_USER_KEYWORD = "user";
28 private final TypeDefinition<?> type;
29 private final LeafListSchemaNode original;
30 private final boolean userOrdered;
32 public LeafListEffectiveStatementImpl(
33 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
35 this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx()
38 final TypeEffectiveStatement<?> typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
39 if (typeStmt == null) {
40 throw new SourceException("Leaf-list is missing a 'type' statement", ctx.getStatementSourceReference());
43 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
44 ctx.getSchemaPath().get());
45 boolean isUserOrdered = false;
46 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
47 if (stmt instanceof OrderedByEffectiveStatementImpl) {
48 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
51 if (stmt instanceof DefaultEffectiveStatementImpl) {
52 builder.setDefaultValue(stmt.argument());
53 } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
54 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
55 } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
56 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
57 } else if (stmt instanceof StatusEffectiveStatementImpl) {
58 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
59 } else if (stmt instanceof UnitsEffectiveStatementImpl) {
60 builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument());
64 type = builder.build();
65 userOrdered = isUserOrdered;
69 public Optional<LeafListSchemaNode> getOriginal() {
70 return Optional.fromNullable(original);
74 public TypeDefinition<?> getType() {
79 public boolean isUserOrdered() {
84 public int hashCode() {
87 result = prime * result + Objects.hashCode(getQName());
88 result = prime * result + Objects.hashCode(getPath());
93 public boolean equals(final Object obj) {
100 if (getClass() != obj.getClass()) {
103 LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
104 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
108 public String toString() {
109 StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
111 sb.append(getQName());
113 return sb.toString();