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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import jakarta.persistence.Column;
25 import java.io.Serial;
26 import lombok.EqualsAndHashCode;
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;
34 * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
36 @EqualsAndHashCode(callSuper = false)
38 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
40 private static final long serialVersionUID = -2730203215911880756L;
44 private JpaToscaConstraintOperation operation;
48 private String compareTo;
51 * Constructor to set operation.
53 * @param operation the operation to set
54 * @param compareTo the string to compare to
56 public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
57 @NonNull final String compareTo) {
58 this.operation = operation;
59 this.compareTo = compareTo;
63 * Authorative constructor.
65 * @param authorativeConcept the authorative concept to copy from
67 public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
69 * The following will call invoke fromAuthorative() which will populate the class fields.
71 super(authorativeConcept);
75 public ToscaConstraint toAuthorative() {
76 var toscaConstraint = new ToscaConstraint();
78 if (operation == null) {
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);
93 return toscaConstraint;
97 public void fromAuthorative(final ToscaConstraint toscaConstraint) {
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();
119 public int compareTo(@NonNull JpaToscaConstraint otherConstraint) {
120 if (this == otherConstraint) {
123 if (getClass() != otherConstraint.getClass()) {
124 return getClass().getName().compareTo(otherConstraint.getClass().getName());
127 final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
129 int result = ObjectUtils.compare(operation, other.operation);
134 return ObjectUtils.compare(compareTo, other.compareTo);