b10aaaa60db0af309ee184bc7a7ce21d4a11b706
[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.yang.binding.AbstractAugmentable;
30 import org.opendaylight.yangtools.yang.binding.Augmentation;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32
33 /**
34  * Class that builds {@link UserdataBuilder} instances. Overall design of the class is that of a
35  * <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a>, where method chaining is used.
36  *
37  * <p>
38  * In general, this class is supposed to be used like this template:
39  *
40  * <pre>
41  *   <code>
42  *     UserdataBuilder createTarget(int fooXyzzy, int barBaz) {
43  *         return new UserdataBuilderBuilder()
44  *             .setFoo(new FooBuilder().setXyzzy(fooXyzzy).build())
45  *             .setBar(new BarBuilder().setBaz(barBaz).build())
46  *             .build();
47  *     }
48  *   </code>
49  * </pre>
50  *
51  * <p>
52  * This pattern is supported by the immutable nature of UserdataBuilder, as instances can be freely passed around
53  * without worrying about synchronization issues.
54  *
55  * <p>
56  * As a side note: method chaining results in:
57  * <ul>
58  * <li>very efficient Java bytecode, as the method invocation result, in this case the Builder reference, is on the
59  * stack, so further method invocations just need to fill method arguments for the next method invocation, which is
60  * terminated by {@link #build()}, which is then returned from the method</li>
61  * <li>better understanding by humans, as the scope of mutable state (the builder) is kept to a minimum and is very
62  * localized</li>
63  * <li>better optimization oportunities, as the object scope is minimized in terms of invocation (rather than method)
64  * stack, making <a href="https://en.wikipedia.org/wiki/Escape_analysis">escape analysis</a> a lot easier. Given enough
65  * compiler (JIT/AOT) prowess, the cost of th builder object can be completely eliminated</li>
66  * </ul>
67  *
68  * @see UserdataBuilder
69  * @see Builder
70  *
71  */
72 @Generated("mdsal-binding-generator")
73 public class UserdataBuilder {
74
75     private String _id;
76     private String _value;
77
78
79     Map<Class<? extends Augmentation<Userdata>>, Augmentation<Userdata>> augmentation = Collections.emptyMap();
80
81     public UserdataBuilder() {}
82
83
84
85     public UserdataBuilder(Userdata base) {
86         Map<Class<? extends Augmentation<Userdata>>, Augmentation<Userdata>> aug = base.augmentations();
87         if (!aug.isEmpty()) {
88             this.augmentation = new HashMap<>(aug);
89         }
90         this._id = base.getId();
91         this._value = base.getValue();
92     }
93
94
95     public String getId() {
96         return _id;
97     }
98
99     public String getValue() {
100         return _value;
101     }
102
103     @SuppressWarnings({"unchecked", "checkstyle:methodTypeParameterName"})
104     public <E$$ extends Augmentation<Userdata>> E$$ augmentation(Class<E$$> augmentationType) {
105         return (E$$) augmentation.get(Objects.requireNonNull(augmentationType));
106     }
107
108
109     public UserdataBuilder setId(final String value) {
110         this._id = value;
111         return this;
112     }
113
114     public UserdataBuilder setValue(final String value) {
115         this._value = value;
116         return this;
117     }
118
119     /**
120      * Add an augmentation to this builder's product.
121      *
122      * @param augmentation augmentation to be added
123      * @return this builder
124      * @throws NullPointerException if {@code augmentation} is null
125      */
126     public UserdataBuilder addAugmentation(Augmentation<Userdata> augmentation) {
127         Class<? extends Augmentation<Userdata>> augmentationType = augmentation.implementedInterface();
128         if (!(this.augmentation instanceof HashMap)) {
129             this.augmentation = new HashMap<>();
130         }
131
132         this.augmentation.put(augmentationType, augmentation);
133         return this;
134     }
135
136     /**
137      * Remove an augmentation from this builder's product. If this builder does not track such an augmentation type,
138      * this method does nothing.
139      *
140      * @param augmentationType augmentation type to be removed
141      * @return this builder
142      */
143     public UserdataBuilder removeAugmentation(Class<? extends Augmentation<Userdata>> augmentationType) {
144         if (this.augmentation instanceof HashMap) {
145             this.augmentation.remove(augmentationType);
146         }
147         return this;
148     }
149
150
151     public Userdata build() {
152         return new UserdataImpl(this);
153     }
154
155     private static final class UserdataImpl extends AbstractAugmentable<Userdata> implements Userdata {
156
157         private final String _id;
158         private final String _value;
159
160         UserdataImpl(UserdataBuilder base) {
161             super(base.augmentation);
162             this._id = base.getId();
163             this._value = base.getValue();
164         }
165
166         @Override
167         public String getId() {
168             return _id;
169         }
170
171         @Override
172         public String getValue() {
173             return _value;
174         }
175
176         private int hash = 0;
177         private volatile boolean hashValid = false;
178
179         @Override
180         public int hashCode() {
181             if (hashValid) {
182                 return hash;
183             }
184
185             final int result = Userdata.bindingHashCode(this);
186             hash = result;
187             hashValid = true;
188             return result;
189         }
190
191         @Override
192         public boolean equals(Object obj) {
193             return Userdata.bindingEquals(this, obj);
194         }
195
196         @Override
197         public String toString() {
198             return Userdata.bindingToString(this);
199         }
200
201         @Override
202         public Class<? extends DataObject> implementedInterface() {
203             return Userdata.class;
204         }
205     }
206 }