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