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