aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java
blob: 105e6f7f82fafb9c36c2341756c3c7766fe4b93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019-2020, 2023 Nordix Foundation.
 *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.base;

import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import org.onap.policy.common.utils.validation.Assertions;

/**
 * A key, upon which other key subclasses can be built, providing implementations of the methods.
 */
@Getter
@ToString
public abstract class PfKeyImpl extends PfKey {
    @Serial
    private static final long serialVersionUID = 8932717618579392561L;

    public static final String NAME_TOKEN = "name";
    public static final String VERSION_TOKEN = "version";

    /**
     * The default constructor creates a null concept key.
     */
    protected PfKeyImpl() {
        this(NULL_KEY_NAME, NULL_KEY_VERSION);
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    protected PfKeyImpl(final PfKeyImpl copyConcept) {
        super(copyConcept);
        setName(copyConcept.getName());
        setVersion(copyConcept.getVersion());
    }

    /**
     * Constructor to create a key with the specified name and version.
     *
     * @param name the key name
     * @param version the key version
     */
    protected PfKeyImpl(@NonNull final String name, @NonNull final String version) {
        super();
        setName(name);
        setVersion(version);
    }

    /**
     * Constructor to create a key using the key and version from the specified key ID.
     *
     * @param id the key ID in a format that respects the KEY_ID_REGEXP
     */
    protected PfKeyImpl(@NonNull final String id) {
        // Check the incoming ID is valid
        Assertions.validateStringParameter("id", id, getKeyIdRegEx());

        // Split on colon, if the id passes the regular expression test above
        // it'll have just one colon separating the name and version
        // No need for range checks or size checks on the array
        final String[] nameVersionArray = id.split(":");

        // Return the new key
        setName(nameVersionArray[0]);
        setVersion(nameVersionArray[1]);
    }

    public abstract void setName(@NonNull final String name);

    public abstract void setVersion(@NonNull final String version);

    @Override
    public PfKeyImpl getKey() {
        return this;
    }

    @Override
    public List<PfKey> getKeys() {
        final List<PfKey> keyList = new ArrayList<>();
        keyList.add(getKey());
        return keyList;
    }

    @Override
    public String getId() {
        return getName() + ':' + getVersion();
    }

    @Override
    public boolean isNullKey() {
        return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
    }

    /**
     * Determines if the name is "null".
     *
     * @return {@code true} if the name is null, {@code false} otherwise
     */
    public boolean isNullName() {
        return PfKey.NULL_KEY_NAME.equals(getName());
    }

    /**
     * Determines if the version is "null".
     *
     * @return {@code true} if the version is null, {@code false} otherwise
     */
    public boolean isNullVersion() {
        return PfKey.NULL_KEY_VERSION.equals(getVersion());
    }

    @Override
    public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
        if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
            return Compatibility.DIFFERENT;
        }

        if (this.equals(otherConceptKey)) {
            return Compatibility.IDENTICAL;
        }
        if (!this.getName().equals(otherConceptKey.getName())) {
            return Compatibility.DIFFERENT;
        }

        final String[] thisVersionArray = getVersion().split("\\.");
        final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");

        // There must always be at least one element in each version
        if (!thisVersionArray[0].equals(otherVersionArray[0])) {
            return Compatibility.MAJOR;
        }

        if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
            && !thisVersionArray[1].equals(otherVersionArray[1])) {
            return Compatibility.MINOR;
        }

        return Compatibility.PATCH;
    }

    @Override
    public boolean isCompatible(@NonNull final PfKey otherKey) {
        if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
            return false;
        }

        final var compatibility = this.getCompatibility(otherConceptKey);

        return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
    }

    @Override
    public boolean isNewerThan(@NonNull final PfKey otherKey) {
        Assertions.instanceOf(otherKey, PfKeyImpl.class);

        final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;

        if (this.equals(otherConceptKey)) {
            return false;
        }

        if (!this.getName().equals(otherConceptKey.getName())) {
            return this.getName().compareTo(otherConceptKey.getName()) > 0;
        }

        final String[] thisVersionArray = getVersion().split("\\.");
        final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");

        // There must always be at least one element in each version
        if (!thisVersionArray[0].equals(otherVersionArray[0])) {
            return Integer.parseInt(thisVersionArray[0]) > Integer.parseInt(otherVersionArray[0]);
        }

        if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
            && !thisVersionArray[1].equals(otherVersionArray[1])) {
            return Integer.parseInt(thisVersionArray[1]) > Integer.parseInt(otherVersionArray[1]);
        }

        if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
            && !thisVersionArray[2].equals(otherVersionArray[2])) {
            return Integer.parseInt(thisVersionArray[2]) > Integer.parseInt(otherVersionArray[2]);
        }

        return false;
    }

    @Override
    public int getMajorVersion() {
        final String[] versionArray = getVersion().split("\\.");

        // There must always be at least one element in each version
        return Integer.parseInt(versionArray[0]);
    }

    @Override
    public int getMinorVersion() {
        final String[] versionArray = getVersion().split("\\.");

        if (versionArray.length >= 2) {
            return Integer.parseInt(versionArray[1]);
        } else {
            return 0;
        }
    }

    @Override
    public int getPatchVersion() {
        final String[] versionArray = getVersion().split("\\.");

        if (versionArray.length >= 3) {
            return Integer.parseInt(versionArray[2]);
        } else {
            return 0;
        }
    }

    @Override
    public void clean() {
        setName(getName());
        setVersion(getVersion());
    }

    @Override
    public int compareTo(@NonNull final PfConcept otherObj) {
        Assertions.argumentNotNull(otherObj, "comparison object may not be null");

        if (this == otherObj) {
            return 0;
        }
        if (getClass() != otherObj.getClass()) {
            return getClass().getName().compareTo(otherObj.getClass().getName());
        }

        final PfKeyImpl other = (PfKeyImpl) otherObj;

        if (!getName().equals(other.getName())) {
            return getName().compareTo(other.getName());
        }
        return getVersion().compareTo(other.getVersion());
    }

    /**
     * Gets the regular expression used to validate a name.
     *
     * @return the regular expression used to validate a name
     */
    protected String getNameRegEx() {
        return NAME_REGEXP;
    }

    /**
     * Gets the regular expression used to validate a version.
     *
     * @return the regular expression used to validate a version
     */
    protected String getVersionRegEx() {
        return VERSION_REGEXP;
    }

    /**
     * Gets the regular expression used to validate a key id.
     *
     * @return the regular expression used to validate a key id
     */
    protected String getKeyIdRegEx() {
        return KEY_ID_REGEXP;
    }
}