2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.sql.Timestamp;
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.List;
27 import javax.persistence.Column;
28 import javax.persistence.Embeddable;
29 import javax.persistence.Embedded;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33 import org.onap.policy.common.parameters.annotations.NotNull;
34 import org.onap.policy.common.utils.validation.Assertions;
37 * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
38 * additional option to have timestamp as a parameter.
45 public class PfReferenceTimestampKey extends PfKey {
46 private static final long serialVersionUID = 1130918285832617215L;
48 private static final String TIMESTAMP_TOKEN = "timeStamp";
50 @Column(name = TIMESTAMP_TOKEN)
52 private Timestamp timeStamp;
56 private PfReferenceKey referenceKey;
59 * The default constructor creates a null reference timestamp key.
61 public PfReferenceTimestampKey() {
62 this.referenceKey = new PfReferenceKey();
63 this.timeStamp = new Timestamp(0);
67 * The Copy Constructor creates a key by copying another key.
69 * @param referenceTimestampKey
70 * the reference key to copy from
72 public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
73 this.referenceKey = referenceTimestampKey.getReferenceKey();
74 this.timeStamp = referenceTimestampKey.getTimeStamp();
78 * Constructor to create a null reference key for the specified parent concept key.
81 * the parent concept key of this reference key
83 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
84 this.referenceKey = new PfReferenceKey(pfConceptKey);
85 this.timeStamp = new Timestamp(0);
89 * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
92 * the parent concept key of this reference key
94 * the local name of this reference key
96 * the time stamp for this reference key
98 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
99 this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
100 this.timeStamp = Timestamp.from(instant);
104 * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
106 * @param parentReferenceKey
107 * the parent reference key of this reference key
109 * the local name of this reference key
111 * the time stamp for this reference key
113 public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
114 final Instant instant) {
115 this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
116 this.timeStamp = Timestamp.from(instant);
120 * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
121 * reference key's concept key and local name) with the given local name.
123 * @param pfConceptKey
124 * the concept key of the parent reference key of this reference key
125 * @param parentLocalName
126 * the local name of the parent reference key of this reference key
128 * the local name of this reference key
130 * the time stamp for this reference key
132 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
133 final String localName, final Instant instant) {
134 this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
135 this.timeStamp = Timestamp.from(instant);
139 * Constructor to create a reference timestamp key for the given parent concept key (specified by the
140 * parent concept key's name and version) with the given local name.
142 * @param parentKeyName
143 * the name of the parent concept key of this reference key
144 * @param parentKeyVersion
145 * the version of the parent concept key of this reference key
147 * the local name of this reference key
149 * the time stamp for this reference key
151 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
152 final Instant instant) {
153 this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
154 this.timeStamp = Timestamp.from(instant);
158 * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
159 * version and local name) with the given local name.
161 * @param parentKeyName
162 * the parent key name of this reference key
163 * @param parentKeyVersion
164 * the parent key version of this reference key
165 * @param parentLocalName
166 * the parent local name of this reference key
168 * the local name of this reference key
170 * the instant for this reference key
172 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
173 final String parentLocalName, final String localName, final Instant instant) {
174 this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
175 this.timeStamp = Timestamp.from(instant);
180 * Constructor to create a key using the key and version from the specified key ID.
182 * @param id the key ID in a format that respects the KEY_ID_REGEXP
184 public PfReferenceTimestampKey(final String id) {
185 this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
186 this.timeStamp = new Timestamp(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
191 * Get a null reference timestamp key.
193 * @return a null reference key
195 public static PfReferenceTimestampKey getNullKey() {
196 return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
197 PfKey.NULL_KEY_NAME, Instant.EPOCH);
200 public Instant getInstant() {
201 return timeStamp.toInstant();
204 public void setInstant(final Instant instant) {
205 setTimeStamp(Timestamp.from(instant));
209 * Get the key of this reference.
211 * @return the pfReferenceTimestamp key
213 public PfReferenceTimestampKey getKey() {
218 * Get the key as a string.
219 * @return pfReferenceTimestamp key.
221 public String getId() {
222 return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
227 * Check if this key is a newer version than the other key.
229 * @param otherKey the key to check against
230 * @return true, if this key is newer than the other key
233 public boolean isNewerThan(@NonNull PfKey otherKey) {
234 Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
235 final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
236 if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
237 return timeStamp.after(otherReferenceKey.timeStamp);
239 return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
243 public boolean isNullKey() {
244 return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
248 public int getMajorVersion() {
249 return getReferenceKey().getMajorVersion();
253 public int getMinorVersion() {
254 return getReferenceKey().getMinorVersion();
258 public int getPatchVersion() {
259 return getReferenceKey().getPatchVersion();
264 public int compareTo(@NonNull final PfConcept otherObj) {
265 if (this == otherObj) {
268 if (getClass() != otherObj.getClass()) {
269 return getClass().getName().compareTo(otherObj.getClass().getName());
271 int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
273 return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
279 public List<PfKey> getKeys() {
280 return Collections.singletonList(getKey());
284 public void clean() {
285 getReferenceKey().clean();
289 public Compatibility getCompatibility(@NonNull PfKey otherKey) {
290 return getReferenceKey().getCompatibility(otherKey);
294 public boolean isCompatible(@NonNull PfKey otherKey) {
295 if (!(otherKey instanceof PfReferenceTimestampKey)) {
298 final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
300 return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
301 .getParentConceptKey());