Squash empty lists/maps
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / generated / type / builder / MethodSignatureBuilderImpl.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 java.util.Objects.requireNonNull;
11
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Objects;
15 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
16 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
17 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
20 import org.opendaylight.yangtools.util.LazyCollections;
21
22 final class MethodSignatureBuilderImpl extends AbstractTypeMemberBuilder<MethodSignatureBuilder>
23         implements MethodSignatureBuilder {
24
25     private List<MethodSignature.Parameter> parameters = Collections.emptyList();
26     private List<MethodSignature.Parameter> unmodifiableParams = Collections.emptyList();
27     private ValueMechanics mechanics = ValueMechanics.NORMAL;
28     private boolean isAbstract;
29     private boolean isDefault;
30
31     MethodSignatureBuilderImpl(final String name) {
32         super(name);
33     }
34
35     @Override
36     public MethodSignatureBuilder setAbstract(final boolean newIsAbstract) {
37         this.isAbstract = newIsAbstract;
38         return this;
39     }
40
41     @Override
42     public MethodSignatureBuilder setDefault(final boolean newIsDefault) {
43         this.isDefault = newIsDefault;
44         return this;
45     }
46
47
48     @Override
49     public MethodSignatureBuilder setMechanics(final ValueMechanics newMechanics) {
50         this.mechanics = requireNonNull(newMechanics);
51         return this;
52     }
53
54     @Override
55     public MethodSignatureBuilder addParameter(final Type type, final String name) {
56         this.parameters = LazyCollections.lazyAdd(this.parameters, new MethodParameterImpl(name, type));
57         this.unmodifiableParams = Collections.unmodifiableList(this.parameters);
58         return this;
59     }
60
61     @Override
62     protected MethodSignatureBuilder thisInstance() {
63         return this;
64     }
65
66     @Override
67     public MethodSignature toInstance(final Type definingType) {
68         final List<AnnotationType> annotations = toAnnotationTypes();
69         return new MethodSignatureImpl(definingType, getName(), annotations, getComment(), getAccessModifier(),
70                 getReturnType(), this.unmodifiableParams, isFinal(), this.isAbstract, isStatic(), isDefault, mechanics);
71     }
72
73     @Override
74     public int hashCode() {
75         final int prime = 31;
76         int result = 1;
77         result = prime * result + Objects.hashCode(getName());
78         result = prime * result + Objects.hashCode(this.parameters);
79         result = prime * result + Objects.hashCode(getReturnType());
80         return result;
81     }
82
83     @Override
84     public boolean equals(final Object obj) {
85         if (this == obj) {
86             return true;
87         }
88         if (obj == null || getClass() != obj.getClass()) {
89             return false;
90         }
91         final MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
92         return Objects.equals(getName(), other.getName())
93                 && Objects.equals(this.parameters, other.parameters)
94                 && Objects.equals(getReturnType(), other.getReturnType());
95     }
96
97     @Override
98     public String toString() {
99         return new StringBuilder().append("MethodSignatureBuilderImpl [name=").append(getName())
100                 .append(", returnType=").append(getReturnType())
101                 .append(", parameters=").append(this.parameters)
102                 .append(", annotationBuilders=").append(getAnnotationBuilders())
103                 .append(", comment=").append(getComment())
104                 .append(']').toString();
105     }
106 }