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 static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ImmutableList;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Objects;
17 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder;
23 import org.opendaylight.yangtools.util.LazyCollections;
25 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
26 private final String name;
27 private Type returnType;
28 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
29 private String comment = "";
30 private boolean isFinal;
31 private boolean isStatic;
32 private AccessModifier accessModifier;
34 AbstractTypeMemberBuilder(final String name) {
39 public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
40 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
41 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
45 public Type getReturnType() {
46 return this.returnType;
49 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
50 return this.annotationBuilders;
53 protected String getComment() {
57 protected boolean isFinal() {
61 protected boolean isStatic() {
66 public AccessModifier getAccessModifier() {
67 return this.accessModifier;
71 public String getName() {
75 protected abstract T thisInstance();
78 public T setReturnType(final Type newReturnType) {
79 checkArgument(newReturnType != null, "Return Type of member cannot be null!");
80 this.returnType = newReturnType;
81 return thisInstance();
85 public T setAccessModifier(final AccessModifier modifier) {
86 checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
87 this.accessModifier = modifier;
88 return thisInstance();
92 public T setComment(final String newComment) {
93 this.comment = newComment;
94 return thisInstance();
98 public T setFinal(final boolean newIsFinal) {
99 this.isFinal = newIsFinal;
100 return thisInstance();
104 public T setStatic(final boolean newIsStatic) {
105 this.isStatic = newIsStatic;
106 return thisInstance();
109 protected List<AnnotationType> toAnnotationTypes() {
110 final List<AnnotationType> annotations = new ArrayList<>();
111 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
112 if (annotBuilder != null) {
113 annotations.add(annotBuilder.build());
117 return ImmutableList.copyOf(annotations);
121 public int hashCode() {
122 final int prime = 31;
124 result = prime * result + Objects.hashCode(getName());
125 result = prime * result + Objects.hashCode(getReturnType());
130 public boolean equals(final Object obj) {
137 if (getClass() != obj.getClass()) {
140 final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
141 return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
145 public String toString() {
146 final StringBuilder builder = new StringBuilder();
147 builder.append("GeneratedPropertyImpl [name=");
148 builder.append(getName());
149 builder.append(", annotations=");
150 builder.append(getAnnotationBuilders());
151 builder.append(", comment=");
152 builder.append(getComment());
153 builder.append(", returnType=");
154 builder.append(getReturnType());
155 builder.append(", isFinal=");
156 builder.append(isFinal());
157 builder.append(", modifier=");
158 builder.append(getAccessModifier());
160 return builder.toString();