Use Objects.hashCode()
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / generated / type / builder / AnnotationTypeBuilderImpl.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.yangtools.binding.generator.util.generated.type.builder;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Objects;
15 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
16 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
17 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
18 import org.opendaylight.yangtools.util.LazyCollections;
19
20 final class AnnotationTypeBuilderImpl extends AbstractBaseType implements AnnotationTypeBuilder {
21
22     private final String packageName;
23     private final String name;
24     private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
25     private List<AnnotationType.Parameter> parameters = Collections.emptyList();
26
27     public AnnotationTypeBuilderImpl(final String packageName, final String name) {
28         super(packageName, name);
29         this.packageName = packageName;
30         this.name = name;
31     }
32
33     @Override
34     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
35         if (packageName != null && name != null) {
36             final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
37             if (!annotationBuilders.contains(builder)) {
38                 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
39                 return builder;
40             }
41         }
42         return null;
43     }
44
45     private boolean addParameter(final ParameterImpl param) {
46         if (!parameters.contains(param)) {
47             parameters = LazyCollections.lazyAdd(parameters, param);
48             return true;
49         } else {
50             return false;
51         }
52     }
53
54     @Override
55     public boolean addParameter(final String paramName, final String value) {
56         if (paramName != null && value != null) {
57             final ParameterImpl param = new ParameterImpl(paramName, value);
58             return addParameter(param);
59         }
60         return false;
61     }
62
63     @Override
64     public boolean addParameters(final String paramName, final List<String> values) {
65         if (paramName != null && values != null) {
66             final ParameterImpl param = new ParameterImpl(paramName, values);
67             return addParameter(param);
68         }
69         return false;
70     }
71
72     @Override
73     public AnnotationType toInstance() {
74         return new AnnotationTypeImpl(packageName, name, annotationBuilders, parameters);
75     }
76
77     @Override
78     public int hashCode() {
79         final int prime = 31;
80         int result = 1;
81         result = prime * result + Objects.hashCode(name);
82         result = prime * result + Objects.hashCode(packageName);
83         return result;
84     }
85
86     @Override
87     public boolean equals(final Object obj) {
88         if (this == obj) {
89             return true;
90         }
91         if (obj == null) {
92             return false;
93         }
94         if (getClass() != obj.getClass()) {
95             return false;
96         }
97         AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj;
98         if (name == null) {
99             if (other.name != null) {
100                 return false;
101             }
102         } else if (!name.equals(other.name)) {
103             return false;
104         }
105         if (packageName == null) {
106             if (other.packageName != null) {
107                 return false;
108             }
109         } else if (!packageName.equals(other.packageName)) {
110             return false;
111         }
112         return true;
113     }
114
115     @Override
116     public String toString() {
117         StringBuilder builder = new StringBuilder();
118         builder.append("AnnotationTypeBuilder [packageName=");
119         builder.append(packageName);
120         builder.append(", name=");
121         builder.append(name);
122         builder.append(", annotationBuilders=");
123         builder.append(annotationBuilders);
124         builder.append(", parameters=");
125         builder.append(parameters);
126         builder.append("]");
127         return builder.toString();
128     }
129
130     private static final class AnnotationTypeImpl implements AnnotationType {
131
132         private final String packageName;
133         private final String name;
134         private final List<AnnotationType> annotations;
135         private final List<AnnotationType.Parameter> parameters;
136         private final List<String> paramNames;
137
138         public AnnotationTypeImpl(final String packageName, final String name,
139                 final List<AnnotationTypeBuilder> annotationBuilders,
140                 final List<AnnotationType.Parameter> parameters) {
141             super();
142             this.packageName = packageName;
143             this.name = name;
144
145             final List<AnnotationType> a = new ArrayList<>();
146             for (final AnnotationTypeBuilder builder : annotationBuilders) {
147                 a.add(builder.toInstance());
148             }
149             this.annotations = ImmutableList.copyOf(a);
150
151             final List<String> p = new ArrayList<>();
152             for (final AnnotationType.Parameter parameter : parameters) {
153                 p.add(parameter.getName());
154             }
155             this.paramNames = ImmutableList.copyOf(p);
156
157             this.parameters = parameters.isEmpty() ? Collections.<AnnotationType.Parameter>emptyList()
158                     : Collections.unmodifiableList(parameters);
159         }
160
161         @Override
162         public String getPackageName() {
163             return packageName;
164         }
165
166         @Override
167         public String getName() {
168             return name;
169         }
170
171         @Override
172         public String getFullyQualifiedName() {
173             return packageName + "." + name;
174         }
175
176         @Override
177         public List<AnnotationType> getAnnotations() {
178             return annotations;
179         }
180
181         @Override
182         public Parameter getParameter(final String paramName) {
183             if (paramName != null) {
184                 for (final AnnotationType.Parameter parameter : parameters) {
185                     if (parameter.getName().equals(paramName)) {
186                         return parameter;
187                     }
188                 }
189             }
190             return null;
191         }
192
193         @Override
194         public List<Parameter> getParameters() {
195             return parameters;
196         }
197
198         @Override
199         public List<String> getParameterNames() {
200             return paramNames;
201         }
202
203         @Override
204         public boolean containsParameters() {
205             return !parameters.isEmpty();
206         }
207
208         @Override
209         public int hashCode() {
210             final int prime = 31;
211             int result = 1;
212             result = prime * result + Objects.hashCode(name);
213             result = prime * result + Objects.hashCode(packageName);
214             return result;
215         }
216
217         @Override
218         public boolean equals(final Object obj) {
219             if (this == obj) {
220                 return true;
221             }
222             if (obj == null) {
223                 return false;
224             }
225             if (getClass() != obj.getClass()) {
226                 return false;
227             }
228             AnnotationTypeImpl other = (AnnotationTypeImpl) obj;
229             if (name == null) {
230                 if (other.name != null) {
231                     return false;
232                 }
233             } else if (!name.equals(other.name)) {
234                 return false;
235             }
236             if (packageName == null) {
237                 if (other.packageName != null) {
238                     return false;
239                 }
240             } else if (!packageName.equals(other.packageName)) {
241                 return false;
242             }
243             return true;
244         }
245
246         @Override
247         public String toString() {
248             StringBuilder builder = new StringBuilder();
249             builder.append("AnnotationType [packageName=");
250             builder.append(packageName);
251             builder.append(", name=");
252             builder.append(name);
253             builder.append(", annotations=");
254             builder.append(annotations);
255             builder.append(", parameters=");
256             builder.append(parameters);
257             builder.append("]");
258             return builder.toString();
259         }
260     }
261
262     private static final class ParameterImpl implements AnnotationType.Parameter {
263
264         private final String name;
265         private final String value;
266         private final List<String> values;
267
268         public ParameterImpl(final String name, final String value) {
269             super();
270             this.name = name;
271             this.value = value;
272             this.values = Collections.emptyList();
273         }
274
275         public ParameterImpl(final String name, final List<String> values) {
276             super();
277             this.name = name;
278             this.values = values;
279             this.value = null;
280         }
281
282         @Override
283         public String getName() {
284             return name;
285         }
286
287         @Override
288         public String getValue() {
289             return value;
290         }
291
292         @Override
293         public List<String> getValues() {
294             return values;
295         }
296
297         @Override
298         public int hashCode() {
299             final int prime = 31;
300             int result = 1;
301             result = prime * result + Objects.hashCode(name);
302             return result;
303         }
304
305         @Override
306         public boolean equals(final Object obj) {
307             if (this == obj) {
308                 return true;
309             }
310             if (obj == null) {
311                 return false;
312             }
313             if (getClass() != obj.getClass()) {
314                 return false;
315             }
316             ParameterImpl other = (ParameterImpl) obj;
317             if (name == null) {
318                 if (other.name != null) {
319                     return false;
320                 }
321             } else if (!name.equals(other.name)) {
322                 return false;
323             }
324             return true;
325         }
326
327         @Override
328         public String toString() {
329             StringBuilder builder = new StringBuilder();
330             builder.append("ParameterImpl [name=");
331             builder.append(name);
332             builder.append(", value=");
333             builder.append(value);
334             builder.append(", values=");
335             builder.append(values);
336             builder.append("]");
337             return builder.toString();
338         }
339     }
340 }