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 javax.persistence.Column;
26 import javax.persistence.Embeddable;
28 import lombok.EqualsAndHashCode;
29 import lombok.NonNull;
30 import lombok.ToString;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.common.utils.validation.Assertions;
35 * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
36 * additional option to have timestamp as a parameter.
41 @EqualsAndHashCode(callSuper = true)
42 @ToString(callSuper = true)
43 public class PfReferenceTimestampKey extends PfReferenceKey {
44 private static final long serialVersionUID = 1130918285832617215L;
46 private static final String TIMESTAMP_TOKEN = "timeStamp";
48 @Column(name = TIMESTAMP_TOKEN)
50 private Timestamp timeStamp;
54 * The default constructor creates a null reference timestamp key.
56 public PfReferenceTimestampKey() {
58 this.timeStamp = new Timestamp(0);
62 * The Copy Constructor creates a key by copying another key.
65 * the reference key to copy from
67 public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) {
69 this.timeStamp = referenceKey.getTimeStamp();
73 * Constructor to create a null reference key for the specified parent concept key.
76 * the parent concept key of this reference key
78 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
80 this.timeStamp = new Timestamp(0);
84 * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
87 * the parent concept key of this reference key
89 * the local name of this reference key
91 * the time stamp for this reference key
93 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
94 super(pfConceptKey, localName);
95 this.timeStamp = Timestamp.from(instant);
99 * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
101 * @param parentReferenceKey
102 * the parent reference key of this reference key
104 * the local name of this reference key
106 * the time stamp for this reference key
108 public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
109 final Instant instant) {
110 super(parentReferenceKey, localName);
111 this.timeStamp = Timestamp.from(instant);
115 * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
116 * reference key's concept key and local name) with the given local name.
118 * @param pfConceptKey
119 * the concept key of the parent reference key of this reference key
120 * @param parentLocalName
121 * the local name of the parent reference key of this reference key
123 * the local name of this reference key
125 * the time stamp for this reference key
127 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
128 final String localName, final Instant instant) {
129 super(pfConceptKey, parentLocalName, localName);
130 this.timeStamp = Timestamp.from(instant);
134 * Constructor to create a reference timestamp key for the given parent concept key (specified by the
135 * parent concept key's name and version) with the given local name.
137 * @param parentKeyName
138 * the name of the parent concept key of this reference key
139 * @param parentKeyVersion
140 * the version of the parent concept key of this reference key
142 * the local name of this reference key
144 * the time stamp for this reference key
146 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
147 final Instant instant) {
148 super(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
149 this.timeStamp = Timestamp.from(instant);
153 * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
154 * version and local name) with the given local name.
156 * @param parentKeyName
157 * the parent key name of this reference key
158 * @param parentKeyVersion
159 * the parent key version of this reference key
160 * @param parentLocalName
161 * the parent local name of this reference key
163 * the local name of this reference key
165 * the instant for this reference key
167 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
168 final String parentLocalName, final String localName, final Instant instant) {
169 super(parentKeyName, parentKeyVersion, parentLocalName, localName);
170 this.timeStamp = Timestamp.from(instant);
175 * Constructor to create a key using the key and version from the specified key ID.
177 * @param id the key ID in a format that respects the KEY_ID_REGEXP
179 public PfReferenceTimestampKey(final String id) {
180 super(id.substring(0, id.lastIndexOf(':')));
181 this.timeStamp = new Timestamp(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
186 * Get a null reference timestamp key.
188 * @return a null reference key
190 public static PfReferenceTimestampKey getNullKey() {
191 return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
192 PfKey.NULL_KEY_NAME, Instant.EPOCH);
195 public Instant getInstant() {
196 return timeStamp.toInstant();
199 public void setInstant(final Instant instant) {
200 setTimeStamp(Timestamp.from(instant));
204 public PfReferenceTimestampKey getKey() {
209 public String getId() {
210 return super.getId() + ':' + getTimeStamp().getTime();
214 public boolean isNullKey() {
215 return super.isNullKey() && getTimeStamp().getTime() == 0;
219 public boolean isNewerThan(@NonNull PfKey otherKey) {
220 Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
221 final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey;
223 if (this.equals(otherConceptKey)) {
226 if (!getTimeStamp().equals(otherConceptKey.timeStamp)) {
227 return timeStamp.after(otherConceptKey.timeStamp);
229 return super.isNewerThan(otherKey);