Bug 981: Removed direct access to parent fields.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / DeviationBuilder.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import java.util.List;
11
12 import org.opendaylight.yangtools.yang.model.api.Deviation;
13 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
16 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractBuilder;
17 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
18 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
19
20 import com.google.common.collect.ImmutableList;
21
22 public final class DeviationBuilder extends AbstractBuilder {
23     private DeviationImpl instance;
24     private final String targetPathStr;
25     private SchemaPath targetPath;
26     private Deviate deviate;
27     private String reference;
28
29     DeviationBuilder(final String moduleName, final int line, final String targetPathStr) {
30         super(moduleName, line);
31         if (!targetPathStr.startsWith("/")) {
32             throw new YangParseException(moduleName, line,
33                     "Deviation argument string must be an absolute schema node identifier.");
34         }
35         this.targetPathStr = targetPathStr;
36         this.targetPath = ParserUtils.parseXPathString(targetPathStr);
37     }
38
39     @Override
40     public Deviation build() {
41         if (targetPath == null) {
42             throw new YangParseException(getModuleName(), getLine(), "Unresolved deviation target");
43         }
44
45         if (instance != null) {
46             return instance;
47         }
48
49         instance = new DeviationImpl();
50         instance.targetPath = targetPath;
51         instance.deviate = deviate;
52         instance.reference = reference;
53
54         // UNKNOWN NODES
55         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
56             unknownNodes.add(b.build());
57         }
58         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
59
60         return instance;
61     }
62
63     public SchemaPath getTargetPath() {
64         return targetPath;
65     }
66
67     public void setTargetPath(final SchemaPath targetPath) {
68         this.targetPath = targetPath;
69     }
70
71     public void setDeviate(final String deviate) {
72         if ("not-supported".equals(deviate)) {
73             this.deviate = Deviate.NOT_SUPPORTED;
74         } else if ("add".equals(deviate)) {
75             this.deviate = Deviate.ADD;
76         } else if ("replace".equals(deviate)) {
77             this.deviate = Deviate.REPLACE;
78         } else if ("delete".equals(deviate)) {
79             this.deviate = Deviate.DELETE;
80         } else {
81             throw new YangParseException(getModuleName(), getLine(), "Unsupported type of 'deviate' statement: " + deviate);
82         }
83     }
84
85     public void setReference(final String reference) {
86         this.reference = reference;
87     }
88
89     @Override
90     public String toString() {
91         return "deviation " + targetPathStr;
92     }
93
94     private static final class DeviationImpl implements Deviation {
95         private SchemaPath targetPath;
96         private Deviate deviate;
97         private String reference;
98         private ImmutableList<UnknownSchemaNode> unknownNodes;
99
100         private DeviationImpl() {
101         }
102
103         @Override
104         public SchemaPath getTargetPath() {
105             return targetPath;
106         }
107
108         @Override
109         public Deviate getDeviate() {
110             return deviate;
111         }
112
113         @Override
114         public String getReference() {
115             return reference;
116         }
117
118         @Override
119         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
120             return unknownNodes;
121         }
122
123         @Override
124         public int hashCode() {
125             final int prime = 31;
126             int result = 1;
127             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
128             result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
129             result = prime * result + ((reference == null) ? 0 : reference.hashCode());
130             return result;
131         }
132
133         @Override
134         public boolean equals(Object obj) {
135             if (this == obj) {
136                 return true;
137             }
138             if (obj == null) {
139                 return false;
140             }
141             if (getClass() != obj.getClass()) {
142                 return false;
143             }
144             DeviationImpl other = (DeviationImpl) obj;
145             if (targetPath == null) {
146                 if (other.targetPath != null) {
147                     return false;
148                 }
149             } else if (!targetPath.equals(other.targetPath)) {
150                 return false;
151             }
152             if (deviate == null) {
153                 if (other.deviate != null) {
154                     return false;
155                 }
156             } else if (!deviate.equals(other.deviate)) {
157                 return false;
158             }
159             if (reference == null) {
160                 if (other.reference != null) {
161                     return false;
162                 }
163             } else if (!reference.equals(other.reference)) {
164                 return false;
165             }
166             return true;
167         }
168
169         @Override
170         public String toString() {
171             StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
172             sb.append("[");
173             sb.append("targetPath=" + targetPath);
174             sb.append(", deviate=" + deviate);
175             sb.append(", reference=" + reference);
176             sb.append("]");
177             return sb.toString();
178         }
179     }
180
181 }