2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import javax.persistence.Column;
25 import lombok.EqualsAndHashCode;
27 import lombok.NonNull;
28 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 {
39 private static final long serialVersionUID = -2730203215911880756L;
44 private JpaToscaConstraintOperation operation;
49 private String compareTo;
52 * Constructor to set operation.
54 * @param operation the operation to set
55 * @param compareTo the string to compare to
57 public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
58 @NonNull final String compareTo) {
59 this.operation = operation;
60 this.compareTo = compareTo;
64 * Authorative constructor.
66 * @param authorativeConcept the authorative concept to copy from
68 public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
69 super(authorativeConcept);
73 public ToscaConstraint toAuthorative() {
74 ToscaConstraint toscaConstraint = new ToscaConstraint();
78 toscaConstraint.setEqual(compareTo);
82 toscaConstraint.setGreaterThan(compareTo);
86 toscaConstraint.setGreaterOrEqual(compareTo);
90 toscaConstraint.setLessThan(compareTo);
94 toscaConstraint.setLessOrEqual(compareTo);
102 return toscaConstraint;
106 public void fromAuthorative(final ToscaConstraint toscaConstraint) {
108 if (toscaConstraint.getEqual() != null) {
109 operation = JpaToscaConstraintOperation.EQ;
110 compareTo = toscaConstraint.getEqual();
112 else if (toscaConstraint.getGreaterThan() != null) {
113 operation = JpaToscaConstraintOperation.GT;
114 compareTo = toscaConstraint.getGreaterThan();
116 else if (toscaConstraint.getGreaterOrEqual() != null) {
117 operation = JpaToscaConstraintOperation.GE;
118 compareTo = toscaConstraint.getGreaterOrEqual();
120 else if (toscaConstraint.getLessThan() != null) {
121 operation = JpaToscaConstraintOperation.LT;
122 compareTo = toscaConstraint.getLessThan();
124 else if (toscaConstraint.getLessOrEqual() != null) {
125 operation = JpaToscaConstraintOperation.LE;
126 compareTo = toscaConstraint.getLessOrEqual();
132 public int compareTo(JpaToscaConstraint otherConstraint) {
133 if (otherConstraint == null) {
136 if (this == otherConstraint) {
139 if (getClass() != otherConstraint.getClass()) {
140 return this.hashCode() - otherConstraint.hashCode();
143 final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
145 int result = ObjectUtils.compare(operation, other.operation);
150 return ObjectUtils.compare(compareTo, other.compareTo);