6d9a5f6f42d90801b7c2ac84f1f680372a2aa15b
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafListEffectiveStatementImpl.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.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;
23
24 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement> implements
25         LeafListSchemaNode, DerivableSchemaNode {
26
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;
31
32     public LeafListEffectiveStatementImpl(
33             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
34         super(ctx);
35         this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx()
36                 .buildEffective();
37
38         final TypeEffectiveStatement<?> typeStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
39         if (typeStmt == null) {
40             throw new SourceException("Leaf-list is missing a 'type' statement", ctx.getStatementSourceReference());
41         }
42
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());
49             }
50
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());
61             }
62         }
63
64         type = builder.build();
65         userOrdered = isUserOrdered;
66     }
67
68     @Override
69     public Optional<LeafListSchemaNode> getOriginal() {
70         return Optional.fromNullable(original);
71     }
72
73     @Override
74     public TypeDefinition<?> getType() {
75         return type;
76     }
77
78     @Override
79     public boolean isUserOrdered() {
80         return userOrdered;
81     }
82
83     @Override
84     public int hashCode() {
85         final int prime = 31;
86         int result = 1;
87         result = prime * result + Objects.hashCode(getQName());
88         result = prime * result + Objects.hashCode(getPath());
89         return result;
90     }
91
92     @Override
93     public boolean equals(final Object obj) {
94         if (this == obj) {
95             return true;
96         }
97         if (obj == null) {
98             return false;
99         }
100         if (getClass() != obj.getClass()) {
101             return false;
102         }
103         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
104         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
105     }
106
107     @Override
108     public String toString() {
109         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
110         sb.append("[");
111         sb.append(getQName());
112         sb.append("]");
113         return sb.toString();
114     }
115 }