cb2fc872b9908eac90a117622b5598ac78b4ebac
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / main / java / org / opendaylight / controller / sal / binding / generator / impl / ConstantBuilderImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.binding.generator.impl;\r
9 \r
10 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
11 import org.opendaylight.controller.sal.binding.model.api.Type;\r
12 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;\r
13 \r
14 final class ConstantBuilderImpl implements ConstantBuilder {\r
15 \r
16     private final Type type;\r
17     private final String name;\r
18     private Object value;\r
19 \r
20     public ConstantBuilderImpl(Type type, String name, Object value) {\r
21         super();\r
22         this.type = type;\r
23         this.name = name;\r
24         this.value = value;\r
25     }\r
26 \r
27     public ConstantBuilderImpl(Type type, String name) {\r
28         super();\r
29         this.type = type;\r
30         this.name = name;\r
31     }\r
32 \r
33     @Override\r
34     public void assignValue(Object value) {\r
35         this.value = value;\r
36     }\r
37 \r
38     @Override\r
39     public Constant toInstance(final Type definingType) {\r
40         return new ConstantImpl(definingType, type, name, value);\r
41     }\r
42 \r
43     private static final class ConstantImpl implements Constant {\r
44 \r
45         final Type definingType;\r
46         private final Type type;\r
47         private final String name;\r
48         private final Object value;\r
49 \r
50         public ConstantImpl(final Type definingType, final Type type,\r
51                 final String name, final Object value) {\r
52             super();\r
53             this.definingType = definingType;\r
54             this.type = type;\r
55             this.name = name;\r
56             this.value = value;\r
57         }\r
58 \r
59         @Override\r
60         public Type getDefiningType() {\r
61             return definingType;\r
62         }\r
63 \r
64         @Override\r
65         public Type getType() {\r
66             return type;\r
67         }\r
68 \r
69         @Override\r
70         public String getName() {\r
71             return name;\r
72         }\r
73 \r
74         @Override\r
75         public Object getValue() {\r
76             return value;\r
77         }\r
78 \r
79         @Override\r
80         public String toFormattedString() {\r
81             StringBuilder builder = new StringBuilder();\r
82             builder.append(type);\r
83             builder.append(" ");\r
84             builder.append(name);\r
85             builder.append(" ");\r
86             builder.append(value);\r
87             return builder.toString();\r
88         }\r
89 \r
90         /*\r
91          * (non-Javadoc)\r
92          * \r
93          * @see java.lang.Object#hashCode()\r
94          */\r
95         @Override\r
96         public int hashCode() {\r
97             final int prime = 31;\r
98             int result = 1;\r
99             result = prime * result + ((name == null) ? 0 : name.hashCode());\r
100             result = prime * result + ((type == null) ? 0 : type.hashCode());\r
101             result = prime * result + ((value == null) ? 0 : value.hashCode());\r
102             if (definingType != null) {\r
103                 result = prime\r
104                         * result\r
105                         + ((definingType.getPackageName() == null) ? 0\r
106                                 : definingType.getPackageName().hashCode());\r
107                 result = prime\r
108                         * result\r
109                         + ((definingType.getName() == null) ? 0 : definingType\r
110                                 .getName().hashCode());\r
111             }\r
112             return result;\r
113         }\r
114 \r
115         /*\r
116          * (non-Javadoc)\r
117          * \r
118          * @see java.lang.Object#equals(java.lang.Object)\r
119          */\r
120         @Override\r
121         public boolean equals(Object obj) {\r
122             if (this == obj) {\r
123                 return true;\r
124             }\r
125             if (obj == null) {\r
126                 return false;\r
127             }\r
128             if (getClass() != obj.getClass()) {\r
129                 return false;\r
130             }\r
131             ConstantImpl other = (ConstantImpl) obj;\r
132             if (name == null) {\r
133                 if (other.name != null) {\r
134                     return false;\r
135                 }\r
136             } else if (!name.equals(other.name)) {\r
137                 return false;\r
138             }\r
139             if (type == null) {\r
140                 if (other.type != null) {\r
141                     return false;\r
142                 }\r
143             } else if (!type.equals(other.type)) {\r
144                 return false;\r
145             }\r
146             if (value == null) {\r
147                 if (other.value != null) {\r
148                     return false;\r
149                 }\r
150             } else if (!value.equals(other.value)) {\r
151                 return false;\r
152             }\r
153             if (definingType == null) {\r
154                 if (other.definingType != null) {\r
155                     return false;\r
156                 }\r
157             } else if ((definingType != null) && (other.definingType != null)) {\r
158                 if (!definingType.getPackageName().equals(\r
159                         other.definingType.getPackageName())\r
160                         && !definingType.getName().equals(\r
161                                 other.definingType.getName())) {\r
162                     return false;\r
163                 }\r
164             }\r
165             return true;\r
166         }\r
167 \r
168         @Override\r
169         public String toString() {\r
170             StringBuilder builder = new StringBuilder();\r
171             builder.append("ConstantImpl [type=");\r
172             builder.append(type);\r
173             builder.append(", name=");\r
174             builder.append(name);\r
175             builder.append(", value=");\r
176             builder.append(value);\r
177             if (definingType != null) {\r
178                 builder.append(", definingType=");\r
179                 builder.append(definingType.getPackageName());\r
180                 builder.append(".");\r
181                 builder.append(definingType.getName());\r
182             } else {\r
183                 builder.append(", definingType= null");\r
184             }\r
185             builder.append("]");\r
186             return builder.toString();\r
187         }\r
188     }\r
189 }\r