Merge "Restructure for authorative models"
[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  * ================================================================================
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 com.google.gson.annotations.SerializedName;
24
25 import java.util.LinkedList;
26 import java.util.List;
27 import javax.persistence.ElementCollection;
28 import javax.ws.rs.core.Response;
29
30 import lombok.Data;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33
34 import org.onap.policy.models.base.PfConcept;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfReferenceKey;
37
38 /**
39  * This class represents valid_values TOSCA constraint.
40  *
41  * @author Chenfei Gao (cgao@research.att.com)
42  */
43 @EqualsAndHashCode(callSuper = false)
44 @Data
45 public class JpaToscaConstraintValidValues extends JpaToscaConstraint {
46     private static final long serialVersionUID = 3152323457560746844L;
47
48     @SerializedName("valid_values")
49     @NonNull
50     @ElementCollection
51     private final List<String> validValues;
52
53     /**
54      * The Default Constructor creates a {@link JpaToscaConstraintValidValues} object with a null key.
55      */
56     public JpaToscaConstraintValidValues() {
57         this(new PfReferenceKey());
58     }
59
60     /**
61      * The Key Constructor creates a {@link JpaToscaConstraintValidValues} object with the given concept key.
62      *
63      * @param key the key of the constraint
64      */
65     public JpaToscaConstraintValidValues(final PfReferenceKey key) {
66         super(key);
67         validValues = new LinkedList<>();
68     }
69
70     /**
71      * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key
72      * and valid values list.
73      *
74      * @param key the key of the constraint
75      * @param validValues the valid values list of the constraint
76      *
77      */
78     public JpaToscaConstraintValidValues(final PfReferenceKey key, @NonNull final List<String> validValues) {
79         super(key);
80         this.validValues = validValues;
81     }
82
83     /**
84      * Copy constructor.
85      *
86      * @param copyConcept the concept to copy from
87      */
88     public JpaToscaConstraintValidValues(@NonNull final JpaToscaConstraintValidValues copyConcept) {
89         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
90     }
91
92     @Override
93     public int compareTo(final PfConcept otherConcept) {
94         if (otherConcept == null) {
95             return -1;
96         }
97         if (this == otherConcept) {
98             return 0;
99         }
100         if (getClass() != otherConcept.getClass()) {
101             return this.hashCode() - otherConcept.hashCode();
102         }
103
104         final JpaToscaConstraintValidValues other = (JpaToscaConstraintValidValues) otherConcept;
105
106         int result = super.compareTo(other);
107         if (result != 0) {
108             return result;
109         }
110
111         if (validValues.equals(other.validValues)) {
112             return 0;
113         }
114         return -1;
115     }
116
117     @Override
118     public PfConcept copyTo(@NonNull final PfConcept target) {
119         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint");
120     }
121 }