Fixed tests. Added javadocs to yang-model-api.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / 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.controller.yang.parser.builder.impl;
9
10 import org.opendaylight.controller.yang.model.api.Deviation;
11 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
12 import org.opendaylight.controller.yang.model.api.SchemaPath;
13 import org.opendaylight.controller.yang.parser.builder.api.Builder;
14 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
15 import org.opendaylight.controller.yang.parser.util.YangParseException;
16
17 public class DeviationBuilder implements Builder {
18     private final DeviationImpl instance;
19     private final int line;
20
21     DeviationBuilder(final String targetPathStr, final int line) {
22         this.line = line;
23         final SchemaPath targetPath = YangModelBuilderUtil
24                 .parseAugmentPath(targetPathStr);
25         instance = new DeviationImpl(targetPath);
26     }
27
28     @Override
29     public Deviation build() {
30         return instance;
31     }
32
33     @Override
34     public int getLine() {
35         return line;
36     }
37
38     public void setDeviate(final String deviate) {
39         if ("not-supported".equals(deviate)) {
40             instance.setDeviate(Deviate.NOT_SUPPORTED);
41         } else if ("add".equals(deviate)) {
42             instance.setDeviate(Deviate.ADD);
43         } else if ("replace".equals(deviate)) {
44             instance.setDeviate(Deviate.REPLACE);
45         } else if ("delete".equals(deviate)) {
46             instance.setDeviate(Deviate.DELETE);
47         } else {
48             throw new YangParseException(line,
49                     "Unsupported type of 'deviate' statement: " + deviate);
50         }
51     }
52
53     public void setReference(final String reference) {
54         instance.setReference(reference);
55     }
56
57     private class DeviationImpl implements Deviation {
58         private final SchemaPath targetPath;
59         private Deviate deviate;
60         private String reference;
61
62         private DeviationImpl(final SchemaPath targetPath) {
63             this.targetPath = targetPath;
64         }
65
66         @Override
67         public SchemaPath getTargetPath() {
68             return targetPath;
69         }
70
71         @Override
72         public Deviate getDeviate() {
73             return deviate;
74         }
75
76         private void setDeviate(final Deviate deviate) {
77             this.deviate = deviate;
78         }
79
80         @Override
81         public String getReference() {
82             return reference;
83         }
84
85         private void setReference(final String reference) {
86             this.reference = reference;
87         }
88
89         @Override
90         public int hashCode() {
91             final int prime = 31;
92             int result = 1;
93             result = prime * result
94                     + ((targetPath == null) ? 0 : targetPath.hashCode());
95             result = prime * result
96                     + ((deviate == null) ? 0 : deviate.hashCode());
97             result = prime * result
98                     + ((reference == null) ? 0 : reference.hashCode());
99             return result;
100         }
101
102         @Override
103         public boolean equals(Object obj) {
104             if (this == obj) {
105                 return true;
106             }
107             if (obj == null) {
108                 return false;
109             }
110             if (getClass() != obj.getClass()) {
111                 return false;
112             }
113             DeviationImpl other = (DeviationImpl) obj;
114             if (targetPath == null) {
115                 if (other.targetPath != null) {
116                     return false;
117                 }
118             } else if (!targetPath.equals(other.targetPath)) {
119                 return false;
120             }
121             if (deviate == null) {
122                 if (other.deviate != null) {
123                     return false;
124                 }
125             } else if (!deviate.equals(other.deviate)) {
126                 return false;
127             }
128             if (reference == null) {
129                 if (other.reference != null) {
130                     return false;
131                 }
132             } else if (!reference.equals(other.reference)) {
133                 return false;
134             }
135             return true;
136         }
137
138         @Override
139         public String toString() {
140             StringBuilder sb = new StringBuilder(
141                     DeviationImpl.class.getSimpleName());
142             sb.append("[");
143             sb.append("targetPath=" + targetPath);
144             sb.append(", deviate=" + deviate);
145             sb.append(", reference=" + reference);
146             sb.append("]");
147             return sb.toString();
148         }
149     }
150
151 }