Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceTimestampKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021, 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 jakarta.persistence.Column;
25 import jakarta.persistence.Embeddable;
26 import jakarta.persistence.Embedded;
27 import jakarta.persistence.Temporal;
28 import jakarta.persistence.TemporalType;
29 import java.io.Serial;
30 import java.time.Instant;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.List;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.utils.validation.Assertions;
39
40 /**
41  * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
42  * additional option to have timestamp as a parameter.
43  *
44  */
45
46 @Embeddable
47 @Data
48 @EqualsAndHashCode(callSuper = false)
49 public class PfReferenceTimestampKey extends PfKey {
50     @Serial
51     private static final long serialVersionUID = 1130918285832617215L;
52
53     private static final String TIMESTAMP_TOKEN = "timeStamp";
54
55     @Column(name = TIMESTAMP_TOKEN, precision = 3)
56     @Temporal(TemporalType.TIMESTAMP)
57     @NotNull
58     private Date timeStamp;
59
60     @Embedded
61     @Column
62     private PfReferenceKey referenceKey;
63
64     /**
65      * The default constructor creates a null reference timestamp key.
66      */
67     public PfReferenceTimestampKey() {
68         this.referenceKey = new PfReferenceKey();
69         this.timeStamp = new Date(0);
70     }
71
72     /**
73      * The Copy Constructor creates a key by copying another key.
74      *
75      * @param referenceTimestampKey
76      *        the reference key to copy from
77      */
78     public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
79         this.referenceKey = referenceTimestampKey.getReferenceKey();
80         this.timeStamp = referenceTimestampKey.getTimeStamp();
81     }
82
83     /**
84      * Constructor to create a null reference key for the specified parent concept key.
85      *
86      * @param pfConceptKey
87      *        the parent concept key of this reference key
88      */
89     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
90         this.referenceKey = new PfReferenceKey(pfConceptKey);
91         this.timeStamp = new Date(0);
92     }
93
94     /**
95      * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
96      *
97      * @param pfConceptKey
98      *        the parent concept key of this reference key
99      * @param localName
100      *        the local name of this reference key
101      * @param instant
102      *        the time stamp for this reference key
103      */
104     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
105         this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
106         this.timeStamp = Date.from(instant);
107     }
108
109     /**
110      * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
111      *
112      * @param parentReferenceKey
113      *        the parent reference key of this reference key
114      * @param localName
115      *        the local name of this reference key
116      * @param instant
117      *        the time stamp for this reference key
118      */
119     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
120                                    final Instant instant) {
121         this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
122         this.timeStamp = Date.from(instant);
123     }
124
125     /**
126      * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
127      * reference key's concept key and local name) with the given local name.
128      *
129      * @param pfConceptKey
130      *        the concept key of the parent reference key of this reference key
131      * @param parentLocalName
132      *        the local name of the parent reference key of this reference key
133      * @param localName
134      *        the local name of this reference key
135      * @param instant
136      *        the time stamp for this reference key
137      */
138     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
139                                    final String localName, final Instant instant) {
140         this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
141         this.timeStamp = Date.from(instant);
142     }
143
144     /**
145      * Constructor to create a reference timestamp key for the given parent concept key (specified by the
146      * parent concept key's name and version) with the given local name.
147      *
148      * @param parentKeyName
149      *        the name of the parent concept key of this reference key
150      * @param parentKeyVersion
151      *        the version of the parent concept key of this reference key
152      * @param localName
153      *        the local name of this reference key
154      * @param instant
155      *        the time stamp for this reference key
156      */
157     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
158                                    final Instant instant) {
159         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
160         this.timeStamp = Date.from(instant);
161     }
162
163     /**
164      * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
165      * version and local name) with the given local name.
166      *
167      * @param parentKeyName
168      *        the parent key name of this reference key
169      * @param parentKeyVersion
170      *        the parent key version of this reference key
171      * @param parentLocalName
172      *        the parent local name of this reference key
173      * @param localName
174      *        the local name of this reference key
175      * @param instant
176      *        the instant for this reference key
177      */
178     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
179                                    final String parentLocalName, final String localName, final Instant instant) {
180         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
181         this.timeStamp = Date.from(instant);
182     }
183
184
185     /**
186      * Constructor to create a key using the key and version from the specified key ID.
187      *
188      * @param id the key ID in a format that respects the KEY_ID_REGEXP
189      */
190     public PfReferenceTimestampKey(final String id) {
191         this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
192         this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
193     }
194
195
196     /**
197      * Get a null reference timestamp key.
198      *
199      * @return a null reference key
200      */
201     public static PfReferenceTimestampKey getNullKey() {
202         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
203             PfKey.NULL_KEY_NAME, Instant.EPOCH);
204     }
205
206     public Instant getInstant() {
207         return timeStamp.toInstant();
208     }
209
210     public void setInstant(final Instant instant) {
211         setTimeStamp(Date.from(instant));
212     }
213
214     /**
215      * Get the key of this reference.
216      *
217      * @return the pfReferenceTimestamp key
218      */
219     @Override
220     public PfReferenceTimestampKey getKey() {
221         return this;
222     }
223
224     /**
225      * Get the key as a string.
226      * @return pfReferenceTimestamp key.
227      */
228     @Override
229     public String getId() {
230         return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
231     }
232
233
234     /**
235      * Check if this key is a newer version than the other key.
236      *
237      * @param otherKey the key to check against
238      * @return true, if this key is newer than the other key
239      */
240     @Override
241     public boolean isNewerThan(@NonNull PfKey otherKey) {
242         Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
243         final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
244         if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
245             return timeStamp.after(otherReferenceKey.timeStamp);
246         }
247         return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
248     }
249
250     @Override
251     public boolean isNullKey() {
252         return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
253     }
254
255     @Override
256     public int getMajorVersion() {
257         return getReferenceKey().getMajorVersion();
258     }
259
260     @Override
261     public int getMinorVersion() {
262         return getReferenceKey().getMinorVersion();
263     }
264
265     @Override
266     public int getPatchVersion() {
267         return getReferenceKey().getPatchVersion();
268     }
269
270
271     @Override
272     public int compareTo(@NonNull final PfConcept otherObj) {
273         if (this == otherObj) {
274             return 0;
275         }
276         if (getClass() != otherObj.getClass()) {
277             return getClass().getName().compareTo(otherObj.getClass().getName());
278         }
279         int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
280         if (0 == result) {
281             return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
282         }
283         return result;
284     }
285
286     @Override
287     public List<PfKey> getKeys() {
288         return Collections.singletonList(getKey());
289     }
290
291     @Override
292     public void clean() {
293         getReferenceKey().clean();
294     }
295
296     @Override
297     public Compatibility getCompatibility(@NonNull PfKey otherKey) {
298         return getReferenceKey().getCompatibility(otherKey);
299     }
300
301     @Override
302     public boolean isCompatible(@NonNull PfKey otherKey) {
303         if (!(otherKey instanceof PfReferenceTimestampKey otherReferenceKey)) {
304             return false;
305         }
306
307         return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
308             .getParentConceptKey());
309     }
310 }