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