Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceTimestampKey.java
index 5cb639e..c7838d8 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2021 Nordix Foundation.
+ * Copyright (C) 2021, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.base;
 
+import jakarta.persistence.Column;
+import jakarta.persistence.Embeddable;
+import jakarta.persistence.Embedded;
+import jakarta.persistence.Temporal;
+import jakarta.persistence.TemporalType;
+import java.io.Serial;
 import java.time.Instant;
-import javax.persistence.Column;
-import javax.persistence.Embeddable;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-import lombok.ToString;
 import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.utils.validation.Assertions;
 
@@ -35,36 +42,42 @@ import org.onap.policy.common.utils.validation.Assertions;
  * additional option to have timestamp as a parameter.
  *
  */
+
 @Embeddable
 @Data
-@EqualsAndHashCode
-@ToString(callSuper = true)
-public class PfReferenceTimestampKey extends PfReferenceKey {
+@EqualsAndHashCode(callSuper = false)
+public class PfReferenceTimestampKey extends PfKey {
+    @Serial
+    private static final long serialVersionUID = 1130918285832617215L;
+
     private static final String TIMESTAMP_TOKEN = "timeStamp";
-    private static final Instant DEFAULT_TIMESTAMP = Instant.EPOCH;
 
-    @Column(name = TIMESTAMP_TOKEN)
+    @Column(name = TIMESTAMP_TOKEN, precision = 3)
+    @Temporal(TemporalType.TIMESTAMP)
     @NotNull
-    private Instant timeStamp;
+    private Date timeStamp;
 
+    @Embedded
+    @Column
+    private PfReferenceKey referenceKey;
 
     /**
      * The default constructor creates a null reference timestamp key.
      */
     public PfReferenceTimestampKey() {
-        super();
-        this.timeStamp = DEFAULT_TIMESTAMP;
+        this.referenceKey = new PfReferenceKey();
+        this.timeStamp = new Date(0);
     }
 
     /**
      * The Copy Constructor creates a key by copying another key.
      *
-     * @param referenceKey
+     * @param referenceTimestampKey
      *        the reference key to copy from
      */
-    public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) {
-        super(referenceKey);
-        this.timeStamp = referenceKey.getTimeStamp();
+    public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
+        this.referenceKey = referenceTimestampKey.getReferenceKey();
+        this.timeStamp = referenceTimestampKey.getTimeStamp();
     }
 
     /**
@@ -74,8 +87,8 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the parent concept key of this reference key
      */
     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
-        super(pfConceptKey);
-        this.timeStamp = DEFAULT_TIMESTAMP;
+        this.referenceKey = new PfReferenceKey(pfConceptKey);
+        this.timeStamp = new Date(0);
     }
 
     /**
@@ -85,12 +98,12 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the parent concept key of this reference key
      * @param localName
      *        the local name of this reference key
-     * @param timeStamp
-     *        the timestamp for this reference key
+     * @param instant
+     *        the time stamp for this reference key
      */
-    public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant timeStamp) {
-        super(pfConceptKey, localName);
-        this.timeStamp = timeStamp;
+    public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
+        this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
+        this.timeStamp = Date.from(instant);
     }
 
     /**
@@ -100,13 +113,13 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the parent reference key of this reference key
      * @param localName
      *        the local name of this reference key
-     * @param timeStamp
-     *        the timestamp for this reference key
+     * @param instant
+     *        the time stamp for this reference key
      */
     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
-                                   final Instant timeStamp) {
-        super(parentReferenceKey, localName);
-        this.timeStamp = timeStamp;
+                                   final Instant instant) {
+        this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
+        this.timeStamp = Date.from(instant);
     }
 
     /**
@@ -119,13 +132,13 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the local name of the parent reference key of this reference key
      * @param localName
      *        the local name of this reference key
-     * @param timeStamp
-     *        the timestamp for this reference key
+     * @param instant
+     *        the time stamp for this reference key
      */
     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
-                                   final String localName, final Instant timeStamp) {
-        super(pfConceptKey, parentLocalName, localName);
-        this.timeStamp = timeStamp;
+                                   final String localName, final Instant instant) {
+        this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
+        this.timeStamp = Date.from(instant);
     }
 
     /**
@@ -138,13 +151,13 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the version of the parent concept key of this reference key
      * @param localName
      *        the local name of this reference key
-     * @param timeStamp
-     *        the timestamp for this reference key
+     * @param instant
+     *        the time stamp for this reference key
      */
     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
