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.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Objects;
16 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
17 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.TypeMemberBuilder;
21 import org.opendaylight.yangtools.util.LazyCollections;
24 * @deprecated Use {org.opendaylight.mdsal.binding.generator.util.generated.type.builder.AbstractTypeMemberBuilder} instead.
27 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
28 private final String name;
29 private Type returnType;
30 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
31 private String comment = "";
32 private boolean isFinal;
33 private boolean isStatic;
34 private AccessModifier accessModifier;
36 public AbstractTypeMemberBuilder(final String name) {
41 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
42 Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
43 Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!");
45 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
46 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
50 protected Type getReturnType() {
54 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
55 return annotationBuilders;
58 protected String getComment() {
62 protected boolean isFinal() {
66 protected boolean isStatic() {
71 public AccessModifier getAccessModifier() {
72 return accessModifier;
76 public String getName() {
80 protected abstract T thisInstance();
83 public T setReturnType(final Type returnType) {
84 Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
85 this.returnType = returnType;
86 return thisInstance();
90 public T setAccessModifier(final AccessModifier modifier) {
91 Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
92 this.accessModifier = modifier;
93 return thisInstance();
97 public T setComment(final String comment) {
98 if (comment == null) {
101 this.comment = comment;
102 return thisInstance();
106 public T setFinal(final boolean isFinal) {
107 this.isFinal = isFinal;
108 return thisInstance();
112 public T setStatic(final boolean isStatic) {
113 this.isStatic = isStatic;
114 return thisInstance();
117 protected List<AnnotationType> toAnnotationTypes() {
118 final List<AnnotationType> annotations = new ArrayList<>();
119 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
120 if (annotBuilder != null) {
121 annotations.add(annotBuilder.toInstance());
125 return ImmutableList.copyOf(annotations);
129 public int hashCode() {
130 final int prime = 31;
132 result = prime * result + Objects.hashCode(getName());
133 result = prime * result + Objects.hashCode(getReturnType());
138 public boolean equals(final Object obj) {
145 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();