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;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
17 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
18 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Type;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.TypeMemberBuilder;
22 import org.opendaylight.yangtools.util.LazyCollections;
24 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
25 private final String name;
26 private Type returnType;
27 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
28 private String comment = "";
29 private boolean isFinal;
30 private boolean isStatic;
31 private AccessModifier accessModifier;
33 public AbstractTypeMemberBuilder(final String name) {
38 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
39 Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
40 Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!");
42 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
43 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
47 protected Type getReturnType() {
51 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
52 return annotationBuilders;
55 protected String getComment() {
59 protected boolean isFinal() {
63 protected boolean isStatic() {
68 public AccessModifier getAccessModifier() {
69 return accessModifier;
73 public String getName() {
77 protected abstract T thisInstance();
80 public T setReturnType(final Type returnType) {
81 Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
82 this.returnType = returnType;
83 return thisInstance();
87 public T setAccessModifier(final AccessModifier modifier) {
88 Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
89 this.accessModifier = modifier;
90 return thisInstance();
94 public T setComment(final String comment) {
95 if (comment == null) {
98 this.comment = comment;
99 return thisInstance();
103 public T setFinal(final boolean isFinal) {
104 this.isFinal = isFinal;
105 return thisInstance();
109 public T setStatic(final boolean isStatic) {
110 this.isStatic = isStatic;
111 return thisInstance();
114 protected List<AnnotationType> toAnnotationTypes() {
115 final List<AnnotationType> annotations = new ArrayList<>();
116 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
117 if (annotBuilder != null) {
118 annotations.add(annotBuilder.toInstance());
122 return ImmutableList.copyOf(annotations);
126 public int hashCode() {
127 final int prime = 31;
129 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
130 result = prime * result
131 + ((getReturnType() == null) ? 0 : getReturnType().hashCode());
136 public boolean equals(final Object obj) {
143 if (getClass() != obj.getClass()) {
146 MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
147 if (getName() == null) {
148 if (other.getName() != null) {
151 } else if (!getName().equals(other.getName())) {
154 if (getReturnType() == null) {
155 if (other.getReturnType() != null) {
158 } else if (!getReturnType().equals(other.getReturnType())) {
165 public String toString() {
166 StringBuilder builder = new StringBuilder();
167 builder.append("GeneratedPropertyImpl [name=");
168 builder.append(getName());
169 builder.append(", annotations=");
170 builder.append(getAnnotationBuilders());
171 builder.append(", comment=");
172 builder.append(getComment());
173 builder.append(", returnType=");
174 builder.append(getReturnType());
175 builder.append(", isFinal=");
176 builder.append(isFinal());
177 builder.append(", modifier=");
178 builder.append(getAccessModifier());
180 return builder.toString();