Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyUse.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 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.List;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.common.parameters.annotations.NotNull;
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.validation.annotations.VerifyKey;
33
34 /**
35  * This class records a usage of a key in the system. When the list of keys being used by a concept
36  * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
37  * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
38  * {@link PfKeyUse} objects.
39  *
40  * <p>Validation checks that each key is valid.
41  */
42 @EqualsAndHashCode(callSuper = false)
43 @ToString
44 public class PfKeyUse extends PfKey {
45     @Serial
46     private static final long serialVersionUID = 2007147220109881705L;
47
48     @VerifyKey
49     @NotNull
50     @Getter
51     private PfKey usedKey;
52
53     /**
54      * The Default Constructor creates this concept with a null key.
55      */
56     public PfKeyUse() {
57         this(new PfConceptKey());
58     }
59
60     /**
61      * This constructor creates an instance of this class, and holds a reference to a used key.
62      *
63      * @param usedKey a used key
64      */
65     public PfKeyUse(@NonNull final PfKey usedKey) {
66         this.usedKey = usedKey;
67     }
68
69     /**
70      * Copy constructor.
71      *
72      * @param copyConcept the concept to copy from
73      */
74     public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
75         super(copyConcept);
76         this.usedKey = PfUtils.makeCopy(copyConcept.usedKey);
77     }
78
79     @Override
80     public PfKey getKey() {
81         return usedKey;
82     }
83
84     @Override
85     public List<PfKey> getKeys() {
86         return usedKey.getKeys();
87     }
88
89     @Override
90     public String getId() {
91         return usedKey.getId();
92     }
93
94     @Override
95     public boolean isNullKey() {
96         return usedKey.isNullKey();
97     }
98
99     /**
100      * Sets the key.
101      *
102      * @param key the key
103      */
104     public void setKey(@NonNull final PfKey key) {
105         this.usedKey = key;
106     }
107
108     @Override
109     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
110         return usedKey.getCompatibility(otherKey);
111     }
112
113     @Override
114     public boolean isCompatible(@NonNull final PfKey otherKey) {
115         return usedKey.isCompatible(otherKey);
116     }
117
118     @Override
119     public boolean isNewerThan(@NonNull final PfKey otherKey) {
120         return usedKey.isCompatible(otherKey);
121     }
122
123     @Override
124     public int getMajorVersion() {
125         return usedKey.getMajorVersion();
126     }
127
128     @Override
129     public int getMinorVersion() {
130         return usedKey.getMinorVersion();
131     }
132
133     @Override
134     public int getPatchVersion() {
135         return usedKey.getPatchVersion();
136     }
137
138     @Override
139     public void clean() {
140         usedKey.clean();
141     }
142
143     @Override
144     public int compareTo(@NonNull final PfConcept otherObj) {
145         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
146
147         if (this == otherObj) {
148             return 0;
149         }
150         if (getClass() != otherObj.getClass()) {
151             return getClass().getName().compareTo(otherObj.getClass().getName());
152         }
153
154         final PfKeyUse other = (PfKeyUse) otherObj;
155
156         return usedKey.compareTo(other.usedKey);
157     }
158 }