Address more sonar issues in policy-models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogical.java
index 9841cbe..73546f6 100644 (file)
 package org.onap.policy.models.tosca.simple.concepts;
 
 import javax.persistence.Column;
-
 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;
 
@@ -39,12 +37,10 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
     private static final long serialVersionUID = -2730203215911880756L;
 
     @Column
-    @NonNull
     @Getter
     private JpaToscaConstraintOperation operation;
 
     @Column
-    @NonNull
     @Getter
     private String compareTo;
 
@@ -66,6 +62,9 @@ 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);
     }
 
@@ -73,30 +72,33 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
     public ToscaConstraint toAuthorative() {
         ToscaConstraint toscaConstraint = new ToscaConstraint();
 
+        if (operation == null) {
+            return null;
+        }
+
         switch (operation) {
-            case EQ: {
+            case EQ:
                 toscaConstraint.setEqual(compareTo);
                 break;
-            }
-            case GT: {
+
+            case GT:
                 toscaConstraint.setGreaterThan(compareTo);
                 break;
-            }
-            case GE: {
+
+            case GE:
                 toscaConstraint.setGreaterOrEqual(compareTo);
                 break;
-            }
-            case LT: {
+
+            case LT:
                 toscaConstraint.setLessThan(compareTo);
                 break;
-            }
-            case LE: {
+
+            case LE:
                 toscaConstraint.setLessOrEqual(compareTo);
                 break;
-            }
-            default: {
+
+            default:
                 // Can't happen
-            }
         }
 
         return toscaConstraint;
@@ -108,20 +110,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();
         }
@@ -137,7 +135,7 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint {
             return 0;
         }
         if (getClass() != otherConstraint.getClass()) {
-            return this.hashCode() - otherConstraint.hashCode();
+            return getClass().getName().compareTo(otherConstraint.getClass().getName());
         }
 
         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;