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
9 package org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Objects;
17 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding.javav2.model.api.AnnotationType;
19 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
20 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.TypeMemberBuilder;
22 import org.opendaylight.yangtools.util.LazyCollections;
25 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
27 private final String name;
28 private String comment = "";
29 private boolean isFinal;
30 private boolean isStatic;
31 private Type returnType;
32 private AccessModifier accessModifier;
33 private List<AnnotationTypeBuilder> annotationBuilders = ImmutableList.of();
35 AbstractTypeMemberBuilder(final String name) {
40 public AnnotationTypeBuilder addAnnotation(final String packageName, final String typeName) {
41 Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
42 Preconditions.checkArgument(typeName != null, "Annotation Type cannot have name as null!");
44 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, typeName);
45 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
49 protected abstract T thisInstance();
51 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
52 return annotationBuilders;
56 public AccessModifier getAccessModifier() {
57 return accessModifier;
61 public String getName() {
65 protected String getComment() {
69 protected boolean isFinal() {
73 protected boolean isStatic() {
77 public Type getReturnType() {
82 public T setReturnType(final Type retType) {
83 Preconditions.checkArgument(retType != null, "Return Type of member cannot be null!");
84 this.returnType = retType;
85 return thisInstance();
89 public T setAccessModifier(final AccessModifier modifier) {
90 Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
91 this.accessModifier = modifier;
92 return thisInstance();
96 public T setComment(final String commentText) {
97 if (commentText == null) {
100 this.comment = commentText;
101 return thisInstance();
105 public T setFinal(final boolean finalOrNot) {
106 this.isFinal = finalOrNot;
107 return thisInstance();
111 public T setStatic(final boolean staticOrNot) {
112 this.isStatic = staticOrNot;
113 return thisInstance();
116 protected List<AnnotationType> toAnnotationTypes() {
117 final List<AnnotationType> annotations = new ArrayList<>();
118 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
119 if (annotBuilder != null) {
120 annotations.add(annotBuilder.toInstance());
124 return ImmutableList.copyOf(annotations);
128 public int hashCode() {
129 return Objects.hash(getName(), getReturnType());
133 public boolean equals(final Object obj) {
141 if (!(obj instanceof AbstractTypeMemberBuilder)) {
144 if (getClass() != obj.getClass()) {
148 AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
149 return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
153 public String toString() {
154 StringBuilder builder = new StringBuilder();
155 builder.append("GeneratedPropertyImpl [name=");
156 builder.append(getName());
157 builder.append(", annotations=");
158 builder.append(getAnnotationBuilders());
159 builder.append(", comment=");
160 builder.append(getComment());
161 builder.append(", returnType=");
162 builder.append(getReturnType());
163 builder.append(", isFinal=");
164 builder.append(isFinal());
165 builder.append(", modifier=");
166 builder.append(getAccessModifier());
168 return builder.toString();