1d4fc8d88757d972e3f1647e93cb6733489994c9
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.protocol.configuration;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import lombok.ToString;
26 import org.apache.commons.lang3.builder.EqualsBuilder;
27 import org.apache.commons.lang3.builder.HashCodeBuilder;
28 import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
29 import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
30 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
31 import org.onap.policy.common.gson.annotation.GsonJsonProperty;
32
33 /**
34  * Maven Related Information.
35  *
36  */
37 @ToString
38 public class DroolsConfiguration {
39
40     /**
41      * Maven Artifact ID
42      * (Required).
43      *
44      */
45     @GsonJsonProperty("artifactId")
46     private String artifactId;
47
48     /**
49      * Maven Group ID
50      * (Required).
51      *
52      */
53     @GsonJsonProperty("groupId")
54     private String groupId;
55
56     /**
57      * Maven Version
58      * (Required).
59      *
60      */
61     @GsonJsonProperty("version")
62     private String version;
63
64     @GsonJsonIgnore
65     private Map<String, Object> additionalProperties = new HashMap<>();
66
67     protected static final Object NOT_FOUND_VALUE = new Object();
68
69     /**
70      * No args constructor for use in serialization.
71      *
72      */
73     public DroolsConfiguration() {
74         // Empty
75     }
76
77     /**
78      * Constructor.
79      *
80      * @param groupId group id
81      * @param artifactId artifact id
82      * @param version version
83      */
84     public DroolsConfiguration(String artifactId, String groupId, String version) {
85         this.artifactId = artifactId;
86         this.groupId = groupId;
87         this.version = version;
88     }
89
90     /**
91      * Maven Artifact ID
92      * (Required).
93      *
94      * @return
95      *     The artifactId
96      */
97     @GsonJsonProperty("artifactId")
98     public String getArtifactId() {
99         return artifactId;
100     }
101
102     /**
103      * Maven Artifact ID
104      * (Required).
105      *
106      * @param artifactId
107      *     The artifactId
108      */
109     @GsonJsonProperty("artifactId")
110     public void setArtifactId(String artifactId) {
111         this.artifactId = artifactId;
112     }
113
114     public DroolsConfiguration withArtifactId(String artifactId) {
115         this.artifactId = artifactId;
116         return this;
117     }
118
119     /**
120      * Maven Group ID
121      * (Required).
122      *
123      * @return
124      *     The groupId
125      */
126     @GsonJsonProperty("groupId")
127     public String getGroupId() {
128         return groupId;
129     }
130
131     /**
132      * Maven Group ID
133      * (Required).
134      *
135      * @param groupId
136      *     The groupId
137      */
138     @GsonJsonProperty("groupId")
139     public void setGroupId(String groupId) {
140         this.groupId = groupId;
141     }
142
143     public DroolsConfiguration withGroupId(String groupId) {
144         this.groupId = groupId;
145         return this;
146     }
147
148     /**
149      * Maven Version
150      * (Required).
151      *
152      * @return
153      *     The version
154      */
155     @GsonJsonProperty("version")
156     public String getVersion() {
157         return version;
158     }
159
160     /**
161      * Maven Version
162      * (Required).
163      *
164      * @param version
165      *     The version
166      */
167     @GsonJsonProperty("version")
168     public void setVersion(String version) {
169         this.version = version;
170     }
171
172     public DroolsConfiguration withVersion(String version) {
173         this.version = version;
174         return this;
175     }
176
177     @GsonJsonAnyGetter
178     public Map<String, Object> getAdditionalProperties() {
179         return this.additionalProperties;
180     }
181
182     @GsonJsonAnySetter
183     public void setAdditionalProperty(String name, Object value) {
184         this.additionalProperties.put(name, value);
185     }
186
187     public DroolsConfiguration withAdditionalProperty(String name, Object value) {
188         this.additionalProperties.put(name, value);
189         return this;
190     }
191
192     protected boolean declaredProperty(String name, Object value) {
193         switch (name) {
194             case "artifactId":
195                 callSetArtifactId(value);
196                 return true;
197             case "groupId":
198                 callSetGroupId(value);
199                 return true;
200             case "version":
201                 callSetVersion(value);
202                 return true;
203             default:
204                 return false;
205         }
206     }
207
208     protected Object declaredPropertyOrNotFound(String name, Object notFoundValue) {
209         switch (name) {
210             case "artifactId":
211                 return getArtifactId();
212             case "groupId":
213                 return getGroupId();
214             case "version":
215                 return getVersion();
216             default:
217                 return notFoundValue;
218         }
219     }
220
221     /**
222      * Get declared property.
223      *
224      * @param name property name
225      * @return the property object
226      */
227     @SuppressWarnings({
228         "unchecked"
229         })
230     public <T> T get(String name) {
231         Object value = declaredPropertyOrNotFound(name, DroolsConfiguration.NOT_FOUND_VALUE);
232         if (DroolsConfiguration.NOT_FOUND_VALUE != value) {
233             return (T) value;
234         } else {
235             return (T) getAdditionalProperties().get(name);
236         }
237     }
238
239     /**
240      * Set property value.
241      *
242      * @param name property name
243      * @param value property value
244      */
245     public void set(String name, Object value) {
246         if (!declaredProperty(name, value)) {
247             getAdditionalProperties().put(name, value);
248         }
249     }
250
251     /**
252      * Set property value and return object.
253      *
254      * @param name property name
255      * @param value property value
256      * @return this
257      */
258     public DroolsConfiguration with(String name, Object value) {
259         if (!declaredProperty(name, value)) {
260             getAdditionalProperties().put(name, value);
261         }
262         return this;
263     }
264
265     @Override
266     public int hashCode() {
267         return new HashCodeBuilder().append(artifactId).append(groupId).append(version).append(additionalProperties)
268                 .toHashCode();
269     }
270
271     @Override
272     public boolean equals(Object other) {
273         if (other == this) {
274             return true;
275         }
276         if (!(other instanceof DroolsConfiguration)) {
277             return false;
278         }
279         DroolsConfiguration rhs = ((DroolsConfiguration) other);
280         return new EqualsBuilder().append(artifactId, rhs.artifactId)
281                 .append(groupId, rhs.groupId).append(version, rhs.version)
282                 .append(additionalProperties, rhs.additionalProperties).isEquals();
283     }
284
285     /**
286      * Call set artifact id.
287      *
288      * @param value id
289      */
290     public void callSetArtifactId(Object value) {
291         if (value instanceof String) {
292             setArtifactId((String) value);
293         } else {
294             throw new IllegalArgumentException("property \"artifactId\" is of type \"java.lang.String\", but got "
295         + value.getClass().toString());
296         }
297     }
298
299     /**
300      * Call set group id.
301      *
302      * @param value id
303      */
304     public void callSetGroupId(Object value) {
305         if (value instanceof String) {
306             setGroupId((String) value);
307         } else {
308             throw new IllegalArgumentException("property \"groupId\" is of type \"java.lang.String\", but got "
309                     + value.getClass().toString());
310         }
311     }
312
313     /**
314      * Call set version.
315      *
316      * @param value version
317      */
318     public void callSetVersion(Object value) {
319         if (value instanceof String) {
320             setVersion((String) value);
321         } else {
322             throw new IllegalArgumentException("property \"version\" is of type \"java.lang.String\", but got "
323                     + value.getClass().toString());
324         }
325     }
326 }