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