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