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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
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;
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;
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.
48 @EqualsAndHashCode(callSuper = false)
49 public class PfReferenceTimestampKey extends PfKey {
51 private static final long serialVersionUID = 1130918285832617215L;
53 private static final String TIMESTAMP_TOKEN = "timeStamp";
55 @Column(name = TIMESTAMP_TOKEN, precision = 3)
56 @Temporal(TemporalType.TIMESTAMP)
58 private Date timeStamp;
62 private PfReferenceKey referenceKey;
65 * The default constructor creates a null reference timestamp key.
67 public PfReferenceTimestampKey() {
68 this.referenceKey = new PfReferenceKey();
69 this.timeStamp = new Date(0);
73 * The Copy Constructor creates a key by copying another key.
75 * @param referenceTimestampKey
76 * the reference key to copy from
78 public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
79 this.referenceKey = referenceTimestampKey.getReferenceKey();
80 this.timeStamp = referenceTimestampKey.getTimeStamp();
84 * Constructor to create a null reference key for the specified parent concept key.
87 * the parent concept key of this reference key
89 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
90 this.referenceKey = new PfReferenceKey(pfConceptKey);
91 this.timeStamp = new Date(0);
95 * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
98 * the parent concept key of this reference key
100 * the local name of this reference key
102 * the time stamp for this reference key
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);
110 * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
112 * @param parentReferenceKey
113 * the parent reference key of this reference key
115 * the local name of this reference key
117 * the time stamp for this reference key
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);
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.
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
134 * the local name of this reference key
136 * the time stamp for this reference key
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);
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.
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
153 * the local name of this reference key
155 * the time stamp for this reference key
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);
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.
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
174 * the local name of this reference key
176 * the instant for this reference key
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);
186 * Constructor to create a key using the key and version from the specified key ID.
188 * @param id the key ID in a format that respects the KEY_ID_REGEXP
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)));
197 * Get a null reference timestamp key.
199 * @return a null reference key
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);
206 public Instant getInstant() {
207 return timeStamp.toInstant();
210 public void setInstant(final Instant instant) {
211 setTimeStamp(Date.from(instant));
215 * Get the key of this reference.
217 * @return the pfReferenceTimestamp key
220 public PfReferenceTimestampKey getKey() {
225 * Get the key as a string.
226 * @return pfReferenceTimestamp key.
229 public String getId() {
230 return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
235 * Check if this key is a newer version than the other key.
237 * @param otherKey the key to check against
238 * @return true, if this key is newer than the other key
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);
247 return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
251 public boolean isNullKey() {
252 return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
256 public int getMajorVersion() {
257 return getReferenceKey().getMajorVersion();
261 public int getMinorVersion() {
262 return getReferenceKey().getMinorVersion();
266 public int getPatchVersion() {
267 return getReferenceKey().getPatchVersion();
272 public int compareTo(@NonNull final PfConcept otherObj) {
273 if (this == otherObj) {
276 if (getClass() != otherObj.getClass()) {
277 return getClass().getName().compareTo(otherObj.getClass().getName());
279 int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
281 return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
287 public List<PfKey> getKeys() {
288 return Collections.singletonList(getKey());
292 public void clean() {
293 getReferenceKey().clean();
297 public Compatibility getCompatibility(@NonNull PfKey otherKey) {
298 return getReferenceKey().getCompatibility(otherKey);
302 public boolean isCompatible(@NonNull PfKey otherKey) {
303 if (!(otherKey instanceof PfReferenceTimestampKey otherReferenceKey)) {
307 return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
308 .getParentConceptKey());