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