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;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
16 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
17 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
18 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
19 import org.opendaylight.yangtools.util.LazyCollections;
21 final class AnnotationTypeBuilderImpl extends AbstractBaseType implements AnnotationTypeBuilder {
23 private final String packageName;
24 private final String name;
25 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
26 private List<AnnotationType.Parameter> parameters = Collections.emptyList();
28 public AnnotationTypeBuilderImpl(final String packageName, final String name) {
29 super(packageName, name);
30 this.packageName = packageName;
35 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
36 if (packageName != null && name != null) {
37 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
38 if (!annotationBuilders.contains(builder)) {
39 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
46 private boolean addParameter(final ParameterImpl param) {
47 if (!parameters.contains(param)) {
48 parameters = LazyCollections.lazyAdd(parameters, param);
56 public boolean addParameter(final String paramName, final String value) {
57 if (paramName != null && value != null) {
58 final ParameterImpl param = new ParameterImpl(paramName, value);
59 return addParameter(param);
65 public boolean addParameters(final String paramName, final List<String> values) {
66 if (paramName != null && values != null) {
67 final ParameterImpl param = new ParameterImpl(paramName, values);
68 return addParameter(param);
74 public AnnotationType toInstance() {
75 return new AnnotationTypeImpl(packageName, name, annotationBuilders, parameters);
79 public int hashCode() {
82 result = prime * result + ((name == null) ? 0 : name.hashCode());
83 result = prime * result
84 + ((packageName == null) ? 0 : packageName.hashCode());
89 public boolean equals(final Object obj) {
96 if (getClass() != obj.getClass()) {
99 AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj;
101 if (other.name != null) {
104 } else if (!name.equals(other.name)) {
107 if (packageName == null) {
108 if (other.packageName != null) {
111 } else if (!packageName.equals(other.packageName)) {
118 public String toString() {
119 StringBuilder builder = new StringBuilder();
120 builder.append("AnnotationTypeBuilder [packageName=");
121 builder.append(packageName);
122 builder.append(", name=");
123 builder.append(name);
124 builder.append(", annotationBuilders=");
125 builder.append(annotationBuilders);
126 builder.append(", parameters=");
127 builder.append(parameters);
129 return builder.toString();
132 private static final class AnnotationTypeImpl implements AnnotationType {
134 private final String packageName;
135 private final String name;
136 private final List<AnnotationType> annotations;
137 private final List<AnnotationType.Parameter> parameters;
138 private final List<String> paramNames;
140 public AnnotationTypeImpl(final String packageName, final String name,
141 final List<AnnotationTypeBuilder> annotationBuilders,
142 final List<AnnotationType.Parameter> parameters) {
144 this.packageName = packageName;
147 final List<AnnotationType> a = new ArrayList<>();
148 for (final AnnotationTypeBuilder builder : annotationBuilders) {
149 a.add(builder.toInstance());
151 this.annotations = ImmutableList.copyOf(a);
153 final List<String> p = new ArrayList<>();
154 for (final AnnotationType.Parameter parameter : parameters) {
155 p.add(parameter.getName());
157 this.paramNames = ImmutableList.copyOf(p);
159 this.parameters = parameters.isEmpty() ? Collections.<AnnotationType.Parameter>emptyList()
160 : Collections.unmodifiableList(parameters);
164 public String getPackageName() {
169 public String getName() {
174 public String getFullyQualifiedName() {
175 return packageName + "." + name;
179 public List<AnnotationType> getAnnotations() {
184 public Parameter getParameter(final String paramName) {
185 if (paramName != null) {
186 for (final AnnotationType.Parameter parameter : parameters) {
187 if (parameter.getName().equals(paramName)) {
196 public List<Parameter> getParameters() {
201 public List<String> getParameterNames() {
206 public boolean containsParameters() {
207 return !parameters.isEmpty();
211 public int hashCode() {
212 final int prime = 31;
214 result = prime * result + ((name == null) ? 0 : name.hashCode());
215 result = prime * result
216 + ((packageName == null) ? 0 : packageName.hashCode());
221 public boolean equals(final Object obj) {
228 if (getClass() != obj.getClass()) {
231 AnnotationTypeImpl other = (AnnotationTypeImpl) obj;
233 if (other.name != null) {
236 } else if (!name.equals(other.name)) {
239 if (packageName == null) {
240 if (other.packageName != null) {
243 } else if (!packageName.equals(other.packageName)) {
250 public String toString() {
251 StringBuilder builder = new StringBuilder();
252 builder.append("AnnotationType [packageName=");
253 builder.append(packageName);
254 builder.append(", name=");
255 builder.append(name);
256 builder.append(", annotations=");
257 builder.append(annotations);
258 builder.append(", parameters=");
259 builder.append(parameters);
261 return builder.toString();
265 private static final class ParameterImpl implements AnnotationType.Parameter {
267 private final String name;
268 private final String value;
269 private final List<String> values;
271 public ParameterImpl(final String name, final String value) {
275 this.values = Collections.emptyList();
278 public ParameterImpl(final String name, final List<String> values) {
281 this.values = values;
286 public String getName() {
291 public String getValue() {
296 public List<String> getValues() {
301 public int hashCode() {
302 final int prime = 31;
304 result = prime * result + ((name == null) ? 0 : name.hashCode());
309 public boolean equals(final Object obj) {
316 if (getClass() != obj.getClass()) {
319 ParameterImpl other = (ParameterImpl) obj;
321 if (other.name != null) {
324 } else if (!name.equals(other.name)) {
331 public String toString() {
332 StringBuilder builder = new StringBuilder();
333 builder.append("ParameterImpl [name=");
334 builder.append(name);
335 builder.append(", value=");
336 builder.append(value);
337 builder.append(", values=");
338 builder.append(values);
340 return builder.toString();