Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogical.java
index 632f84a..5d4577d 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019, 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.tosca.simple.concepts;
 
-import javax.persistence.Column;
-
+import jakarta.persistence.Column;
+import java.io.Serial;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NonNull;
 import lombok.ToString;
-
 import org.apache.commons.lang3.ObjectUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
 
@@ -36,15 +36,14 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
 @EqualsAndHashCode(callSuper = false)
 @ToString
 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
+    @Serial
     private static final long serialVersionUID = -2730203215911880756L;
 
     @Column
-    @NonNull
     @Getter
     private JpaToscaConstraintOperation operation;
 
     @Column
-    @NonNull
     @Getter
     private String compareTo;
 
@@ -66,40 +65,29 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
      * @param authorativeConcept the authorative concept to copy from
      */
     public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
+        /*
+         * The following will call invoke fromAuthorative() which will populate the class fields.
+         */
         super(authorativeConcept);
     }
 
     @Override
     public ToscaConstraint toAuthorative() {
-        ToscaConstraint toscaConstraint = new ToscaConstraint();
+        var toscaConstraint = new ToscaConstraint();
 
         if (operation == null) {
             return null;
         }
 
         switch (operation) {
-            case EQ:
-                toscaConstraint.setEqual(compareTo);
-                break;
-
-            case GT:
-                toscaConstraint.setGreaterThan(compareTo);
-                break;
-
-            case GE:
-                toscaConstraint.setGreaterOrEqual(compareTo);
-                break;
-
-            case LT:
-                toscaConstraint.setLessThan(compareTo);
-                break;
-
-            case LE:
-                toscaConstraint.setLessOrEqual(compareTo);
-                break;
-
-            default:
-                // Can't happen
+            case EQ -> toscaConstraint.setEqual(compareTo);
+            case GT -> toscaConstraint.setGreaterThan(compareTo);
+            case GE -> toscaConstraint.setGreaterOrEqual(compareTo);
+            case LT -> toscaConstraint.setLessThan(compareTo);
+            case LE -> toscaConstraint.setLessOrEqual(compareTo);
+            default -> {
+                return null;
+            }
         }
 
         return toscaConstraint;
@@ -111,20 +99,16 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
         if (toscaConstraint.getEqual() != null) {
             operation = JpaToscaConstraintOperation.EQ;
             compareTo = toscaConstraint.getEqual();
-        }
-        else if (toscaConstraint.getGreaterThan() != null) {
+        } else  if (toscaConstraint.getGreaterThan() != null) {
             operation = JpaToscaConstraintOperation.GT;
             compareTo = toscaConstraint.getGreaterThan();
-        }
-        else if (toscaConstraint.getGreaterOrEqual() != null) {
+        } else  if (toscaConstraint.getGreaterOrEqual() != null) {
             operation = JpaToscaConstraintOperation.GE;
             compareTo = toscaConstraint.getGreaterOrEqual();
-        }
-        else if (toscaConstraint.getLessThan() != null) {
+        } else  if (toscaConstraint.getLessThan() != null) {
             operation = JpaToscaConstraintOperation.LT;
             compareTo = toscaConstraint.getLessThan();
-        }
-        else if (toscaConstraint.getLessOrEqual() != null) {
+        } else  if (toscaConstraint.getLessOrEqual() != null) {
             operation = JpaToscaConstraintOperation.LE;
             compareTo = toscaConstraint.getLessOrEqual();
         }
@@ -132,15 +116,12 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
     }
 
     @Override
-    public int compareTo(JpaToscaConstraint otherConstraint) {
-        if (otherConstraint == null) {
-            return -1;
-        }
+    public int compareTo(@NonNull JpaToscaConstraint otherConstraint) {
         if (this == otherConstraint) {
             return 0;
         }
         if (getClass() != otherConstraint.getClass()) {
-            return this.hashCode() - otherConstraint.hashCode();
+            return getClass().getName().compareTo(otherConstraint.getClass().getName());
         }
 
         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;