Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintValidValues.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import jakarta.persistence.ElementCollection;
25 import java.io.Serial;
26 import java.util.ArrayList;
27 import java.util.List;
28 import lombok.EqualsAndHashCode;
29 import lombok.Getter;
30 import lombok.NonNull;
31 import lombok.ToString;
32 import org.onap.policy.models.base.PfUtils;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
34
35 /**
36  * This class represents valid_values TOSCA constraint.
37  *
38  * @author Chenfei Gao (cgao@research.att.com)
39  */
40 @EqualsAndHashCode(callSuper = false)
41 @ToString
42 public class JpaToscaConstraintValidValues extends JpaToscaConstraint {
43     @Serial
44     private static final long serialVersionUID = -5060193250508635456L;
45
46     @ElementCollection
47     @Getter
48     private List<String> validValues;
49
50     /**
51      * Constructor to set the valid values.
52      *
53      * @param validValues the valid values that are allowed
54      */
55     public JpaToscaConstraintValidValues(@NonNull final List<String> validValues) {
56         this.validValues = validValues;
57     }
58
59     /**
60      * Authorative constructor.
61      *
62      * @param authorativeConcept the authorative concept to copy from
63      */
64     public JpaToscaConstraintValidValues(final ToscaConstraint authorativeConcept) {
65         /*
66          * The following will call invoke fromAuthorative() which will populate the class fields.
67          */
68         super(authorativeConcept);
69     }
70
71     @Override
72     public ToscaConstraint toAuthorative() {
73         var toscaConstraint = new ToscaConstraint();
74
75         toscaConstraint.setValidValues(validValues);
76
77         return toscaConstraint;
78     }
79
80     @Override
81     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
82         validValues = new ArrayList<>();
83         if (toscaConstraint.getValidValues() != null) {
84             validValues.addAll(toscaConstraint.getValidValues());
85         }
86     }
87
88     @Override
89     public int compareTo(@NonNull JpaToscaConstraint otherConstraint) {
90         if (this == otherConstraint) {
91             return 0;
92         }
93         if (getClass() != otherConstraint.getClass()) {
94             return getClass().getName().compareTo(otherConstraint.getClass().getName());
95         }
96
97         final JpaToscaConstraintValidValues other = (JpaToscaConstraintValidValues) otherConstraint;
98
99         return PfUtils.compareObjects(validValues, other.validValues);
100     }
101 }