Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 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 lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import lombok.NonNull;
28
29 /**
30  * The key uniquely identifies every entity in the system. This class is an abstract class to give a common parent for
31  * all key types in the system.
32  */
33 @NoArgsConstructor(access = AccessLevel.PROTECTED)
34 public abstract class PfKey extends PfConcept {
35     @Serial
36     private static final long serialVersionUID = 6281159885962014041L;
37
38     /** Regular expression to specify the structure of key names. */
39     public static final String NAME_REGEXP = "^[A-Za-z0-9\\-_\\.]+$";
40
41     /** Regular expression to specify the structure of key versions. */
42     public static final String VERSION_REGEXP = "^(\\d+.){2}\\d+$";
43
44     /** Regular expression to specify the structure of key IDs. */
45     public static final String KEY_ID_REGEXP = "^[A-Za-z0-9\\-_\\.]+:(\\d+.){2}\\d+$";
46
47     /** Specifies the value for names in NULL keys. */
48     public static final String NULL_KEY_NAME = "NULL";
49
50     /** Specifies the value for versions in NULL keys. */
51     public static final String NULL_KEY_VERSION = "0.0.0";
52
53     /**
54      * This enumeration is returned on key compatibility checks.
55      */
56     public enum Compatibility {
57         /** The keys have different names. */
58         DIFFERENT,
59         /**
60          * The name of the key matches but the Major version number of the keys is different (x in x.y.z do not match).
61          */
62         MAJOR,
63         /**
64          * The name of the key matches but the Minor version number of the keys is different (y in x.y.z do not match).
65          */
66         MINOR,
67         /**
68          * The name of the key matches but the Patch version number of the keys is different (z in x.y.z do not match).
69          */
70         PATCH,
71         /** The keys match completely. */
72         IDENTICAL
73     }
74
75     /**
76      * Copy constructor.
77      *
78      * @param copyConcept the concept to copy from
79      */
80     protected PfKey(final PfKey copyConcept) {
81         super(copyConcept);
82     }
83
84     @Override
85     public abstract String getId();
86
87     /**
88      * Return the result of a compatibility check of two keys.
89      *
90      * @param otherKey the key to check compatibility against
91      * @return the compatibility result of the check
92      */
93     public abstract Compatibility getCompatibility(@NonNull PfKey otherKey);
94
95     /**
96      * Check if two keys are compatible, that is the keys are IDENTICAL or have only MINOR, PATCH differences.
97      *
98      * @param otherKey the key to check compatibility against
99      * @return true, if the keys are compatible
100      */
101     public abstract boolean isCompatible(@NonNull PfKey otherKey);
102
103     /**
104      * Check if this key is a newer version than the other key.
105      *
106      * @param otherKey the key to check against
107      * @return true, if this key is newer than the other key
108      */
109     public abstract boolean isNewerThan(@NonNull PfKey otherKey);
110
111     /**
112      * Check if a key equals its null key.
113      *
114      * @return true, if the key is a null key
115      */
116     public abstract boolean isNullKey();
117
118     /**
119      * Get the major version of a key.
120      *
121      * @return the major version of a key
122      */
123     public abstract int getMajorVersion();
124
125     /**
126      * Get the minor version of a key.
127      *
128      * @return the minor version of a key
129      */
130     public abstract int getMinorVersion();
131
132     /**
133      * Get the patch version of a key.
134      *
135      * @return the patch version of a key
136      */
137     public abstract int getPatchVersion();
138 }