Replace copyTo methods with copy constructors
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptKey.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.persistence.Column;
28 import javax.persistence.Embeddable;
29
30 import lombok.EqualsAndHashCode;
31 import lombok.Getter;
32 import lombok.NonNull;
33 import lombok.ToString;
34
35 import org.onap.policy.common.utils.validation.Assertions;
36 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
37
38 /**
39  * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
40  * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
41  * identified using a {@link PfReferenceKey} key.
42  *
43  * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
44  * regular expressions respectively.
45  */
46 @Embeddable
47 @Getter
48 @ToString
49 @EqualsAndHashCode(callSuper = false)
50 public class PfConceptKey extends PfKey {
51     private static final long serialVersionUID = 8932717618579392561L;
52
53     private static final String NAME_TOKEN = "name";
54     private static final String VERSION_TOKEN = "version";
55
56     @Column(name = NAME_TOKEN, length = 120)
57     private String name;
58
59     @Column(name = VERSION_TOKEN, length = 20)
60     private String version;
61
62     /**
63      * The default constructor creates a null concept key.
64      */
65     public PfConceptKey() {
66         this(NULL_KEY_NAME, NULL_KEY_VERSION);
67     }
68
69     /**
70      * Copy constructor.
71      *
72      * @param copyConcept the concept to copy from
73      */
74     public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
75         super(copyConcept);
76         this.name = copyConcept.name;
77         this.version = copyConcept.version;
78     }
79
80     /**
81      * Constructor to create a key with the specified name and version.
82      *
83      * @param name the key name
84      * @param version the key version
85      */
86     public PfConceptKey(@NonNull final String name, @NonNull final String version) {
87         super();
88         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
89         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
90     }
91
92     /**
93      * Constructor to create a key using the key and version from the specified key ID.
94      *
95      * @param id the key ID in a format that respects the KEY_ID_REGEXP
96      */
97     public PfConceptKey(@NonNull final String id) {
98         // Check the incoming ID is valid
99         Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
100
101         // Split on colon, if the id passes the regular expression test above
102         // it'll have just one colon separating the name and version
103         // No need for range checks or size checks on the array
104         final String[] nameVersionArray = id.split(":");
105
106         // Return the new key
107         name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
108         version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
109     }
110
111     /**
112      * Get a null concept key.
113      *
114      * @return a null concept key
115      */
116     public static final PfConceptKey getNullKey() {
117         return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
118     }
119
120     @Override
121     public PfConceptKey getKey() {
122         return this;
123     }
124
125     public void setName(@NonNull final String name) {
126         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
127     }
128
129     public void setVersion(@NonNull final String version) {
130         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
131     }
132
133     @Override
134     public List<PfKey> getKeys() {
135         final List<PfKey> keyList = new ArrayList<>();
136         keyList.add(getKey());
137         return keyList;
138     }
139
140     @Override
141     public String getId() {
142         return name + ':' + version;
143     }
144
145     @Override
146     public boolean isNullKey() {
147         return this.equals(PfConceptKey.getNullKey());
148     }
149
150     /**
151      * Determines if the version is "null".
152      *
153      * @return {@code true} if the version is null, {@code false} otherwise
154      */
155     public boolean isNullVersion() {
156         return PfKey.NULL_KEY_VERSION.equals(getVersion());
157     }
158
159     @Override
160     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
161         if (!(otherKey instanceof PfConceptKey)) {
162             return Compatibility.DIFFERENT;
163         }
164         final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
165
166         if (this.equals(otherConceptKey)) {
167             return Compatibility.IDENTICAL;
168         }
169         if (!this.getName().equals(otherConceptKey.getName())) {
170             return Compatibility.DIFFERENT;
171         }
172
173         final String[] thisVersionArray = getVersion().split("\\.");
174         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
175
176         // There must always be at least one element in each version
177         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
178             return Compatibility.MAJOR;
179         }
180
181         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
182                         && !thisVersionArray[1].equals(otherVersionArray[1])) {
183             return Compatibility.MINOR;
184         }
185
186         return Compatibility.PATCH;
187     }
188
189     @Override
190     public boolean isCompatible(@NonNull final PfKey otherKey) {
191         if (!(otherKey instanceof PfConceptKey)) {
192             return false;
193         }
194         final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
195
196         final Compatibility compatibility = this.getCompatibility(otherConceptKey);
197
198         return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
199     }
200
201     @Override
202     public boolean isNewerThan(@NonNull final PfKey otherKey) {
203         Assertions.instanceOf(otherKey, PfConceptKey.class);
204
205         final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
206
207         if (this.equals(otherConceptKey)) {
208             return false;
209         }
210
211         if (!this.getName().equals(otherConceptKey.getName())) {
212             return this.getName().compareTo(otherConceptKey.getName()) > 0;
213         }
214
215         final String[] thisVersionArray = getVersion().split("\\.");
216         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
217
218         // There must always be at least one element in each version
219         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
220             return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
221         }
222
223         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
224                         && !thisVersionArray[1].equals(otherVersionArray[1])) {
225             return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
226         }
227
228         if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
229                         && !thisVersionArray[2].equals(otherVersionArray[2])) {
230             return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
231         }
232
233         return false;
234     }
235
236     @Override
237     public int getMajorVersion() {
238         final String[] versionArray = getVersion().split("\\.");
239
240         // There must always be at least one element in each version
241         return Integer.parseInt(versionArray[0]);
242     }
243
244     @Override
245     public int getMinorVersion() {
246         final String[] versionArray = getVersion().split("\\.");
247
248         if (versionArray.length >= 2) {
249             return Integer.parseInt(versionArray[1]);
250         }
251         else {
252             return 0;
253         }
254     }
255
256     @Override
257     public int getPatchVersion() {
258         final String[] versionArray = getVersion().split("\\.");
259
260         if (versionArray.length >= 3) {
261             return Integer.parseInt(versionArray[2]);
262         }
263         else {
264             return 0;
265         }
266     }
267
268     @Override
269     public PfValidationResult validate(final PfValidationResult result) {
270         final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
271                         NAME_REGEXP);
272         if (nameValidationErrorMessage != null) {
273             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
274                             "name invalid-" + nameValidationErrorMessage));
275         }
276
277         final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
278                         version, VERSION_REGEXP);
279         if (versionValidationErrorMessage != null) {
280             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
281                             "version invalid-" + versionValidationErrorMessage));
282         }
283
284         return result;
285     }
286
287     @Override
288     public void clean() {
289         name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
290         version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
291     }
292
293     @Override
294     public int compareTo(@NonNull final PfConcept otherObj) {
295         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
296
297         if (this == otherObj) {
298             return 0;
299         }
300         if (getClass() != otherObj.getClass()) {
301             return getClass().getName().compareTo(otherObj.getClass().getName());
302         }
303
304         final PfConceptKey other = (PfConceptKey) otherObj;
305
306         if (!name.equals(other.name)) {
307             return name.compareTo(other.name);
308         }
309         return version.compareTo(other.version);
310     }
311 }