Bug 735 - Part 1: Update ietf-restconf and ietf-yangtypes to newer versions
[yangtools.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / 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.yangtools.binding.generator.util.generated.type.builder;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
14 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
15 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
16 import org.opendaylight.yangtools.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
18 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
24
25 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements
26         GeneratedTypeBuilderBase<T> {
27
28     private String comment = "";
29
30     private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
31     private final List<Type> implementsTypes = new ArrayList<>();
32     private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
33     private final List<Constant> constants = new ArrayList<>();
34     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
35     private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
36     private final List<GeneratedTOBuilder> enclosedTransferObjects = new ArrayList<>();
37     private final List<GeneratedPropertyBuilder> properties = new ArrayList<>();
38     private boolean isAbstract;
39
40     public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
41         super(packageName, name);
42     }
43
44     protected String getComment() {
45         return comment;
46     }
47
48     protected List<AnnotationTypeBuilder> getAnnotations() {
49         return annotationBuilders;
50     }
51
52     public boolean isAbstract() {
53         return isAbstract;
54     }
55
56     public List<Type> getImplementsTypes() {
57         return implementsTypes;
58     }
59
60     protected List<EnumBuilder> getEnumerations() {
61         return enumDefinitions;
62     }
63
64     protected List<Constant> getConstants() {
65         return constants;
66     }
67
68     public List<MethodSignatureBuilder> getMethodDefinitions() {
69         return methodDefinitions;
70     }
71
72     protected List<GeneratedTypeBuilder> getEnclosedTypes() {
73         return enclosedTypes;
74     }
75
76     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
77         return enclosedTransferObjects;
78     }
79
80     protected abstract T thisInstance();
81
82     @Override
83     public GeneratedTOBuilder addEnclosingTransferObject(String name) {
84         if (name == null) {
85             throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
86         }
87         GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
88         enclosedTransferObjects.add(builder);
89         return builder;
90     }
91
92     @Override
93     public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
94         if (genTOBuilder == null) {
95             throw new IllegalArgumentException("Parameter genTOBuilder cannot be null!");
96         }
97         enclosedTransferObjects.add(genTOBuilder);
98         return thisInstance();
99     }
100
101     @Override
102     public T addComment(String comment) {
103         this.comment = comment;
104         return thisInstance();
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 T setAbstract(boolean isAbstract) {
123         this.isAbstract = isAbstract;
124         return thisInstance();
125     }
126
127     @Override
128     public T addImplementsType(Type genType) {
129         if (genType == null) {
130             throw new IllegalArgumentException("Type cannot be null");
131         }
132         implementsTypes.add(genType);
133         return thisInstance();
134     }
135
136     @Override
137     public Constant addConstant(Type type, String name, Object value) {
138         if (type == null) {
139             throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
140         }
141         if (name == null) {
142             throw new IllegalArgumentException("Name of constant cannot be null!");
143         }
144
145         final Constant constant = new ConstantImpl(this, type, name, value);
146         constants.add(constant);
147         return constant;
148     }
149
150     @Override
151     public EnumBuilder addEnumeration(String name) {
152         if (name == null) {
153             throw new IllegalArgumentException("Name of enumeration cannot be null!");
154         }
155         final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
156         enumDefinitions.add(builder);
157         return builder;
158     }
159
160     @Override
161     public MethodSignatureBuilder addMethod(String name) {
162         if (name == null) {
163             throw new IllegalArgumentException("Name of method cannot be null!");
164         }
165         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
166         builder.setAccessModifier(AccessModifier.PUBLIC);
167         builder.setAbstract(true);
168         methodDefinitions.add(builder);
169         return builder;
170     }
171
172     @Override
173     public boolean containsMethod(String name) {
174         if (name == null) {
175             throw new IllegalArgumentException("Parameter name can't be null");
176         }
177         for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
178             if (name.equals(methodDefinition.getName())) {
179                 return true;
180             }
181         }
182         return false;
183     }
184
185     @Override
186     public GeneratedPropertyBuilder addProperty(String name) {
187         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
188         builder.setAccessModifier(AccessModifier.PUBLIC);
189         properties.add(builder);
190         return builder;
191     }
192
193     @Override
194     public boolean containsProperty(String name) {
195         if (name == null) {
196             throw new IllegalArgumentException("Parameter name can't be null");
197         }
198         for (GeneratedPropertyBuilder property : properties) {
199             if (name.equals(property.getName())) {
200                 return true;
201             }
202         }
203         return false;
204     }
205
206     @Override
207     public int hashCode() {
208         final int prime = 31;
209         int result = 1;
210         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
211         result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
212         return result;
213     }
214
215     @Override
216     public boolean equals(Object obj) {
217         if (this == obj) {
218             return true;
219         }
220         if (obj == null) {
221             return false;
222         }
223         if (getClass() != obj.getClass()) {
224             return false;
225         }
226         AbstractGeneratedTypeBuilder<T> other = (AbstractGeneratedTypeBuilder<T>) obj;
227         if (getName() == null) {
228             if (other.getName() != null) {
229                 return false;
230             }
231         } else if (!getName().equals(other.getName())) {
232             return false;
233         }
234         if (getPackageName() == null) {
235             if (other.getPackageName() != null) {
236                 return false;
237             }
238         } else if (!getPackageName().equals(other.getPackageName())) {
239             return false;
240         }
241         return true;
242     }
243
244     public Type getParent() {
245         return null;
246     }
247
248     public List<GeneratedPropertyBuilder> getProperties() {
249         return properties;
250     }
251 }