2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 javax.persistence.Column;
25 import lombok.EqualsAndHashCode;
27 import lombok.NonNull;
28 import lombok.ToString;
29 import org.apache.commons.lang3.ObjectUtils;
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
33 * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
35 @EqualsAndHashCode(callSuper = false)
37 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
38 private static final long serialVersionUID = -2730203215911880756L;
42 private JpaToscaConstraintOperation operation;
46 private String compareTo;
49 * Constructor to set operation.
51 * @param operation the operation to set
52 * @param compareTo the string to compare to
54 public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
55 @NonNull final String compareTo) {
56 this.operation = operation;
57 this.compareTo = compareTo;
61 * Authorative constructor.
63 * @param authorativeConcept the authorative concept to copy from
65 public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
67 * The following will call invoke fromAuthorative() which will populate the class fields.
69 super(authorativeConcept);
73 public ToscaConstraint toAuthorative() {
74 var toscaConstraint = new ToscaConstraint();
76 if (operation == null) {
82 toscaConstraint.setEqual(compareTo);
86 toscaConstraint.setGreaterThan(compareTo);
90 toscaConstraint.setGreaterOrEqual(compareTo);
94 toscaConstraint.setLessThan(compareTo);
98 toscaConstraint.setLessOrEqual(compareTo);
105 return toscaConstraint;
109 public void fromAuthorative(final ToscaConstraint toscaConstraint) {
111 if (toscaConstraint.getEqual() != null) {
112 operation = JpaToscaConstraintOperation.EQ;
113 compareTo = toscaConstraint.getEqual();
114 } else if (toscaConstraint.getGreaterThan() != null) {
115 operation = JpaToscaConstraintOperation.GT;
116 compareTo = toscaConstraint.getGreaterThan();
117 } else if (toscaConstraint.getGreaterOrEqual() != null) {
118 operation = JpaToscaConstraintOperation.GE;
119 compareTo = toscaConstraint.getGreaterOrEqual();
120 } else if (toscaConstraint.getLessThan() != null) {
121 operation = JpaToscaConstraintOperation.LT;
122 compareTo = toscaConstraint.getLessThan();
123 } else if (toscaConstraint.getLessOrEqual() != null) {
124 operation = JpaToscaConstraintOperation.LE;
125 compareTo = toscaConstraint.getLessOrEqual();
131 public int compareTo(JpaToscaConstraint otherConstraint) {
132 if (otherConstraint == null) {
135 if (this == otherConstraint) {
138 if (getClass() != otherConstraint.getClass()) {
139 return getClass().getName().compareTo(otherConstraint.getClass().getName());
142 final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
144 int result = ObjectUtils.compare(operation, other.operation);
149 return ObjectUtils.compare(compareTo, other.compareTo);