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.binding2.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.binding2.generator.util.AbstractBaseType;
19 import org.opendaylight.mdsal.binding2.model.api.AnnotationType;
20 import org.opendaylight.mdsal.binding2.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 public AnnotationTypeBuilderImpl(final String packageName, final String name) {
32 super(packageName, name);
33 this.packageName = packageName;
38 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
39 if (packageName != null && name != null) {
40 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
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 public AnnotationTypeImpl(final String packageName, final String name,
129 final List<AnnotationTypeBuilder> annotationBuilders,
130 final List<AnnotationType.Parameter> parameters) {
132 this.packageName = packageName;
135 final List<AnnotationType> a = new ArrayList<>();
136 for (final AnnotationTypeBuilder builder : annotationBuilders) {
137 a.add(builder.toInstance());
139 this.annotations = ImmutableList.copyOf(a);
141 final List<String> p = new ArrayList<>();
142 for (final AnnotationType.Parameter parameter : parameters) {
143 p.add(parameter.getName());
145 this.paramNames = ImmutableList.copyOf(p);
147 this.parameters = parameters.isEmpty() ? ImmutableList.of()
148 : Collections.unmodifiableList(parameters);
152 public String getPackageName() {
157 public String getName() {
162 public String getFullyQualifiedName() {
163 return packageName + "." + name;
167 public List<AnnotationType> getAnnotations() {
172 public Parameter getParameter(final String paramName) {
173 if (paramName != null) {
174 for (final AnnotationType.Parameter parameter : parameters) {
175 if (parameter.getName().equals(paramName)) {
184 public List<Parameter> getParameters() {
189 public List<String> getParameterNames() {
194 public boolean containsParameters() {
195 return !parameters.isEmpty();
199 public int hashCode() {
200 return Objects.hash(name, packageName);
204 public int compareTo(AnnotationType other) {
205 return ComparisonChain.start()
206 .compare(this.name, other.getName())
207 .compare(this.packageName, other.getPackageName())
208 //FIXME: what is natural ordering for AnnotationType?
209 .compare(this.annotations, other.getAnnotations(), 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 public ParameterImpl(final String name, final String value) {
259 this.values = ImmutableList.of();
262 public ParameterImpl(final String name, final List<String> values) {
265 this.values = values;
270 public String getName() {
275 public String getSingleValue() {
280 public List<String> getValues() {
285 public int hashCode() {
286 return Objects.hash(name);
290 public boolean equals(final Object obj) {
297 if (getClass() != obj.getClass()) {
300 if (!(obj instanceof ParameterImpl)) {
303 ParameterImpl other = (ParameterImpl) obj;
304 return Objects.equals(name, other.name);
308 public String toString() {
309 StringBuilder builder = new StringBuilder();
310 builder.append("ParameterImpl [name=");
311 builder.append(name);
312 builder.append(", value=");
313 builder.append(value);
314 builder.append(", values=");
315 builder.append(values);
317 return builder.toString();