Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import jakarta.persistence.ElementCollection;
27 import jakarta.persistence.Entity;
28 import jakarta.persistence.Inheritance;
29 import jakarta.persistence.InheritanceType;
30 import jakarta.persistence.Table;
31 import java.io.Serial;
32 import java.util.ArrayList;
33 import java.util.List;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NoArgsConstructor;
37 import lombok.NonNull;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
44
45 /**
46  * Class to represent custom data type in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 @Entity
52 @Table(name = "ToscaDataType")
53 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @Data
55 @EqualsAndHashCode(callSuper = true)
56 @NoArgsConstructor
57 public class JpaToscaDataType extends JpaToscaWithToscaProperties<ToscaDataType> {
58     @Serial
59     private static final long serialVersionUID = -3922690413436539164L;
60
61     @ElementCollection
62     private List<@NotNull @Valid JpaToscaConstraint> constraints;
63
64     /**
65      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
66      *
67      * @param key the key
68      */
69     public JpaToscaDataType(@NonNull final PfConceptKey key) {
70         super(key);
71     }
72
73     /**
74      * Copy constructor.
75      *
76      * @param copyConcept the concept to copy from
77      */
78     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
79         super(copyConcept);
80         // Constraints are immutable
81         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
82     }
83
84     /**
85      * Authorative constructor.
86      *
87      * @param authorativeConcept the authorative concept to copy from
88      */
89     public JpaToscaDataType(final ToscaDataType authorativeConcept) {
90         super(authorativeConcept);
91     }
92
93     @Override
94     public ToscaDataType toAuthorative() {
95         var toscaDataType = new ToscaDataType();
96         super.setToscaEntity(toscaDataType);
97         super.toAuthorative();
98
99         toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
100
101         return toscaDataType;
102     }
103
104     @Override
105     public void fromAuthorative(final ToscaDataType toscaDataType) {
106         super.fromAuthorative(toscaDataType);
107
108         constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
109     }
110
111     @Override
112     public int compareTo(final PfConcept otherConcept) {
113         if (this == otherConcept) {
114             return 0;
115         }
116
117         int result = super.compareTo(otherConcept);
118         if (result != 0) {
119             return result;
120         }
121
122         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
123
124         return PfUtils.compareCollections(constraints, other.constraints);
125     }
126 }