Extended binding-model-api to support of Enclosed Generated Types and TOs.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / generated / type / builder / AbstractGeneratedTypeBuilder.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.controller.binding.generator.util.generated.type.builder;
9
10 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;
11 import org.opendaylight.controller.sal.binding.model.api.Constant;
12 import org.opendaylight.controller.sal.binding.model.api.Type;
13 import org.opendaylight.controller.sal.binding.model.api.type.builder.*;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 abstract class AbstractGeneratedTypeBuilder implements GeneratedTypeBuilder {
19
20     private final String packageName;
21     private String comment = "";
22     private final String name;
23
24     private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
25     private final List<Type> implementsTypes = new ArrayList<>();
26     private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
27     private final List<Constant> constants = new ArrayList<>();
28     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
29     private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
30     private final List<GeneratedTOBuilder> enclosingTransferObjects = new ArrayList<>();
31     private boolean isAbstract;
32
33     public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
34         if (packageName == null) {
35             throw new IllegalArgumentException("Package Name for Generated Type cannot be null!");
36         }
37         if (name == null) {
38             throw new IllegalArgumentException("Name of Generated Type cannot be null!");
39         }
40         this.packageName = packageName;
41         this.name = name;
42     }
43
44     @Override
45     public String getPackageName() {
46         return packageName;
47     }
48
49     @Override
50     public String getName() {
51         return name;
52     }
53
54     @Override
55     public String getFullyQualifiedName() {
56         return packageName + "." + name;
57     }
58
59
60     protected String getComment() {
61         return comment;
62     }
63
64     protected List<AnnotationTypeBuilder> getAnnotations() {
65         return annotationBuilders;
66     }
67
68     protected boolean isAbstract() {
69         return isAbstract;
70     }
71
72     protected List<Type> getImplementsTypes() {
73         return implementsTypes;
74     }
75
76     protected List<EnumBuilder> getEnumerations() {
77         return enumDefinitions;
78     }
79
80     protected List<Constant> getConstants() {
81         return constants;
82     }
83
84     protected List<MethodSignatureBuilder> getMethodDefinitions() {
85         return methodDefinitions;
86     }
87
88     protected List<GeneratedTypeBuilder> getEnclosedTypes() {
89         return enclosedTypes;
90     }
91
92     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
93         return enclosingTransferObjects;
94     }
95
96     @Override
97     public GeneratedTypeBuilder addEnclosingType(String name) {
98         if (name == null) {
99             throw new IllegalArgumentException("Name for Enclosing Generated Type cannot be null!");
100         }
101         GeneratedTypeBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
102         enclosedTypes.add(builder);
103         return builder;
104     }
105
106     @Override
107     public GeneratedTOBuilder addEnclosingTransferObject(String name) {
108         if (name == null) {
109             throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
110         }
111         GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
112         enclosingTransferObjects.add(builder);
113         return builder;
114     }
115
116     @Override
117     public void addComment(String comment) {
118         this.comment = comment;
119     }
120
121     @Override
122     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
123         if (packageName == null) {
124             throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
125         }
126         if (name == null) {
127             throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
128         }
129
130         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
131         annotationBuilders.add(builder);
132         return builder;
133     }
134
135      @Override
136      public void setAbstract(boolean isAbstract) {
137          this.isAbstract = isAbstract;
138      }
139
140      @Override
141     public boolean addImplementsType(Type genType) {
142         if (genType == null) {
143             throw new IllegalArgumentException("Type cannot be null");
144         }
145         return implementsTypes.add(genType);
146     }
147
148     @Override
149     public Constant addConstant(Type type, String name, Object value) {
150         if (type == null) {
151             throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
152         }
153         if (name == null) {
154             throw new IllegalArgumentException("Name of constant cannot be null!");
155         }
156
157         final Constant constant = new ConstantImpl(this, type, name, value);
158         constants.add(constant);
159         return constant;
160     }
161
162     @Override
163     public EnumBuilder addEnumeration(String name) {
164         if (name == null) {
165             throw new IllegalArgumentException("Name of enumeration cannot be null!");
166         }
167         final EnumBuilder builder = new EnumerationBuilderImpl(
168                 getFullyQualifiedName(), name);
169         enumDefinitions.add(builder);
170         return builder;
171     }
172
173     @Override
174     public MethodSignatureBuilder addMethod(String name) {
175         if (name == null) {
176             throw new IllegalArgumentException("Name of method cannot be null!");
177         }
178         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
179         builder.setAccessModifier(AccessModifier.PUBLIC);
180         builder.setAbstract(true);
181         methodDefinitions.add(builder);
182         return builder;
183     }
184
185     @Override
186     public int hashCode() {
187         final int prime = 31;
188         int result = 1;
189         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
190         result = prime * result
191                 + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
192         return result;
193     }
194
195     @Override
196     public boolean equals(Object obj) {
197         if (this == obj) {
198             return true;
199         }
200         if (obj == null) {
201             return false;
202         }
203         if (getClass() != obj.getClass()) {
204             return false;
205         }
206         AbstractGeneratedTypeBuilder other = (AbstractGeneratedTypeBuilder) obj;
207         if (getName() == null) {
208             if (other.getName() != null) {
209                 return false;
210             }
211         } else if (!getName().equals(other.getName())) {
212             return false;
213         }
214         if (getPackageName() == null) {
215             if (other.getPackageName() != null) {
216                 return false;
217             }
218         } else if (!getPackageName().equals(other.getPackageName())) {
219             return false;
220         }
221         return true;
222     }
223 }