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.binding.model.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.mdsal.binding.model.api.AccessModifier;
17 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.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 JavaTypeName identifier) {
39 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
40 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
44 public Type getReturnType() {
45 return this.returnType;
48 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
49 return this.annotationBuilders;
52 protected String getComment() {
56 protected boolean isFinal() {
60 protected boolean isStatic() {
65 public AccessModifier getAccessModifier() {
66 return this.accessModifier;
70 public String getName() {
74 protected abstract T thisInstance();
77 public T setReturnType(final Type returnType) {
78 Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
79 this.returnType = returnType;
80 return thisInstance();
84 public T setAccessModifier(final AccessModifier modifier) {
85 Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
86 this.accessModifier = modifier;
87 return thisInstance();
91 public T setComment(final String comment) {
92 if (comment == null) {
95 this.comment = comment;
96 return thisInstance();
100 public T setFinal(final boolean isFinal) {
101 this.isFinal = isFinal;
102 return thisInstance();
106 public T setStatic(final boolean isStatic) {
107 this.isStatic = isStatic;
108 return thisInstance();
111 protected List<AnnotationType> toAnnotationTypes() {
112 final List<AnnotationType> annotations = new ArrayList<>();
113 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
114 if (annotBuilder != null) {
115 annotations.add(annotBuilder.build());
119 return ImmutableList.copyOf(annotations);
123 public int hashCode() {
124 final int prime = 31;
126 result = prime * result + Objects.hashCode(getName());
127 result = prime * result + Objects.hashCode(getReturnType());
132 public boolean equals(final Object obj) {
139 if (getClass() != obj.getClass()) {
142 final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
143 return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
147 public String toString() {
148 final StringBuilder builder = new StringBuilder();
149 builder.append("GeneratedPropertyImpl [name=");
150 builder.append(getName());
151 builder.append(", annotations=");
152 builder.append(getAnnotationBuilders());
153 builder.append(", comment=");
154 builder.append(getComment());
155 builder.append(", returnType=");
156 builder.append(getReturnType());
157 builder.append(", isFinal=");
158 builder.append(isFinal());
159 builder.append(", modifier=");
160 builder.append(getAccessModifier());
162 return builder.toString();