2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.util.generated.type.builder;
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.mdsal.binding.model.api.AnnotationType;
16 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
17 import org.opendaylight.mdsal.binding.model.util.AbstractBaseType;
18 import org.opendaylight.yangtools.util.LazyCollections;
20 final class AnnotationTypeBuilderImpl extends AbstractBaseType implements AnnotationTypeBuilder {
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();
27 public AnnotationTypeBuilderImpl(final String packageName, final String name) {
28 super(packageName, name);
29 this.packageName = packageName;
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 (!this.annotationBuilders.contains(builder)) {
38 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
45 private boolean addParameter(final ParameterImpl param) {
46 if (!this.parameters.contains(param)) {
47 this.parameters = LazyCollections.lazyAdd(this.parameters, param);
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);
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);
73 public AnnotationType toInstance() {
74 return new AnnotationTypeImpl(this.packageName, this.name, this.annotationBuilders, this.parameters);
78 public int hashCode() {
81 result = (prime * result) + Objects.hashCode(this.name);
82 result = (prime * result) + Objects.hashCode(this.packageName);
87 public boolean equals(final Object obj) {
94 if (getClass() != obj.getClass()) {
97 final AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj;
98 return Objects.equals(this.name, other.name) && Objects.equals(this.packageName, other.packageName);
102 public String toString() {
103 final StringBuilder builder = new StringBuilder();
104 builder.append("AnnotationTypeBuilder [packageName=");
105 builder.append(this.packageName);
106 builder.append(", name=");
107 builder.append(this.name);
108 builder.append(", annotationBuilders=");
109 builder.append(this.annotationBuilders);
110 builder.append(", parameters=");
111 builder.append(this.parameters);
113 return builder.toString();
116 private static final class AnnotationTypeImpl implements AnnotationType {
118 private final String packageName;
119 private final String name;
120 private final List<AnnotationType> annotations;
121 private final List<AnnotationType.Parameter> parameters;
122 private final List<String> paramNames;
124 public AnnotationTypeImpl(final String packageName, final String name,
125 final List<AnnotationTypeBuilder> annotationBuilders,
126 final List<AnnotationType.Parameter> parameters) {
127 this.packageName = packageName;
130 final List<AnnotationType> a = new ArrayList<>();
131 for (final AnnotationTypeBuilder builder : annotationBuilders) {
132 a.add(builder.toInstance());
134 this.annotations = ImmutableList.copyOf(a);
136 final List<String> p = new ArrayList<>();
137 for (final AnnotationType.Parameter parameter : parameters) {
138 p.add(parameter.getName());
140 this.paramNames = ImmutableList.copyOf(p);
142 this.parameters = parameters.isEmpty() ? Collections.<AnnotationType.Parameter>emptyList()
143 : Collections.unmodifiableList(parameters);
147 public String getPackageName() {
148 return this.packageName;
152 public String getName() {
157 public String getFullyQualifiedName() {
158 return this.packageName + "." + this.name;
162 public List<AnnotationType> getAnnotations() {
163 return this.annotations;
167 public Parameter getParameter(final String paramName) {
168 if (paramName != null) {
169 for (final AnnotationType.Parameter parameter : this.parameters) {
170 if (parameter.getName().equals(paramName)) {
179 public List<Parameter> getParameters() {
180 return this.parameters;
184 public List<String> getParameterNames() {
185 return this.paramNames;
189 public boolean containsParameters() {
190 return !this.parameters.isEmpty();
194 public int hashCode() {
195 final int prime = 31;
197 result = (prime * result) + Objects.hashCode(this.name);
198 result = (prime * result) + Objects.hashCode(this.packageName);
203 public boolean equals(final Object obj) {
210 if (getClass() != obj.getClass()) {
213 final AnnotationTypeImpl other = (AnnotationTypeImpl) obj;
214 return Objects.equals(this.name, other.name) && Objects.equals(this.packageName, other.packageName);
218 public String toString() {
219 final StringBuilder builder = new StringBuilder();
220 builder.append("AnnotationType [packageName=");
221 builder.append(this.packageName);
222 builder.append(", name=");
223 builder.append(this.name);
224 builder.append(", annotations=");
225 builder.append(this.annotations);
226 builder.append(", parameters=");
227 builder.append(this.parameters);
229 return builder.toString();
233 private static final class ParameterImpl implements AnnotationType.Parameter {
235 private final String name;
236 private final String value;
237 private final List<String> values;
239 public ParameterImpl(final String name, final String value) {
242 this.values = Collections.emptyList();
245 public ParameterImpl(final String name, final List<String> values) {
247 this.values = values;
252 public String getName() {
257 public String getValue() {
262 public List<String> getValues() {
267 public int hashCode() {
268 final int prime = 31;
270 result = (prime * result) + Objects.hashCode(this.name);
275 public boolean equals(final Object obj) {
282 if (getClass() != obj.getClass()) {
285 final ParameterImpl other = (ParameterImpl) obj;
286 return Objects.equals(this.name, other.name);
290 public String toString() {
291 final StringBuilder builder = new StringBuilder();
292 builder.append("ParameterImpl [name=");
293 builder.append(this.name);
294 builder.append(", value=");
295 builder.append(this.value);
296 builder.append(", values=");
297 builder.append(this.values);
299 return builder.toString();