Integrate JavaTypeName as Identifier
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / generated / type / builder / AbstractTypeMemberBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.model.util.generated.type.builder;
9
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;
23
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;
32
33     public AbstractTypeMemberBuilder(final String name) {
34         this.name = name;
35     }
36
37     @Override
38     public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
39         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
40         this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
41         return builder;
42     }
43
44     public Type getReturnType() {
45         return this.returnType;
46     }
47
48     protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
49         return this.annotationBuilders;
50     }
51
52     protected String getComment() {
53         return this.comment;
54     }
55
56     protected boolean isFinal() {
57         return this.isFinal;
58     }
59
60     protected boolean isStatic() {
61         return this.isStatic;
62     }
63
64     @Override
65     public AccessModifier getAccessModifier() {
66         return this.accessModifier;
67     }
68
69     @Override
70     public String getName() {
71         return this.name;
72     }
73
74     protected abstract T thisInstance();
75
76     @Override
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();
81     }
82
83     @Override
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();
88     }
89
90     @Override
91     public T setComment(final String comment) {
92         if (comment == null) {
93             this.comment = "";
94         }
95         this.comment = comment;
96         return thisInstance();
97     }
98
99     @Override
100     public T setFinal(final boolean isFinal) {
101         this.isFinal = isFinal;
102         return thisInstance();
103     }
104
105     @Override
106     public T setStatic(final boolean isStatic) {
107         this.isStatic = isStatic;
108         return thisInstance();
109     }
110
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());
116             }
117         }
118
119         return ImmutableList.copyOf(annotations);
120     }
121
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = 1;
126         result = prime * result + Objects.hashCode(getName());
127         result = prime * result + Objects.hashCode(getReturnType());
128         return result;
129     }
130
131     @Override
132     public boolean equals(final Object obj) {
133         if (this == obj) {
134             return true;
135         }
136         if (obj == null) {
137             return false;
138         }
139         if (getClass() != obj.getClass()) {
140             return false;
141         }
142         final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
143         return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
144     }
145
146     @Override
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());
161         builder.append("]");
162         return builder.toString();
163     }
164 }