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