16e89e08ab1d4274a2a2bc1a83d36e8aa9764813
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / generated / type / builder / ConstantBuilderImpl.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.binding.generator.util.generated.type.builder;
9
10 import org.opendaylight.controller.sal.binding.model.api.Constant;
11 import org.opendaylight.controller.sal.binding.model.api.Type;
12 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
13
14 final class ConstantBuilderImpl implements ConstantBuilder {
15
16     private final Type type;
17     private final String name;
18     private Object value;
19
20     public ConstantBuilderImpl(Type type, String name, Object value) {
21         super();
22         this.type = type;
23         this.name = name;
24         this.value = value;
25     }
26
27     public ConstantBuilderImpl(Type type, String name) {
28         super();
29         this.type = type;
30         this.name = name;
31     }
32
33     @Override
34     public void assignValue(Object value) {
35         this.value = value;
36     }
37
38     @Override
39     public Constant toInstance(final Type definingType) {
40         return new ConstantImpl(definingType, type, name, value);
41     }
42
43     private static final class ConstantImpl implements Constant {
44
45         final Type definingType;
46         private final Type type;
47         private final String name;
48         private final Object value;
49
50         public ConstantImpl(final Type definingType, final Type type,
51                 final String name, final Object value) {
52             super();
53             this.definingType = definingType;
54             this.type = type;
55             this.name = name;
56             this.value = value;
57         }
58
59         @Override
60         public Type getDefiningType() {
61             return definingType;
62         }
63
64         @Override
65         public Type getType() {
66             return type;
67         }
68
69         @Override
70         public String getName() {
71             return name;
72         }
73
74         @Override
75         public Object getValue() {
76             return value;
77         }
78         
79         @Override
80         public String toFormattedString() {
81             StringBuilder builder = new StringBuilder();
82             builder.append(type);
83             builder.append(" ");
84             builder.append(name);
85             builder.append(" ");
86             builder.append(value);
87             return builder.toString();
88         }
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see java.lang.Object#hashCode()
94          */
95         @Override
96         public int hashCode() {
97             final int prime = 31;
98             int result = 1;
99             result = prime * result + ((name == null) ? 0 : name.hashCode());
100             result = prime * result + ((type == null) ? 0 : type.hashCode());
101             return result;
102         }
103
104         /*
105          * (non-Javadoc)
106          * 
107          * @see java.lang.Object#equals(java.lang.Object)
108          */
109         @Override
110         public boolean equals(Object obj) {
111             if (this == obj) {
112                 return true;
113             }
114             if (obj == null) {
115                 return false;
116             }
117             if (getClass() != obj.getClass()) {
118                 return false;
119             }
120             ConstantImpl other = (ConstantImpl) obj;
121             if (name == null) {
122                 if (other.name != null) {
123                     return false;
124                 }
125             } else if (!name.equals(other.name)) {
126                 return false;
127             }
128             if (type == null) {
129                 if (other.type != null) {
130                     return false;
131                 }
132             } else if (!type.equals(other.type)) {
133                 return false;
134             }
135             if (value == null) {
136                 if (other.value != null) {
137                     return false;
138                 }
139             } else if (!value.equals(other.value)) {
140                 return false;
141             }
142             return true;
143         }
144
145         @Override
146         public String toString() {
147             StringBuilder builder = new StringBuilder();
148             builder.append("Constant [type=");
149             builder.append(type);
150             builder.append(", name=");
151             builder.append(name);
152             builder.append(", value=");
153             builder.append(value);
154             if (definingType != null) {
155                 builder.append(", definingType=");
156                 builder.append(definingType.getPackageName());
157                 builder.append(".");
158                 builder.append(definingType.getName());
159             } else {
160                 builder.append(", definingType= null");
161             }
162             builder.append("]");
163             return builder.toString();
164         }
165     }
166 }