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