-                                   final Instant timeStamp) {
-        super(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
-        this.timeStamp = timeStamp;
+                                   final Instant instant) {
+        this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
+        this.timeStamp = Date.from(instant);
     }
 
     /**
@@ -159,13 +172,13 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      *        the parent local name of this reference key
      * @param localName
      *        the local name of this reference key
-     * @param timeStamp
-     *        the timestamp for this reference key
+     * @param instant
+     *        the instant for this reference key
      */
     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
-                                   final String parentLocalName, final String localName, final Instant timeStamp) {
-        super(parentKeyName, parentKeyVersion, parentLocalName, localName);
-        this.timeStamp = timeStamp;
+                                   final String parentLocalName, final String localName, final Instant instant) {
+        this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
+        this.timeStamp = Date.from(instant);
     }
 
 
@@ -175,8 +188,8 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      * @param id the key ID in a format that respects the KEY_ID_REGEXP
      */
     public PfReferenceTimestampKey(final String id) {
-        super(id.substring(0, id.lastIndexOf(':')));
-        this.timeStamp = Instant.ofEpochSecond(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
+        this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
+        this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
     }
 
 
@@ -187,36 +200,111 @@ public class PfReferenceTimestampKey extends PfReferenceKey {
      */
     public static PfReferenceTimestampKey getNullKey() {
         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
-            PfKey.NULL_KEY_NAME, DEFAULT_TIMESTAMP);
+            PfKey.NULL_KEY_NAME, Instant.EPOCH);
+    }
+
+    public Instant getInstant() {
+        return timeStamp.toInstant();
     }
 
+    public void setInstant(final Instant instant) {
+        setTimeStamp(Date.from(instant));
+    }
 
+    /**
+     * Get the key of this reference.
+     *
+     * @return the pfReferenceTimestamp key
+     */
     @Override
     public PfReferenceTimestampKey getKey() {
         return this;
     }
 
+    /**
+     * Get the key as a string.
+     * @return pfReferenceTimestamp key.
+     */
     @Override
     public String getId() {
-        return super.getId() + ':' + getTimeStamp().getEpochSecond();
+        return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
+    }
+
+
+    /**
+     * Check if this key is a newer version than the other key.
+     *
+     * @param otherKey the key to check against
+     * @return true, if this key is newer than the other key
+     */
+    @Override
+    public boolean isNewerThan(@NonNull PfKey otherKey) {
+        Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
+        final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
+        if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
+            return timeStamp.after(otherReferenceKey.timeStamp);
+        }
+        return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
     }
 
     @Override
     public boolean isNullKey() {
-        return super.isNullKey() && getTimeStamp().getEpochSecond() == 0;
+        return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
     }
 
     @Override
-    public boolean isNewerThan(@NonNull PfKey otherKey) {
-        Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
-        final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey;
+    public int getMajorVersion() {
+        return getReferenceKey().getMajorVersion();
+    }
 
-        if (this.equals(otherConceptKey)) {
-            return false;
+    @Override
+    public int getMinorVersion() {
+        return getReferenceKey().getMinorVersion();
+    }
+
+    @Override
+    public int getPatchVersion() {
+        return getReferenceKey().getPatchVersion();
+    }
+
+
+    @Override
+    public int compareTo(@NonNull final PfConcept otherObj) {
+        if (this == otherObj) {
+            return 0;
+        }
+        if (getClass() != otherObj.getClass()) {
+            return getClass().getName().compareTo(otherObj.getClass().getName());
         }
-        if (!getTimeStamp().equals(otherConceptKey.timeStamp)) {
-            return timeStamp.isAfter(otherConceptKey.timeStamp);
+        int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
+        if (0 == result) {
+            return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
+        }
+        return result;
+    }
+
+    @Override
+    public List<PfKey> getKeys() {
+        return Collections.singletonList(getKey());
+    }
+
+    @Override
+    public void clean() {
+        getReferenceKey().clean();
+    }
+
+    @Override
+    public Compatibility getCompatibility(@NonNull PfKey otherKey) {
+        return getReferenceKey().getCompatibility(otherKey);
+    }
+
+    @Override
+    public boolean isCompatible(@NonNull PfKey otherKey) {
+        if (!(otherKey instanceof PfReferenceTimestampKey otherReferenceKey)) {
+            return false;
         }
-        return super.isNewerThan(otherKey);
+
+        return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
+            .getParentConceptKey());
     }
 }