Merge from development repository.
[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(String targetPathStr) {
21         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(String deviate) {
32         if (deviate.equals("not-supported")) {
33             instance.setDeviate(Deviate.NOT_SUPPORTED);
34         } else if (deviate.equals("add")) {
35             instance.setDeviate(Deviate.ADD);
36         } else if (deviate.equals("replace")) {
37             instance.setDeviate(Deviate.REPLACE);
38         } else if (deviate.equals("delete")) {
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(String reference) {
47         instance.setReference(reference);
48     }
49
50     private static class DeviationImpl implements Deviation {
51
52         private SchemaPath targetPath;
53         private Deviate deviate;
54         private String reference;
55
56         private DeviationImpl(SchemaPath targetPath) {
57             this.targetPath = targetPath;
58         }
59
60         @Override
61         public SchemaPath getTargetPath() {
62             return targetPath;
63         }
64
65         @Override
66         public Deviate getDeviate() {
67             return deviate;
68         }
69
70         private void setDeviate(Deviate deviate) {
71             this.deviate = deviate;
72         }
73
74         @Override
75         public String getReference() {
76             return reference;
77         }
78
79         private void setReference(String reference) {
80             this.reference = reference;
81         }
82
83         @Override
84         public int hashCode() {
85             final int prime = 31;
86             int result = 1;
87             result = prime * result
88                     + ((targetPath == null) ? 0 : targetPath.hashCode());
89             result = prime * result
90                     + ((deviate == null) ? 0 : deviate.hashCode());
91             result = prime * result
92                     + ((reference == null) ? 0 : reference.hashCode());
93             return result;
94         }
95
96         @Override
97         public boolean equals(Object obj) {
98             if (this == obj) {
99                 return true;
100             }
101             if (obj == null) {
102                 return false;
103             }
104             if (getClass() != obj.getClass()) {
105                 return false;
106             }
107             DeviationImpl other = (DeviationImpl) obj;
108             if (targetPath == null) {
109                 if (other.targetPath != null) {
110                     return false;
111                 }
112             } else if (!targetPath.equals(other.targetPath)) {
113                 return false;
114             }
115             if (deviate == null) {
116                 if (other.deviate != null) {
117                     return false;
118                 }
119             } else if (!deviate.equals(other.deviate)) {
120                 return false;
121             }
122             if (reference == null) {
123                 if (other.reference != null) {
124                     return false;
125                 }
126             } else if (!reference.equals(other.reference)) {
127                 return false;
128             }
129             return true;
130         }
131
132         @Override
133         public String toString() {
134             StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
135             sb.append("[");
136             sb.append("targetPath="+ targetPath);
137             sb.append(", deviate="+ deviate);
138             sb.append(", reference="+ reference);
139             sb.append("]");
140             return sb.toString();
141         }
142     }
143
144 }