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 = SourceException.throwIfNull(
39 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
40 "Leaf-list is missing a 'type' statement");
42 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
43 ctx.getSchemaPath().get());
44 boolean isUserOrdered = false;
45 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46 if (stmt instanceof OrderedByEffectiveStatementImpl) {
47 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
50 if (stmt instanceof DefaultEffectiveStatementImpl) {
51 builder.setDefaultValue(stmt.argument());
52 } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
53 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
54 } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
55 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
56 } else if (stmt instanceof StatusEffectiveStatementImpl) {
57 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
58 } else if (stmt instanceof UnitsEffectiveStatementImpl) {
59 builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument());
63 type = builder.build();
64 userOrdered = isUserOrdered;
68 public Optional<LeafListSchemaNode> getOriginal() {
69 return Optional.fromNullable(original);
73 public TypeDefinition<?> getType() {
78 public boolean isUserOrdered() {
83 public int hashCode() {
86 result = prime * result + Objects.hashCode(getQName());
87 result = prime * result + Objects.hashCode(getPath());
92 public boolean equals(final Object obj) {
99 if (getClass() != obj.getClass()) {
102 LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
103 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
107 public String toString() {
108 StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
110 sb.append(getQName());
112 return sb.toString();