Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.java
index 0035eb0..107fb3a 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-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020, 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 jakarta.persistence.ElementCollection;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.Table;
+import java.io.Serial;
 import java.util.ArrayList;
 import java.util.List;
-
-import javax.persistence.ElementCollection;
-import javax.persistence.Entity;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.Table;
-
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
 import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Valid;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfKey;
 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.tosca.authorative.concepts.ToscaDataType;
 
 /**
  * Class to represent custom data type in TOSCA definition.
@@ -56,21 +53,13 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = true)
-public class JpaToscaDataType extends JpaToscaEntityType {
+@NoArgsConstructor
+public class JpaToscaDataType extends JpaToscaWithToscaProperties<ToscaDataType> {
+    @Serial
     private static final long serialVersionUID = -3922690413436539164L;
 
     @ElementCollection
-    private List<JpaToscaConstraint> constraints;
-
-    @ElementCollection
-    private List<JpaToscaProperty> properties;
-
-    /**
-     * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
-     */
-    public JpaToscaDataType() {
-        this(new PfConceptKey());
-    }
+    private List<@NotNull @Valid JpaToscaConstraint> constraints;
 
     /**
      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
@@ -88,159 +77,50 @@ public class JpaToscaDataType extends JpaToscaEntityType {
      */
     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
         super(copyConcept);
+        // Constraints are immutable
+        this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
     }
 
-    @Override
-    public List<PfKey> getKeys() {
-        final List<PfKey> keyList = super.getKeys();
-
-        if (constraints != null) {
-            for (JpaToscaConstraint constraint : constraints) {
-                keyList.addAll(constraint.getKeys());
-            }
-        }
-
-        if (properties != null) {
-            for (JpaToscaProperty property : properties) {
-                keyList.addAll(property.getKeys());
-            }
-        }
-
-        return keyList;
+    /**
+     * Authorative constructor.
+     *
+     * @param authorativeConcept the authorative concept to copy from
+     */
+    public JpaToscaDataType(final ToscaDataType authorativeConcept) {
+        super(authorativeConcept);
     }
 
     @Override
-    public void clean() {
-        super.clean();
+    public ToscaDataType toAuthorative() {
+        var toscaDataType = new ToscaDataType();
+        super.setToscaEntity(toscaDataType);
+        super.toAuthorative();
 
-        if (constraints != null) {
-            for (JpaToscaConstraint constraint : constraints) {
-                constraint.clean();
-            }
-        }
+        toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
 
-        if (properties != null) {
-            for (JpaToscaProperty property : properties) {
-                property.clean();
-            }
-        }
+        return toscaDataType;
     }
 
     @Override
-    public PfValidationResult validate(final PfValidationResult resultIn) {
-        PfValidationResult result = super.validate(resultIn);
-
-        if (constraints != null) {
-            result = validateConstraints(result);
-        }
-
-        if (properties != null) {
-            result = validateProperties(result);
-        }
-
-        return result;
-    }
-
-    /**
-     * Validate the constraints.
-     *
-     * @param result The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (JpaToscaConstraint constraint : constraints) {
-            if (constraint == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "data type constraint may not be null "));
-            } else {
-                result = constraint.validate(result);
-            }
-        }
-        return result;
-    }
+    public void fromAuthorative(final ToscaDataType toscaDataType) {
+        super.fromAuthorative(toscaDataType);
 
-    /**
-     * Validate the properties.
-     *
-     * @param result The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateProperties(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (JpaToscaProperty property : properties) {
-            if (property == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "data type property may not be null "));
-            } else {
-                result = property.validate(result);
-            }
-        }
-        return result;
+        constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
     }
 
     @Override
     public int compareTo(final PfConcept otherConcept) {
-        if (otherConcept == null) {
-            return -1;
-        }
         if (this == otherConcept) {
             return 0;
         }
-        if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
-        }
-
-        final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
-        if (!super.equals(other)) {
-            return super.compareTo(other);
-        }
-
-        int result = PfUtils.compareObjects(constraints, other.constraints);
-        if (result != 0) {
-            return result;
-        }
 
-        result = PfUtils.compareObjects(properties, other.properties);
+        int result = super.compareTo(otherConcept);
         if (result != 0) {
             return result;
         }
 
-        return 0;
-    }
-
-    @Override
-    public PfConcept copyTo(@NonNull PfConcept target) {
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfConcept.class);
-
-        final JpaToscaDataType copy = ((JpaToscaDataType) copyObject);
-        super.copyTo(target);
-
-        if (constraints == null) {
-            copy.setConstraints(null);
-        }
-        else {
-            final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
-            for (final JpaToscaConstraint constraint : constraints) {
-                newConstraints.add(constraint); // Constraints are immutable
-            }
-            copy.setConstraints(newConstraints);
-        }
-
-        if (properties == null) {
-            copy.setProperties(null);
-        }
-        else {
-            final List<JpaToscaProperty> newProperties = new ArrayList<>();
-            for (final JpaToscaProperty property : properties) {
-                newProperties.add(new JpaToscaProperty(property));
-            }
-            copy.setProperties(newProperties);
-        }
+        final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
 
-        return copy;
+        return PfUtils.compareCollections(constraints, other.constraints);
     }
-}
\ No newline at end of file
+}