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;
23 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
24 private final String name;
25 private Type returnType;
26 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
27 private String comment = "";
28 private boolean isFinal;
29 private boolean isStatic;
30 private AccessModifier accessModifier;
32 public AbstractTypeMemberBuilder(final String name) {
37 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
38 Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
39 Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!");
41 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
42 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
46 protected Type getReturnType() {
50 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
51 return annotationBuilders;
54 protected String getComment() {
58 protected boolean isFinal() {
62 protected boolean isStatic() {
67 public AccessModifier getAccessModifier() {
68 return accessModifier;
72 public String getName() {
76 protected abstract T thisInstance();
79 public T setReturnType(final Type returnType) {
80 Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
81 this.returnType = returnType;
82 return thisInstance();
86 public T setAccessModifier(final AccessModifier modifier) {
87 Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
88 this.accessModifier = modifier;
89 return thisInstance();
93 public T setComment(final String comment) {
94 if (comment == null) {
97 this.comment = comment;
98 return thisInstance();
102 public T setFinal(final boolean isFinal) {
103 this.isFinal = isFinal;
104 return thisInstance();
108 public T setStatic(final boolean isStatic) {
109 this.isStatic = isStatic;
110 return thisInstance();
113 protected List<AnnotationType> toAnnotationTypes() {
114 final List<AnnotationType> annotations = new ArrayList<>();
115 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
116 if (annotBuilder != null) {
117 annotations.add(annotBuilder.toInstance());
121 return ImmutableList.copyOf(annotations);
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + Objects.hashCode(getName());
129 result = prime * result + Objects.hashCode(getReturnType());
134 public boolean equals(final Object obj) {
141 if (getClass() != obj.getClass()) {
144 AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
145 if (getName() == null) {
146 if (other.getName() != null) {
149 } else if (!getName().equals(other.getName())) {
152 if (getReturnType() == null) {
153 if (other.getReturnType() != null) {
156 } else if (!getReturnType().equals(other.getReturnType())) {
163 public String toString() {
164 StringBuilder builder = new StringBuilder();
165 builder.append("GeneratedPropertyImpl [name=");
166 builder.append(getName());
167 builder.append(", annotations=");
168 builder.append(getAnnotationBuilders());
169 builder.append(", comment=");
170 builder.append(getComment());
171 builder.append(", returnType=");
172 builder.append(getReturnType());
173 builder.append(", isFinal=");
174 builder.append(isFinal());
175 builder.append(", modifier=");
176 builder.append(getAccessModifier());
178 return builder.toString();