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