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