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.TypeMemberComment;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder;
24 import org.opendaylight.yangtools.util.LazyCollections;
26 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
27 private final String name;
28 private Type returnType;
29 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
30 private TypeMemberComment comment;
31 private boolean isFinal;
32 private boolean isStatic;
33 private AccessModifier accessModifier;
35 AbstractTypeMemberBuilder(final String name) {
40 public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
41 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
42 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
46 public Type getReturnType() {
47 return this.returnType;
50 protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
51 return this.annotationBuilders;
54 protected TypeMemberComment getComment() {
58 protected boolean isFinal() {
62 protected boolean isStatic() {
67 public AccessModifier getAccessModifier() {
68 return this.accessModifier;
72 public String getName() {
76 protected abstract T thisInstance();
79 public T setReturnType(final Type newReturnType) {
80 checkArgument(newReturnType != null, "Return Type of member cannot be null!");
81 this.returnType = newReturnType;
82 return thisInstance();
86 public T setAccessModifier(final AccessModifier modifier) {
87 checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
88 this.accessModifier = modifier;
89 return thisInstance();
93 public T setComment(final TypeMemberComment newComment) {
94 this.comment = newComment;
95 return thisInstance();
99 public T setFinal(final boolean newIsFinal) {
100 this.isFinal = newIsFinal;
101 return thisInstance();
105 public T setStatic(final boolean newIsStatic) {
106 this.isStatic = newIsStatic;
107 return thisInstance();
110 protected List<AnnotationType> toAnnotationTypes() {
111 final List<AnnotationType> annotations = new ArrayList<>();
112 for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
113 if (annotBuilder != null) {
114 annotations.add(annotBuilder.build());
118 return ImmutableList.copyOf(annotations);
122 public int hashCode() {
123 final int prime = 31;
125 result = prime * result + Objects.hashCode(getName());
126 result = prime * result + Objects.hashCode(getReturnType());
131 public boolean equals(final Object obj) {
135 if (obj == null || getClass() != obj.getClass()) {
138 final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
139 return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
143 public String toString() {
144 return new StringBuilder().append("GeneratedPropertyImpl [name=").append(getName())
145 .append(", annotations=").append(getAnnotationBuilders())
146 .append(", comment=").append(getComment())
147 .append(", returnType=").append(getReturnType())
148 .append(", isFinal=").append(isFinal())
149 .append(", modifier=").append(getAccessModifier())
150 .append(']').toString();