de3e19223f5fca053240f27ab1d0ca3031d63be0
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / generated / type / builder / GeneratedTOBuilderImpl.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.*;
11 import org.opendaylight.controller.sal.binding.model.api.type.builder.*;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16
17 public final class GeneratedTOBuilderImpl extends AbstractGeneratedTypeBuilder implements GeneratedTOBuilder {
18
19     private GeneratedTransferObject extendsType;
20     private final List<GeneratedPropertyBuilder> properties = new ArrayList<>();
21     private final List<GeneratedPropertyBuilder> equalsProperties = new ArrayList<>();
22     private final List<GeneratedPropertyBuilder> hashProperties = new ArrayList<>();
23     private final List<GeneratedPropertyBuilder> toStringProperties = new ArrayList<>();
24
25     public GeneratedTOBuilderImpl(String packageName, String name) {
26         super(packageName, name);
27         setAbstract(false);
28     }
29
30     @Override
31     public void setExtendsType(final GeneratedTransferObject genTransObj) {
32         if (genTransObj == null) {
33             throw new IllegalArgumentException("Generated Transfer Object cannot be null!");
34         }
35         extendsType = genTransObj;
36     }
37
38     @Override
39     public GeneratedPropertyBuilder addProperty(String name) {
40         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
41         builder.setAccessModifier(AccessModifier.PUBLIC);
42         properties.add(builder);
43         return builder;
44     }
45
46     /**
47      * Add new Method Signature definition for Generated Type Builder and
48      * returns Method Signature Builder for specifying all Method parameters.
49      * <br>
50      * Name of Method cannot be <code>null</code>,
51      * if it is <code>null</code> the method SHOULD throw {@link IllegalArgumentException}
52      * <br>
53      * By <i>Default</i> the MethodSignatureBuilder SHOULD be pre-set as
54      * {@link MethodSignatureBuilder#setAbstract(false)}, {@link MethodSignatureBuilder#setFinal(false)} and
55      * {@link MethodSignatureBuilder#setAccessModifier(PUBLIC)}
56      *
57      * @param name Name of Method
58      * @return <code>new</code> instance of Method Signature Builder.
59      */
60     @Override
61     public MethodSignatureBuilder addMethod(String name) {
62         final MethodSignatureBuilder builder = super.addMethod(name);
63         builder.setAbstract(false);
64         return builder;
65     }
66
67     @Override
68     public boolean addEqualsIdentity(GeneratedPropertyBuilder property) {
69         return equalsProperties.add(property);
70     }
71
72     @Override
73     public boolean addHashIdentity(GeneratedPropertyBuilder property) {
74         return hashProperties.add(property);
75     }
76
77     @Override
78     public boolean addToStringProperty(GeneratedPropertyBuilder property) {
79         return toStringProperties.add(property);
80     }
81
82     @Override
83     public GeneratedTransferObject toInstance() {
84         return new GeneratedTransferObjectImpl(null, getPackageName(), getName(), getComment(),
85                 getAnnotations(), isAbstract(), extendsType, getImplementsTypes(), getEnclosedTypes(),
86                 getEnclosedTransferObjects(), getConstants(), getEnumerations(), getMethodDefinitions(), properties,
87                 equalsProperties, hashProperties, toStringProperties);
88     }
89
90     @Override
91     public String toString() {
92         StringBuilder builder = new StringBuilder();
93         builder.append("GeneratedTransferObject [packageName=");
94         builder.append(getPackageName());
95         builder.append(", name=");
96         builder.append(getName());
97         builder.append(", comment=");
98         builder.append(getComment());
99         builder.append(", constants=");
100         builder.append(getConstants());
101         builder.append(", enumerations=");
102         builder.append(getEnumerations());
103         builder.append(", properties=");
104         builder.append(properties);
105         builder.append(", equalsProperties=");
106         builder.append(equalsProperties);
107         builder.append(", hashCodeProperties=");
108         builder.append(hashProperties);
109         builder.append(", stringProperties=");
110         builder.append(toStringProperties);
111         builder.append(", annotations=");
112         builder.append(getAnnotations());
113         builder.append(", methods=");
114         builder.append(getMethodDefinitions());
115         builder.append("]");
116         return builder.toString();
117     }
118
119     private static final class GeneratedTransferObjectImpl extends AbstractGeneratedType implements
120             GeneratedTransferObject {
121
122         private final List<GeneratedProperty> properties;
123         private final List<GeneratedProperty> equalsProperties;
124         private final List<GeneratedProperty> hashCodeProperties;
125         private final List<GeneratedProperty> stringProperties;
126         private final GeneratedTransferObject extendsType;
127
128         GeneratedTransferObjectImpl(final Type parent,
129                     final String packageName,
130                                            final String name, final String comment,
131                                            final List<AnnotationTypeBuilder> annotationBuilders,
132                                            final boolean isAbstract,
133                                            final GeneratedTransferObject extendsType,
134                                            final List<Type> implementsTypes,
135                                            final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
136                                            final List<GeneratedTOBuilder> enclosedGenTOBuilders,
137                                            final List<Constant> constants,
138                                            final List<EnumBuilder> enumBuilders,
139                                            final List<MethodSignatureBuilder> methodBuilders,
140                                            final List<GeneratedPropertyBuilder> propBuilders,
141                                            final List<GeneratedPropertyBuilder> equalsBuilders,
142                                            final List<GeneratedPropertyBuilder> hashCodeBuilders,
143                                            final List<GeneratedPropertyBuilder> stringBuilders) {
144             super(parent, packageName, name, comment, annotationBuilders, isAbstract, implementsTypes,
145                     enclosedGenTypeBuilders,
146                     enclosedGenTOBuilders, enumBuilders, constants, methodBuilders);
147             this.extendsType = extendsType;
148             this.properties = toUnmodifiableProperties(propBuilders);
149             this.equalsProperties = toUnmodifiableProperties(equalsBuilders);
150             this.hashCodeProperties = toUnmodifiableProperties(hashCodeBuilders);
151             this.stringProperties = toUnmodifiableProperties(stringBuilders);
152         }
153
154         private List<GeneratedProperty> toUnmodifiableProperties(
155                 final List<GeneratedPropertyBuilder> propBuilders) {
156             final List<GeneratedProperty> properties = new ArrayList<>();
157             for (final GeneratedPropertyBuilder builder : propBuilders) {
158                 properties.add(builder.toInstance(this));
159             }
160             return Collections.unmodifiableList(properties);
161         }
162
163         @Override
164         public GeneratedTransferObject getExtends() {
165             return extendsType;
166         }
167
168         @Override
169         public List<GeneratedProperty> getProperties() {
170             return properties;
171         }
172
173         @Override
174         public List<GeneratedProperty> getEqualsIdentifiers() {
175             return equalsProperties;
176         }
177
178         @Override
179         public List<GeneratedProperty> getHashCodeIdentifiers() {
180             return hashCodeProperties;
181         }
182
183         @Override
184         public List<GeneratedProperty> getToStringIdentifiers() {
185             return stringProperties;
186         }
187
188         @Override
189         public String toString() {
190             StringBuilder builder = new StringBuilder();
191             builder.append("GeneratedTransferObject [packageName=");
192             builder.append(getPackageName());
193             builder.append(", name=");
194             builder.append(getName());
195             builder.append(", comment=");
196             builder.append(", annotations=");
197             builder.append(getAnnotations());
198             builder.append(getComment());
199             builder.append(", extends=");
200             builder.append(getExtends());
201             builder.append(", implements=");
202             builder.append(getImplements());
203             builder.append(", enclosedTypes=");
204             builder.append(getEnclosedTypes());
205             builder.append(", constants=");
206             builder.append(getConstantDefinitions());
207             builder.append(", enumerations=");
208             builder.append(getEnumerations());
209             builder.append(", properties=");
210             builder.append(properties);
211             builder.append(", equalsProperties=");
212             builder.append(equalsProperties);
213             builder.append(", hashCodeProperties=");
214             builder.append(hashCodeProperties);
215             builder.append(", stringProperties=");
216             builder.append(stringProperties);
217             builder.append(", methods=");
218             builder.append(getMethodDefinitions());
219             builder.append("]");
220             return builder.toString();
221         }
222     }
223 }