2 * Copyright (c) 2017 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.javav2.generator.util.generated.type.builder;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ComparisonChain;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Ordering;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Objects;
18 import org.opendaylight.mdsal.binding.javav2.generator.util.AbstractBaseType;
19 import org.opendaylight.mdsal.binding.javav2.model.api.AnnotationType;
20 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.yangtools.util.LazyCollections;
24 final class AnnotationTypeBuilderImpl extends AbstractBaseType implements AnnotationTypeBuilder {
26 private final String packageName;
27 private final String name;
28 private List<AnnotationTypeBuilder> annotationBuilders = ImmutableList.of();
29 private List<AnnotationType.Parameter> parameters = ImmutableList.of();
31 AnnotationTypeBuilderImpl(final String packageName, final String name) {
32 super(packageName, name, true, null);
33 this.packageName = packageName;
38 public AnnotationTypeBuilder addAnnotation(final String pkgName, final String typeName) {
39 if (pkgName != null && typeName != null) {
40 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(pkgName, typeName);
41 if (!annotationBuilders.contains(builder)) {
42 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
49 private boolean addParameter(final ParameterImpl param) {
50 if (!parameters.contains(param)) {
51 parameters = LazyCollections.lazyAdd(parameters, param);
59 public boolean addParameter(final String paramName, final String value) {
60 if (paramName != null && value != null) {
61 final ParameterImpl param = new ParameterImpl(paramName, value);
62 return addParameter(param);
68 public boolean addParameters(final String paramName, final List<String> values) {
69 if (paramName != null && values != null) {
70 final ParameterImpl param = new ParameterImpl(paramName, values);
71 return addParameter(param);
77 public AnnotationType toInstance() {
78 return new AnnotationTypeImpl(packageName, name, annotationBuilders, parameters);
82 public int hashCode() {
83 return Objects.hash(name, packageName);
87 public boolean equals(final Object obj) {
94 if (getClass() != obj.getClass()) {
97 if (!(obj instanceof AnnotationTypeBuilderImpl)) {
101 AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj;
102 return Objects.equals(name, other.name) && Objects.equals(packageName, other.packageName);
106 public String toString() {
107 StringBuilder builder = new StringBuilder();
108 builder.append("AnnotationTypeBuilder [packageName=");
109 builder.append(packageName);
110 builder.append(", name=");
111 builder.append(name);
112 builder.append(", annotationBuilders=");
113 builder.append(annotationBuilders);
114 builder.append(", parameters=");
115 builder.append(parameters);
117 return builder.toString();
120 private static final class AnnotationTypeImpl implements AnnotationType {
122 private final String packageName;
123 private final String name;
124 private final List<AnnotationType> annotations;
125 private final List<AnnotationType.Parameter> parameters;
126 private final List<String> paramNames;
128 AnnotationTypeImpl(final String packageName, final String name,
129 final List<AnnotationTypeBuilder> annotationBuilders,
130 final List<AnnotationType.Parameter> parameters) {
131 this.packageName = packageName;
134 final List<AnnotationType> a = new ArrayList<>();
135 for (final AnnotationTypeBuilder builder : annotationBuilders) {
136 a.add(builder.toInstance());
138 this.annotations = ImmutableList.copyOf(a);
140 final List<String> p = new ArrayList<>();
141 for (final AnnotationType.Parameter parameter : parameters) {
142 p.add(parameter.getName());
144 this.paramNames = ImmutableList.copyOf(p);
146 this.parameters = parameters.isEmpty() ? ImmutableList.of()
147 : Collections.unmodifiableList(parameters);
151 public String getPackageName() {
156 public String getName() {
161 public String getFullyQualifiedName() {
162 return packageName + "." + name;
166 public List<AnnotationType> getAnnotations() {
171 public Parameter getParameter(final String paramName) {
172 if (paramName != null) {
173 for (final AnnotationType.Parameter parameter : parameters) {
174 if (parameter.getName().equals(paramName)) {
183 public List<Parameter> getParameters() {
188 public List<String> getParameterNames() {
193 public boolean containsParameters() {
194 return !parameters.isEmpty();
198 public int hashCode() {
199 return Objects.hash(name, packageName);
203 public int compareTo(AnnotationType other) {
204 return ComparisonChain.start()
205 .compare(this.name, other.getName())
206 .compare(this.packageName, other.getPackageName())
207 //FIXME: what is natural ordering for AnnotationType?
208 .compare(this.annotations, other.getAnnotations(),
209 Ordering.<AnnotationType>natural().lexicographical())
210 .compare(this.paramNames, other.getParameterNames(), Ordering.<String>natural().lexicographical())
215 public boolean equals(final Object obj) {
222 if (getClass() != obj.getClass()) {
225 if (!(obj instanceof AnnotationTypeImpl)) {
229 AnnotationTypeImpl other = (AnnotationTypeImpl) obj;
230 return Objects.equals(name, other.name) && Objects.equals(packageName, other.packageName);
234 public String toString() {
235 StringBuilder builder = new StringBuilder();
236 builder.append("AnnotationType [packageName=");
237 builder.append(packageName);
238 builder.append(", name=");
239 builder.append(name);
240 builder.append(", annotations=");
241 builder.append(annotations);
242 builder.append(", parameters=");
243 builder.append(parameters);
245 return builder.toString();
249 private static final class ParameterImpl implements AnnotationType.Parameter {
251 private final String name;
252 private final String value;
253 private final List<String> values;
255 ParameterImpl(final String name, final String value) {
258 this.values = ImmutableList.of();
261 ParameterImpl(final String name, final List<String> values) {
263 this.values = values;
268 public String getName() {
273 public String getSingleValue() {
278 public List<String> getValues() {
283 public int hashCode() {
284 return Objects.hash(name);
288 public boolean equals(final Object obj) {
295 if (getClass() != obj.getClass()) {
298 if (!(obj instanceof ParameterImpl)) {
301 ParameterImpl other = (ParameterImpl) obj;
302 return Objects.equals(name, other.name);
306 public String toString() {
307 StringBuilder builder = new StringBuilder();
308 builder.append("ParameterImpl [name=");
309 builder.append(name);
310 builder.append(", value=");
311 builder.append(value);
312 builder.append(", values=");
313 builder.append(values);
315 return builder.toString();