bb1268cc93977f229bf8eb654d264eb462a67ed4
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Objects;
28 import javax.annotation.processing.Generated;
29 import org.opendaylight.yangtools.concepts.Builder;
30 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable;
31 import org.opendaylight.yangtools.yang.binding.Augmentation;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33
34 /**
35  * Class that builds {@link UserdataBuilder} instances. Overall design of the class is that of a
36  * <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a>, where method chaining is used.
37  *
38  * <p>
39  * In general, this class is supposed to be used like this template:
40  *
41  * <pre>
42  *   <code>
43  *     UserdataBuilder createTarget(int fooXyzzy, int barBaz) {
44  *         return new UserdataBuilderBuilder()
45  *             .setFoo(new FooBuilder().setXyzzy(fooXyzzy).build())
46  *             .setBar(new BarBuilder().setBaz(barBaz).build())
47  *             .build();
48  *     }
49  *   </code>
50  * </pre>
51  *
52  * <p>
53  * This pattern is supported by the immutable nature of UserdataBuilder, as instances can be freely passed around
54  * without worrying about synchronization issues.
55  *
56  * <p>
57  * As a side note: method chaining results in:
58  * <ul>
59  * <li>very efficient Java bytecode, as the method invocation result, in this case the Builder reference, is on the
60  * stack, so further method invocations just need to fill method arguments for the next method invocation, which is
61  * terminated by {@link #build()}, which is then returned from the method</li>
62  * <li>better understanding by humans, as the scope of mutable state (the builder) is kept to a minimum and is very
63  * localized</li>
64  * <li>better optimization oportunities, as the object scope is minimized in terms of invocation (rather than method)
65  * stack, making <a href="https://en.wikipedia.org/wiki/Escape_analysis">escape analysis</a> a lot easier. Given enough
66  * compiler (JIT/AOT) prowess, the cost of th builder object can be completely eliminated</li>
67  * </ul>
68  *
69  * @see UserdataBuilder
70  * @see Builder
71  *
72  */
73 @Generated("mdsal-binding-generator")
74 public class UserdataBuilder implements Builder<Userdata> {
75
76     private String _id;
77     private String _value;
78
79
80     Map<Class<? extends Augmentation<Userdata>>, Augmentation<Userdata>> augmentation = Collections.emptyMap();
81
82     public UserdataBuilder() {}
83
84
85
86     public UserdataBuilder(Userdata base) {
87         Map<Class<? extends Augmentation<Userdata>>, Augmentation<Userdata>> aug = base.augmentations();
88         if (!aug.isEmpty()) {
89             this.augmentation = new HashMap<>(aug);
90         }
91         this._id = base.getId();
92         this._value = base.getValue();
93     }
94
95
96     public String getId() {
97         return _id;
98     }
99
100     public String getValue() {
101         return _value;
102     }
103
104     @SuppressWarnings({"unchecked", "checkstyle:methodTypeParameterName"})
105     public <E$$ extends Augmentation<Userdata>> E$$ augmentation(Class<E$$> augmentationType) {
106         return (E$$) augmentation.get(Objects.requireNonNull(augmentationType));
107     }
108
109
110     public UserdataBuilder setId(final String value) {
111         this._id = value;
112         return this;
113     }
114
115     public UserdataBuilder setValue(final String value) {
116         this._value = value;
117         return this;
118     }
119
120     /**
121      * Add an augmentation to this builder's product.
122      *
123      * @param augmentation augmentation to be added
124      * @return this builder
125      * @throws NullPointerException if {@code augmentation} is null
126      */
127     public UserdataBuilder addAugmentation(Augmentation<Userdata> augmentation) {
128         Class<? extends Augmentation<Userdata>> augmentationType = augmentation.implementedInterface();
129         if (!(this.augmentation instanceof HashMap)) {
130             this.augmentation = new HashMap<>();
131         }
132
133         this.augmentation.put(augmentationType, augmentation);
134         return this;
135     }
136
137     /**
138      * Remove an augmentation from this builder's product. If this builder does not track such an augmentation type,
139      * this method does nothing.
140      *
141      * @param augmentationType augmentation type to be removed
142      * @return this builder
143      */
144     public UserdataBuilder removeAugmentation(Class<? extends Augmentation<Userdata>> augmentationType) {
145         if (this.augmentation instanceof HashMap) {
146             this.augmentation.remove(augmentationType);
147         }
148         return this;
149     }
150
151     @Override
152     public Userdata build() {
153         return new UserdataImpl(this);
154     }
155
156     private static final class UserdataImpl extends AbstractAugmentable<Userdata> implements Userdata {
157
158         private final String _id;
159         private final String _value;
160
161         UserdataImpl(UserdataBuilder base) {
162             super(base.augmentation);
163             this._id = base.getId();
164             this._value = base.getValue();
165         }
166
167         @Override
168         public String getId() {
169             return _id;
170         }
171
172         @Override
173         public String getValue() {
174             return _value;
175         }
176
177         private int hash = 0;
178         private volatile boolean hashValid = false;
179
180         @Override
181         public int hashCode() {
182             if (hashValid) {
183                 return hash;
184             }
185
186             final int result = Userdata.bindingHashCode(this);
187             hash = result;
188             hashValid = true;
189             return result;
190         }
191
192         @Override
193         public boolean equals(Object obj) {
194             return Userdata.bindingEquals(this, obj);
195         }
196
197         @Override
198         public String toString() {
199             return Userdata.bindingToString(this);
200         }
201
202         @Override
203         public Class<? extends DataObject> implementedInterface() {
204             return Userdata.class;
205         }
206     }
207 }