5aeb2c5177ee7f869a7e6b7d07ddc457d05ac04d
[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 addComment(String comment) {
104         this.comment = comment;
105     }
106
107     @Override
108     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
109         if (packageName == null) {
110             throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
111         }
112         if (name == null) {
113             throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
114         }
115
116         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
117         annotationBuilders.add(builder);
118         return builder;
119     }
120
121      @Override
122      public void setAbstract(boolean isAbstract) {
123          this.isAbstract = isAbstract;
124      }
125
126      @Override
127     public boolean addImplementsType(Type genType) {
128         if (genType == null) {
129             throw new IllegalArgumentException("Type cannot be null");
130         }
131         return implementsTypes.add(genType);
132     }
133
134     @Override
135     public Constant addConstant(Type type, String name, Object value) {
136         if (type == null) {
137             throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
138         }
139         if (name == null) {
140             throw new IllegalArgumentException("Name of constant cannot be null!");
141         }
142
143         final Constant constant = new ConstantImpl(this, type, name, value);
144         constants.add(constant);
145         return constant;
146     }
147
148     @Override
149     public EnumBuilder addEnumeration(String name) {
150         if (name == null) {
151             throw new IllegalArgumentException("Name of enumeration cannot be null!");
152         }
153         final EnumBuilder builder = new EnumerationBuilderImpl(
154                 getFullyQualifiedName(), name);
155         enumDefinitions.add(builder);
156         return builder;
157     }
158
159     @Override
160     public MethodSignatureBuilder addMethod(String name) {
161         if (name == null) {
162             throw new IllegalArgumentException("Name of method cannot be null!");
163         }
164         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
165         builder.setAccessModifier(AccessModifier.PUBLIC);
166         builder.setAbstract(true);
167         methodDefinitions.add(builder);
168         return builder;
169     }
170
171     @Override
172     public int hashCode() {
173         final int prime = 31;
174         int result = 1;
175         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
176         result = prime * result
177                 + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
178         return result;
179     }
180
181     @Override
182     public boolean equals(Object obj) {
183         if (this == obj) {
184             return true;
185         }
186         if (obj == null) {
187             return false;
188         }
189         if (getClass() != obj.getClass()) {
190             return false;
191         }
192         AbstractGeneratedTypeBuilder other = (AbstractGeneratedTypeBuilder) obj;
193         if (getName() == null) {
194             if (other.getName() != null) {
195                 return false;
196             }
197         } else if (!getName().equals(other.getName())) {
198             return false;
199         }
200         if (getPackageName() == null) {
201             if (other.getPackageName() != null) {
202                 return false;
203             }
204         } else if (!getPackageName().equals(other.getPackageName())) {
205             return false;
206         }
207         return true;
208     }
209 }