Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogical.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.Column;
25 import java.io.Serial;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.apache.commons.lang3.ObjectUtils;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
32
33 /**
34  * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
35  */
36 @EqualsAndHashCode(callSuper = false)
37 @ToString
38 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
39     @Serial
40     private static final long serialVersionUID = -2730203215911880756L;
41
42     @Column
43     @Getter
44     private JpaToscaConstraintOperation operation;
45
46     @Column
47     @Getter
48     private String compareTo;
49
50     /**
51      * Constructor to set operation.
52      *
53      * @param operation the operation to set
54      * @param compareTo the string to compare to
55      */
56     public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
57             @NonNull final String compareTo) {
58         this.operation = operation;
59         this.compareTo = compareTo;
60     }
61
62     /**
63      * Authorative constructor.
64      *
65      * @param authorativeConcept the authorative concept to copy from
66      */
67     public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
68         /*
69          * The following will call invoke fromAuthorative() which will populate the class fields.
70          */
71         super(authorativeConcept);
72     }
73
74     @Override
75     public ToscaConstraint toAuthorative() {
76         var toscaConstraint = new ToscaConstraint();
77
78         if (operation == null) {
79             return null;
80         }
81
82         switch (operation) {
83             case EQ -> toscaConstraint.setEqual(compareTo);
84             case GT -> toscaConstraint.setGreaterThan(compareTo);
85             case GE -> toscaConstraint.setGreaterOrEqual(compareTo);
86             case LT -> toscaConstraint.setLessThan(compareTo);
87             case LE -> toscaConstraint.setLessOrEqual(compareTo);
88             default -> {
89                 return null;
90             }
91         }
92
93         return toscaConstraint;
94     }
95
96     @Override
97     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
98         // @formatter:off
99         if (toscaConstraint.getEqual() != null) {
100             operation = JpaToscaConstraintOperation.EQ;
101             compareTo = toscaConstraint.getEqual();
102         } else  if (toscaConstraint.getGreaterThan() != null) {
103             operation = JpaToscaConstraintOperation.GT;
104             compareTo = toscaConstraint.getGreaterThan();
105         } else  if (toscaConstraint.getGreaterOrEqual() != null) {
106             operation = JpaToscaConstraintOperation.GE;
107             compareTo = toscaConstraint.getGreaterOrEqual();
108         } else  if (toscaConstraint.getLessThan() != null) {
109             operation = JpaToscaConstraintOperation.LT;
110             compareTo = toscaConstraint.getLessThan();
111         } else  if (toscaConstraint.getLessOrEqual() != null) {
112             operation = JpaToscaConstraintOperation.LE;
113             compareTo = toscaConstraint.getLessOrEqual();
114         }
115         // @formatter:on
116     }
117
118     @Override
119     public int compareTo(@NonNull JpaToscaConstraint otherConstraint) {
120         if (this == otherConstraint) {
121             return 0;
122         }
123         if (getClass() != otherConstraint.getClass()) {
124             return getClass().getName().compareTo(otherConstraint.getClass().getName());
125         }
126
127         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
128
129         int result = ObjectUtils.compare(operation, other.operation);
130         if (result != 0) {
131             return result;
132         }
133
134         return ObjectUtils.compare(compareTo, other.compareTo);
135     }
136 }