2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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 java.time.Instant;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.Embeddable;
30 import javax.persistence.Embedded;
31 import javax.persistence.Temporal;
32 import javax.persistence.TemporalType;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.onap.policy.common.parameters.annotations.NotNull;
37 import org.onap.policy.common.utils.validation.Assertions;
40 * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
41 * additional option to have timestamp as a parameter.
47 @EqualsAndHashCode(callSuper = false)
48 public class PfReferenceTimestampKey extends PfKey {
49 private static final long serialVersionUID = 1130918285832617215L;
51 private static final String TIMESTAMP_TOKEN = "timeStamp";
53 @Column(name = TIMESTAMP_TOKEN)
54 @Temporal(TemporalType.TIMESTAMP)
56 private Date timeStamp;
60 private PfReferenceKey referenceKey;
63 * The default constructor creates a null reference timestamp key.
65 public PfReferenceTimestampKey() {
66 this.referenceKey = new PfReferenceKey();
67 this.timeStamp = new Date(0);
71 * The Copy Constructor creates a key by copying another key.
73 * @param referenceTimestampKey
74 * the reference key to copy from
76 public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
77 this.referenceKey = referenceTimestampKey.getReferenceKey();
78 this.timeStamp = referenceTimestampKey.getTimeStamp();
82 * Constructor to create a null reference key for the specified parent concept key.
85 * the parent concept key of this reference key
87 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
88 this.referenceKey = new PfReferenceKey(pfConceptKey);
89 this.timeStamp = new Date(0);
93 * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
96 * the parent concept key of this reference key
98 * the local name of this reference key
100 * the time stamp for this reference key
102 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
103 this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
104 this.timeStamp = Date.from(instant);
108 * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
110 * @param parentReferenceKey
111 * the parent reference key of this reference key
113 * the local name of this reference key
115 * the time stamp for this reference key
117 public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
118 final Instant instant) {
119 this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
120 this.timeStamp = Date.from(instant);
124 * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
125 * reference key's concept key and local name) with the given local name.
127 * @param pfConceptKey
128 * the concept key of the parent reference key of this reference key
129 * @param parentLocalName
130 * the local name of the parent reference key of this reference key
132 * the local name of this reference key
134 * the time stamp for this reference key
136 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
137 final String localName, final Instant instant) {
138 this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
139 this.timeStamp = Date.from(instant);
143 * Constructor to create a reference timestamp key for the given parent concept key (specified by the
144 * parent concept key's name and version) with the given local name.
146 * @param parentKeyName
147 * the name of the parent concept key of this reference key
148 * @param parentKeyVersion
149 * the version of the parent concept key of this reference key
151 * the local name of this reference key
153 * the time stamp for this reference key
155 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
156 final Instant instant) {
157 this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
158 this.timeStamp = Date.from(instant);
162 * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
163 * version and local name) with the given local name.
165 * @param parentKeyName
166 * the parent key name of this reference key
167 * @param parentKeyVersion
168 * the parent key version of this reference key
169 * @param parentLocalName
170 * the parent local name of this reference key
172 * the local name of this reference key
174 * the instant for this reference key
176 public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
177 final String parentLocalName, final String localName, final Instant instant) {
178 this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
179 this.timeStamp = Date.from(instant);
184 * Constructor to create a key using the key and version from the specified key ID.
186 * @param id the key ID in a format that respects the KEY_ID_REGEXP
188 public PfReferenceTimestampKey(final String id) {
189 this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
190 this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
195 * Get a null reference timestamp key.
197 * @return a null reference key
199 public static PfReferenceTimestampKey getNullKey() {
200 return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
201 PfKey.NULL_KEY_NAME, Instant.EPOCH);
204 public Instant getInstant() {
205 return timeStamp.toInstant();
208 public void setInstant(final Instant instant) {
209 setTimeStamp(Date.from(instant));
213 * Get the key of this reference.
215 * @return the pfReferenceTimestamp key
217 public PfReferenceTimestampKey getKey() {
222 * Get the key as a string.
223 * @return pfReferenceTimestamp key.
225 public String getId() {
226 return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
231 * Check if this key is a newer version than the other key.
233 * @param otherKey the key to check against
234 * @return true, if this key is newer than the other key
237 public boolean isNewerThan(@NonNull PfKey otherKey) {
238 Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
239 final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
240 if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
241 return timeStamp.after(otherReferenceKey.timeStamp);
243 return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
247 public boolean isNullKey() {
248 return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
252 public int getMajorVersion() {
253 return getReferenceKey().getMajorVersion();
257 public int getMinorVersion() {
258 return getReferenceKey().getMinorVersion();
262 public int getPatchVersion() {
263 return getReferenceKey().getPatchVersion();
268 public int compareTo(@NonNull final PfConcept otherObj) {
269 if (this == otherObj) {
272 if (getClass() != otherObj.getClass()) {
273 return getClass().getName().compareTo(otherObj.getClass().getName());
275 int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
277 return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
283 public List<PfKey> getKeys() {
284 return Collections.singletonList(getKey());
288 public void clean() {
289 getReferenceKey().clean();
293 public Compatibility getCompatibility(@NonNull PfKey otherKey) {
294 return getReferenceKey().getCompatibility(otherKey);
298 public boolean isCompatible(@NonNull PfKey otherKey) {
299 if (!(otherKey instanceof PfReferenceTimestampKey)) {
302 final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
304 return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
305 .getParentConceptKey());