Optimize binding types memory usage
[yangtools.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / generated / type / builder / AbstractTypeMemberBuilder.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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16
17 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
18 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Type;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.TypeMemberBuilder;
22 import org.opendaylight.yangtools.util.LazyCollections;
23
24 abstract class AbstractTypeMemberBuilder<T extends TypeMemberBuilder<T>> implements TypeMemberBuilder<T> {
25     private final String name;
26     private Type returnType;
27     private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
28     private String comment = "";
29     private boolean isFinal;
30     private boolean isStatic;
31     private AccessModifier accessModifier;
32
33     public AbstractTypeMemberBuilder(final String name) {
34         this.name = name;
35     }
36
37     @Override
38     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
39         Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!");
40         Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!");
41
42         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
43         annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
44         return builder;
45     }
46
47     protected Type getReturnType() {
48         return returnType;
49     }
50
51     protected Iterable<AnnotationTypeBuilder> getAnnotationBuilders() {
52         return annotationBuilders;
53     }
54
55     protected String getComment() {
56         return comment;
57     }
58
59     protected boolean isFinal() {
60         return isFinal;
61     }
62
63     protected boolean isStatic() {
64         return isStatic;
65     }
66
67     @Override
68     public AccessModifier getAccessModifier() {
69         return accessModifier;
70     }
71
72     @Override
73     public String getName() {
74         return name;
75     }
76
77     protected abstract T thisInstance();
78
79     @Override
80     public T setReturnType(final Type returnType) {
81         Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!");
82         this.returnType = returnType;
83         return thisInstance();
84     }
85
86     @Override
87     public T setAccessModifier(final AccessModifier modifier) {
88         Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!");
89         this.accessModifier = modifier;
90         return thisInstance();
91     }
92
93     @Override
94     public T setComment(final String comment) {
95         if (comment == null) {
96             this.comment = "";
97         }
98         this.comment = comment;
99         return thisInstance();
100     }
101
102     @Override
103     public T setFinal(final boolean isFinal) {
104         this.isFinal = isFinal;
105         return thisInstance();
106     }
107
108     @Override
109     public T setStatic(final boolean isStatic) {
110         this.isStatic = isStatic;
111         return thisInstance();
112     }
113
114     protected List<AnnotationType> toAnnotationTypes() {
115         final List<AnnotationType> annotations = new ArrayList<>();
116         for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) {
117             if (annotBuilder != null) {
118                 annotations.add(annotBuilder.toInstance());
119             }
120         }
121
122         return ImmutableList.copyOf(annotations);
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
130         result = prime * result
131                 + ((getReturnType() == null) ? 0 : getReturnType().hashCode());
132         return result;
133     }
134
135     @Override
136     public boolean equals(final Object obj) {
137         if (this == obj) {
138             return true;
139         }
140         if (obj == null) {
141             return false;
142         }
143         if (getClass() != obj.getClass()) {
144             return false;
145         }
146         MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
147         if (getName() == null) {
148             if (other.getName() != null) {
149                 return false;
150             }
151         } else if (!getName().equals(other.getName())) {
152             return false;
153         }
154         if (getReturnType() == null) {
155             if (other.getReturnType() != null) {
156                 return false;
157             }
158         } else if (!getReturnType().equals(other.getReturnType())) {
159             return false;
160         }
161         return true;
162     }
163
164     @Override
165     public String toString() {
166         StringBuilder builder = new StringBuilder();
167         builder.append("GeneratedPropertyImpl [name=");
168         builder.append(getName());
169         builder.append(", annotations=");
170         builder.append(getAnnotationBuilders());
171         builder.append(", comment=");
172         builder.append(getComment());
173         builder.append(", returnType=");
174         builder.append(getReturnType());
175         builder.append(", isFinal=");
176         builder.append(isFinal());
177         builder.append(", modifier=");
178         builder.append(getAccessModifier());
179         builder.append("]");
180         return builder.toString();
181     }
182 }