14e9db522939415c08afb603589a594c789e375c
[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 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 java.util.ArrayList;
24 import java.util.List;
25 import javax.persistence.ElementCollection;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.models.base.PfUtils;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
32
33 /**
34  * This class represents in_range TOSCA constraint.
35  */
36 @EqualsAndHashCode(callSuper = false)
37 @ToString
38 public class JpaToscaConstraintInRange extends JpaToscaConstraint {
39     private static final long serialVersionUID = -5060193250508635456L;
40
41     @ElementCollection
42     @Getter
43     private List<String> rangeValues;
44
45     /**
46      * Constructor to set the range values.
47      *
48      * @param rangeValues the range values that are allowed
49      */
50     public JpaToscaConstraintInRange(@NonNull final List<String> rangeValues) {
51         this.rangeValues = rangeValues;
52     }
53
54     /**
55      * Authorative constructor.
56      *
57      * @param authorativeConcept the authorative concept to copy from
58      */
59     public JpaToscaConstraintInRange(final ToscaConstraint authorativeConcept) {
60         /*
61          * The following will call invoke fromAuthorative() which will populate the class fields.
62          */
63         super(authorativeConcept);
64     }
65
66     @Override
67     public ToscaConstraint toAuthorative() {
68         var toscaConstraint = new ToscaConstraint();
69
70         toscaConstraint.setRangeValues(rangeValues);
71
72         return toscaConstraint;
73     }
74
75     @Override
76     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
77         rangeValues = new ArrayList<>();
78         if (toscaConstraint.getRangeValues() != null) {
79             rangeValues.addAll(toscaConstraint.getRangeValues());
80         }
81     }
82
83     @Override
84     public int compareTo(JpaToscaConstraint otherConstraint) {
85         if (otherConstraint == null) {
86             return -1;
87         }
88         if (this == otherConstraint) {
89             return 0;
90         }
91         if (getClass() != otherConstraint.getClass()) {
92             return getClass().getName().compareTo(otherConstraint.getClass().getName());
93         }
94
95         final JpaToscaConstraintInRange other = (JpaToscaConstraintInRange) otherConstraint;
96
97         return PfUtils.compareObjects(rangeValues, other.rangeValues);
98     }
99 }