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.yangtools.binding.generator.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.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;
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 (!annotationBuilders.contains(builder)) {
38 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
45 private boolean addParameter(final ParameterImpl param) {
46 if (!parameters.contains(param)) {
47 parameters = LazyCollections.lazyAdd(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(packageName, name, annotationBuilders, parameters);
78 public int hashCode() {
81 result = prime * result + Objects.hashCode(name);
82 result = prime * result + Objects.hashCode(packageName);
87 public boolean equals(final Object obj) {
94 if (getClass() != obj.getClass()) {
97 AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj;
99 if (other.name != null) {
102 } else if (!name.equals(other.name)) {
105 if (packageName == null) {
106 if (other.packageName != null) {
109 } else if (!packageName.equals(other.packageName)) {
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);
127 return builder.toString();
130 private static final class AnnotationTypeImpl implements AnnotationType {
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;
138 public AnnotationTypeImpl(final String packageName, final String name,
139 final List<AnnotationTypeBuilder> annotationBuilders,
140 final List<AnnotationType.Parameter> parameters) {
142 this.packageName = packageName;
145 final List<AnnotationType> a = new ArrayList<>();
146 for (final AnnotationTypeBuilder builder : annotationBuilders) {
147 a.add(builder.toInstance());
149 this.annotations = ImmutableList.copyOf(a);
151 final List<String> p = new ArrayList<>();
152 for (final AnnotationType.Parameter parameter : parameters) {
153 p.add(parameter.getName());
155 this.paramNames = ImmutableList.copyOf(p);
157 this.parameters = parameters.isEmpty() ? Collections.<AnnotationType.Parameter>emptyList()
158 : Collections.unmodifiableList(parameters);
162 public String getPackageName() {
167 public String getName() {
172 public String getFullyQualifiedName() {
173 return packageName + "." + name;
177 public List<AnnotationType> getAnnotations() {
182 public Parameter getParameter(final String paramName) {
183 if (paramName != null) {
184 for (final AnnotationType.Parameter parameter : parameters) {
185 if (parameter.getName().equals(paramName)) {
194 public List<Parameter> getParameters() {
199 public List<String> getParameterNames() {
204 public boolean containsParameters() {
205 return !parameters.isEmpty();
209 public int hashCode() {
210 final int prime = 31;
212 result = prime * result + Objects.hashCode(name);
213 result = prime * result + Objects.hashCode(packageName);
218 public boolean equals(final Object obj) {
225 if (getClass() != obj.getClass()) {
228 AnnotationTypeImpl other = (AnnotationTypeImpl) obj;
230 if (other.name != null) {
233 } else if (!name.equals(other.name)) {
236 if (packageName == null) {
237 if (other.packageName != null) {
240 } else if (!packageName.equals(other.packageName)) {
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);
258 return builder.toString();
262 private static final class ParameterImpl implements AnnotationType.Parameter {
264 private final String name;
265 private final String value;
266 private final List<String> values;
268 public ParameterImpl(final String name, final String value) {
272 this.values = Collections.emptyList();
275 public ParameterImpl(final String name, final List<String> values) {
278 this.values = values;
283 public String getName() {
288 public String getValue() {
293 public List<String> getValues() {
298 public int hashCode() {
299 final int prime = 31;
301 result = prime * result + Objects.hashCode(name);
306 public boolean equals(final Object obj) {
313 if (getClass() != obj.getClass()) {
316 ParameterImpl other = (ParameterImpl) obj;
318 if (other.name != null) {
321 } else if (!name.equals(other.name)) {
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);
337 return builder.toString();