Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 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.io.Serial;
25 import java.util.ArrayList;
26 import java.util.List;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.common.utils.validation.Assertions;
31
32 /**
33  * A key, upon which other key subclasses can be built, providing implementations of the methods.
34  */
35 @Getter
36 @ToString
37 public abstract class PfKeyImpl extends PfKey {
38     @Serial
39     private static final long serialVersionUID = 8932717618579392561L;
40
41     public static final String NAME_TOKEN = "name";
42     public static final String VERSION_TOKEN = "version";
43
44     /**
45      * The default constructor creates a null concept key.
46      */
47     protected PfKeyImpl() {
48         this(NULL_KEY_NAME, NULL_KEY_VERSION);
49     }
50
51     /**
52      * Copy constructor.
53      *
54      * @param copyConcept the concept to copy from
55      */
56     protected PfKeyImpl(final PfKeyImpl copyConcept) {
57         super(copyConcept);
58         setName(copyConcept.getName());
59         setVersion(copyConcept.getVersion());
60     }
61
62     /**
63      * Constructor to create a key with the specified name and version.
64      *
65      * @param name the key name
66      * @param version the key version
67      */
68     protected PfKeyImpl(@NonNull final String name, @NonNull final String version) {
69         super();
70         setName(name);
71         setVersion(version);
72     }
73
74     /**
75      * Constructor to create a key using the key and version from the specified key ID.
76      *
77      * @param id the key ID in a format that respects the KEY_ID_REGEXP
78      */
79     protected PfKeyImpl(@NonNull final String id) {
80         // Check the incoming ID is valid
81         Assertions.validateStringParameter("id", id, getKeyIdRegEx());
82
83         // Split on colon, if the id passes the regular expression test above
84         // it'll have just one colon separating the name and version
85         // No need for range checks or size checks on the array
86         final String[] nameVersionArray = id.split(":");
87
88         // Return the new key
89         setName(nameVersionArray[0]);
90         setVersion(nameVersionArray[1]);
91     }
92
93     public abstract void setName(@NonNull final String name);
94
95     public abstract void setVersion(@NonNull final String version);
96
97     @Override
98     public PfKeyImpl getKey() {
99         return this;
100     }
101
102     @Override
103     public List<PfKey> getKeys() {
104         final List<PfKey> keyList = new ArrayList<>();
105         keyList.add(getKey());
106         return keyList;
107     }
108
109     @Override
110     public String getId() {
111         return getName() + ':' + getVersion();
112     }
113
114     @Override
115     public boolean isNullKey() {
116         return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
117     }
118
119     /**
120      * Determines if the name is "null".
121      *
122      * @return {@code true} if the name is null, {@code false} otherwise
123      */
124     public boolean isNullName() {
125         return PfKey.NULL_KEY_NAME.equals(getName());
126     }
127
128     /**
129      * Determines if the version is "null".
130      *
131      * @return {@code true} if the version is null, {@code false} otherwise
132      */
133     public boolean isNullVersion() {
134         return PfKey.NULL_KEY_VERSION.equals(getVersion());
135     }
136
137     @Override
138     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
139         if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
140             return Compatibility.DIFFERENT;
141         }
142
143         if (this.equals(otherConceptKey)) {
144             return Compatibility.IDENTICAL;
145         }
146         if (!this.getName().equals(otherConceptKey.getName())) {
147             return Compatibility.DIFFERENT;
148         }
149
150         final String[] thisVersionArray = getVersion().split("\\.");
151         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
152
153         // There must always be at least one element in each version
154         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
155             return Compatibility.MAJOR;
156         }
157
158         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
159             && !thisVersionArray[1].equals(otherVersionArray[1])) {
160             return Compatibility.MINOR;
161         }
162
163         return Compatibility.PATCH;
164     }
165
166     @Override
167     public boolean isCompatible(@NonNull final PfKey otherKey) {
168         if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
169             return false;
170         }
171
172         final var compatibility = this.getCompatibility(otherConceptKey);
173
174         return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
175     }
176
177     @Override
178     public boolean isNewerThan(@NonNull final PfKey otherKey) {
179         Assertions.instanceOf(otherKey, PfKeyImpl.class);
180
181         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
182
183         if (this.equals(otherConceptKey)) {
184             return false;
185         }
186
187         if (!this.getName().equals(otherConceptKey.getName())) {
188             return this.getName().compareTo(otherConceptKey.getName()) > 0;
189         }
190
191         final String[] thisVersionArray = getVersion().split("\\.");
192         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
193
194         // There must always be at least one element in each version
195         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
196             return Integer.parseInt(thisVersionArray[0]) > Integer.parseInt(otherVersionArray[0]);
197         }
198
199         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
200             && !thisVersionArray[1].equals(otherVersionArray[1])) {
201             return Integer.parseInt(thisVersionArray[1]) > Integer.parseInt(otherVersionArray[1]);
202         }
203
204         if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
205             && !thisVersionArray[2].equals(otherVersionArray[2])) {
206             return Integer.parseInt(thisVersionArray[2]) > Integer.parseInt(otherVersionArray[2]);
207         }
208
209         return false;
210     }
211
212     @Override
213     public int getMajorVersion() {
214         final String[] versionArray = getVersion().split("\\.");
215
216         // There must always be at least one element in each version
217         return Integer.parseInt(versionArray[0]);
218     }
219
220     @Override
221     public int getMinorVersion() {
222         final String[] versionArray = getVersion().split("\\.");
223
224         if (versionArray.length >= 2) {
225             return Integer.parseInt(versionArray[1]);
226         } else {
227             return 0;
228         }
229     }
230
231     @Override
232     public int getPatchVersion() {
233         final String[] versionArray = getVersion().split("\\.");
234
235         if (versionArray.length >= 3) {
236             return Integer.parseInt(versionArray[2]);
237         } else {
238             return 0;
239         }
240     }
241
242     @Override
243     public void clean() {
244         setName(getName());
245         setVersion(getVersion());
246     }
247
248     @Override
249     public int compareTo(@NonNull final PfConcept otherObj) {
250         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
251
252         if (this == otherObj) {
253             return 0;
254         }
255         if (getClass() != otherObj.getClass()) {
256             return getClass().getName().compareTo(otherObj.getClass().getName());
257         }
258
259         final PfKeyImpl other = (PfKeyImpl) otherObj;
260
261         if (!getName().equals(other.getName())) {
262             return getName().compareTo(other.getName());
263         }
264         return getVersion().compareTo(other.getVersion());
265     }
266
267     /**
268      * Gets the regular expression used to validate a name.
269      *
270      * @return the regular expression used to validate a name
271      */
272     protected String getNameRegEx() {
273         return NAME_REGEXP;
274     }
275
276     /**
277      * Gets the regular expression used to validate a version.
278      *
279      * @return the regular expression used to validate a version
280      */
281     protected String getVersionRegEx() {
282         return VERSION_REGEXP;
283     }
284
285     /**
286      * Gets the regular expression used to validate a key id.
287      *
288      * @return the regular expression used to validate a key id
289      */
290     protected String getKeyIdRegEx() {
291         return KEY_ID_REGEXP;
292     }
293 }