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