Refactor to authorative TOSCA serializtion
[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 AT&T Intellectual Property. All rights reserved.
4  *  Modifications Copyright (C) 2019 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 java.util.ArrayList;
25 import java.util.List;
26 import javax.persistence.ElementCollection;
27
28 import lombok.EqualsAndHashCode;
29 import lombok.Getter;
30 import lombok.NonNull;
31 import lombok.ToString;
32
33 import org.onap.policy.models.base.PfUtils;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
35
36 /**
37  * This class represents valid_values TOSCA constraint.
38  *
39  * @author Chenfei Gao (cgao@research.att.com)
40  */
41 @EqualsAndHashCode(callSuper = false)
42 @ToString
43 public class JpaToscaConstraintValidValues extends JpaToscaConstraint {
44     private static final long serialVersionUID = -5060193250508635456L;
45
46     @ElementCollection
47     @NonNull
48     @Getter
49     private List<String> validValues;
50
51     /**
52      * Constructor to set the valid values.
53      *
54      * @param validValues the valid values that are allowed
55      */
56     public JpaToscaConstraintValidValues(@NonNull final List<String> validValues) {
57         this.validValues = validValues;
58     }
59
60     /**
61      * Authorative constructor.
62      *
63      * @param authorativeConcept the authorative concept to copy from
64      */
65     public JpaToscaConstraintValidValues(final ToscaConstraint authorativeConcept) {
66         super(authorativeConcept);
67     }
68
69     @Override
70     public ToscaConstraint toAuthorative() {
71         ToscaConstraint toscaConstraint = new ToscaConstraint();
72
73         toscaConstraint.setValidValues(validValues);
74
75         return toscaConstraint;
76     }
77
78     @Override
79     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
80         if (toscaConstraint.getValidValues() != null) {
81             validValues = new ArrayList<>();
82             validValues.addAll(toscaConstraint.getValidValues());
83         }
84     }
85
86     @Override
87     public int compareTo(JpaToscaConstraint otherConstraint) {
88         if (otherConstraint == null) {
89             return -1;
90         }
91         if (this == otherConstraint) {
92             return 0;
93         }
94         if (getClass() != otherConstraint.getClass()) {
95             return this.hashCode() - otherConstraint.hashCode();
96         }
97
98         final JpaToscaConstraintValidValues other = (JpaToscaConstraintValidValues) otherConstraint;
99
100         return PfUtils.compareObjects(validValues, other.validValues);
101     }
102 }