Bug 1411-3: MDSAL Binding2 Generator Util
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding2 / generator / util / generated / type / builder / AbstractTypeMemberBuilder.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.generator.util.generated.type.builder;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Objects;
17 import org.opendaylight.mdsal.binding2.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding2.model.api.AnnotationType;
19 import org.opendaylight.mdsal.binding2.model.api.Type;
20 import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.binding2.model.api.type.builder.TypeMemberBuilder;
22 import org.opendaylight.yangtools.util.LazyCollections;
23
24 @Beta
25 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
26
27     private final String name;
28     private String comment = "";
29     private boolean isFinal;
30     private boolean isStatic;
31     private Type returnType;
32     private AccessModifier accessModifier;
33     private List<AnnotationTypeBuilder> annotationBuilders = ImmutableList.of();
34
35     public AbstractTypeMemberBuilder(final String name) {
36         this.name = name;
37     }
38
39     @Override
40     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
41         Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
42         Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!");
43
44         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
45         annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
46         return builder;
47     }
48
49     protected abstract T thisInstance();
50
51     protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
52         return annotationBuilders;
53     }
54
55     @Override
56     public AccessModifier getAccessModifier() {
57         return accessModifier;
58     }
59
60     @Override
61     public String getName() {
62         return name;
63     }
64
65     protected String getComment() {
66         return comment;
67     }
68
69     protected boolean isFinal() {
70         return isFinal;
71     }
72
73     protected boolean isStatic() {
74         return isStatic;
75     }
76
77     protected Type getReturnType() {
78         return returnType;
79     }
80
81     @Override
82     public T setReturnType(final Type returnType) {
83         Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
84         this.returnType = returnType;
85         return thisInstance();
86     }
87
88     @Override
89     public T setAccessModifier(final AccessModifier modifier) {
90         Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
91         this.accessModifier = modifier;
92         return thisInstance();
93     }
94
95     @Override
96     public T setComment(final String comment) {
97         if (comment == null) {
98             this.comment = "";
99         }
100         this.comment = comment;
101         return thisInstance();
102     }
103
104     @Override
105     public T setFinal(final boolean isFinal) {
106         this.isFinal = isFinal;
107         return thisInstance();
108     }
109
110     @Override
111     public T setStatic(final boolean isStatic) {
112         this.isStatic = isStatic;
113         return thisInstance();
114     }
115
116     protected List<AnnotationType> toAnnotationTypes() {
117         final List<AnnotationType> annotations = new ArrayList<>();
118         for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
119             if (annotBuilder != null) {
120                 annotations.add(annotBuilder.toInstance());
121             }
122         }
123
124         return ImmutableList.copyOf(annotations);
125     }
126
127     @Override
128     public int hashCode() {
129         return Objects.hash(getName(), getReturnType());
130     }
131
132     @Override
133     public boolean equals(final Object obj) {
134         if (this == obj) {
135             return true;
136         }
137         if (obj == null) {
138             return false;
139         }
140
141         if (!(obj instanceof AbstractTypeMemberBuilder)) {
142             return false;
143         }
144         if (getClass() != obj.getClass()) {
145             return false;
146         }
147
148         AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
149         return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
150     }
151
152     @Override
153     public String toString() {
154         StringBuilder builder = new StringBuilder();
155         builder.append("GeneratedPropertyImpl [name=");
156         builder.append(getName());
157         builder.append(", annotations=");
158         builder.append(getAnnotationBuilders());
159         builder.append(", comment=");
160         builder.append(getComment());
161         builder.append(", returnType=");
162         builder.append(getReturnType());
163         builder.append(", isFinal=");
164         builder.append(isFinal());
165         builder.append(", modifier=");
166         builder.append(getAccessModifier());
167         builder.append("]");
168         return builder.toString();
169     }
170 }