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