Introduce formatting methods for SourceException
[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 = SourceException.throwIfNull(
39             firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
40             "Leaf-list is missing a 'type' statement");
41
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());
48             }
49
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());
60             }
61         }
62
63         type = builder.build();
64         userOrdered = isUserOrdered;
65     }
66
67     @Override
68     public Optional<LeafListSchemaNode> getOriginal() {
69         return Optional.fromNullable(original);
70     }
71
72     @Override
73     public TypeDefinition<?> getType() {
74         return type;
75     }
76
77     @Override
78     public boolean isUserOrdered() {
79         return userOrdered;
80     }
81
82     @Override
83     public int hashCode() {
84         final int prime = 31;
85         int result = 1;
86         result = prime * result + Objects.hashCode(getQName());
87         result = prime * result + Objects.hashCode(getPath());
88         return result;
89     }
90
91     @Override
92     public boolean equals(final Object obj) {
93         if (this == obj) {
94             return true;
95         }
96         if (obj == null) {
97             return false;
98         }
99         if (getClass() != obj.getClass()) {
100             return false;
101         }
102         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
103         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
104     }
105
106     @Override
107     public String toString() {
108         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
109         sb.append("[");
110         sb.append(getQName());
111         sb.append("]");
112         return sb.toString();
113     }
114 }