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