Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTimeInterval.java
index 5e5d014..e54f119 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
  * ================================================================================
  * 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.tosca.simple.concepts;
 
 import com.google.gson.annotations.SerializedName;
-import java.util.Date;
+import jakarta.persistence.EmbeddedId;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.Table;
+import java.io.Serial;
+import java.sql.Timestamp;
+import java.time.Instant;
 import java.util.List;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.Table;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.PfValidationMessage;
-import org.onap.policy.models.base.PfValidationResult;
-import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * Class to represent the TimeInterval in TOSCA definition.
@@ -55,16 +57,19 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 @Data
 @EqualsAndHashCode(callSuper = false)
 public class JpaToscaTimeInterval extends PfConcept {
+    @Serial
     private static final long serialVersionUID = 9151467029611969980L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @SerializedName("start_time")
-    private Date startTime;
+    private Timestamp startTime;
 
     @SerializedName("end_time")
-    private Date endTime;
+    private Timestamp endTime;
 
     /**
      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
@@ -79,7 +84,7 @@ public class JpaToscaTimeInterval extends PfConcept {
      * @param key the key
      */
     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
-        this(key, new Date(0), new Date(0));
+        this(key, Instant.EPOCH, Instant.EPOCH);
     }
 
     /**
@@ -87,11 +92,11 @@ public class JpaToscaTimeInterval extends PfConcept {
      *
      * @param key the key
      */
-    public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime,
-            @NonNull final Date endTime) {
+    public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Instant startTime,
+            @NonNull final Instant endTime) {
         this.key = key;
-        this.startTime = startTime;
-        this.endTime = endTime;
+        this.startTime = Timestamp.from(startTime);
+        this.endTime = Timestamp.from(endTime);
     }
 
     /**
@@ -117,30 +122,19 @@ public class JpaToscaTimeInterval extends PfConcept {
     }
 
     @Override
-    public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        if (key.isNullKey()) {
-            result.addValidationMessage(
-                    new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
-        }
-
-        result = key.validate(result);
+    public BeanValidationResult validate(@NonNull String fieldName) {
+        BeanValidationResult result = super.validate(fieldName);
 
         if (startTime == null || startTime.getTime() == 0) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "start time on time interval may not be null or zero"));
+            addResult(result, "startTime", startTime, "is null or zero");
         }
 
         if (endTime == null || endTime.getTime() == 0) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "end time on time interval may not be null or zero"));
+            addResult(result, "endTime", endTime, "is null or zero");
         }
 
         if (startTime != null && endTime != null && endTime.before(startTime)) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "end time \"" + endTime.toString() + "\" on time interval may not be before start time \""
-                            + startTime.toString() + "\""));
+            addResult(result, "endTime", endTime, "is before startTime");
         }
 
         return result;
@@ -159,13 +153,14 @@ public class JpaToscaTimeInterval extends PfConcept {
         }
 
         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
-        if (!key.equals(other.key)) {
-            return key.compareTo(other.key);
+        int result = key.compareTo(other.key);
+        if (result != 0) {
+            return result;
         }
 
-        int returnVal = PfUtils.compareObjects(startTime, other.startTime);
-        if (returnVal != 0) {
-            return returnVal;
+        result = PfUtils.compareObjects(startTime, other.startTime);
+        if (result != 0) {
+            return result;
         }
 
         return PfUtils.compareObjects(endTime, other.endTime);