Bug 4412: New yang parser effective statements cleanup
[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.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
20
21 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement> implements
22         LeafListSchemaNode, DerivableSchemaNode {
23
24     private final LeafListSchemaNode original;
25     private final TypeDefinition<?> type;
26     private final boolean userOrdered;
27     private static final String ORDER_BY_USER_KEYWORD = "user";
28
29     public LeafListEffectiveStatementImpl(
30             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
31         super(ctx);
32         this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx()
33                 .buildEffective();
34
35         OrderedByEffectiveStatementImpl orderedByStmt = firstEffective(OrderedByEffectiveStatementImpl.class);
36         if (orderedByStmt != null && orderedByStmt.argument().equals(ORDER_BY_USER_KEYWORD)) {
37             this.userOrdered = true;
38         } else {
39             this.userOrdered = false;
40         }
41
42         EffectiveStatement<?, ?> typeEffectiveSubstatement = firstEffectiveSubstatementOfType(TypeDefinition.class);
43         this.type = TypeUtils.getTypeFromEffectiveStatement(typeEffectiveSubstatement);
44     }
45
46     @Override
47     public Optional<LeafListSchemaNode> getOriginal() {
48         return Optional.fromNullable(original);
49     }
50
51     @Override
52     public TypeDefinition<?> getType() {
53         return type;
54     }
55
56     @Override
57     public boolean isUserOrdered() {
58         return userOrdered;
59     }
60
61     @Override
62     public int hashCode() {
63         final int prime = 31;
64         int result = 1;
65         result = prime * result + Objects.hashCode(getQName());
66         result = prime * result + Objects.hashCode(getPath());
67         return result;
68     }
69
70     @Override
71     public boolean equals(final Object obj) {
72         if (this == obj) {
73             return true;
74         }
75         if (obj == null) {
76             return false;
77         }
78         if (getClass() != obj.getClass()) {
79             return false;
80         }
81         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
82         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
83     }
84
85     @Override
86     public String toString() {
87         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
88         sb.append("[");
89         sb.append(getQName());
90         sb.append("]");
91         return sb.toString();
92     }
93 }