cd6216473d9a8e5547d09b2ff98624aa1796d001
[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 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
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 javax.persistence.Column;
24 import lombok.EqualsAndHashCode;
25 import lombok.Getter;
26 import lombok.NonNull;
27 import lombok.ToString;
28 import org.apache.commons.lang3.ObjectUtils;
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
30
31 /**
32  * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
33  */
34 @EqualsAndHashCode(callSuper = false)
35 @ToString
36 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
37     private static final long serialVersionUID = -2730203215911880756L;
38
39     @Column
40     @NonNull
41     @Getter
42     private JpaToscaConstraintOperation operation;
43
44     @Column
45     @NonNull
46     @Getter
47     private String compareTo;
48
49     /**
50      * Constructor to set operation.
51      *
52      * @param operation the operation to set
53      * @param compareTo the string to compare to
54      */
55     public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
56             @NonNull final String compareTo) {
57         this.operation = operation;
58         this.compareTo = compareTo;
59     }
60
61     /**
62      * Authorative constructor.
63      *
64      * @param authorativeConcept the authorative concept to copy from
65      */
66     @SuppressWarnings("java:S2637")
67     public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
68         super(authorativeConcept);
69     }
70
71     @Override
72     public ToscaConstraint toAuthorative() {
73         ToscaConstraint toscaConstraint = new ToscaConstraint();
74
75         if (operation == null) {
76             return null;
77         }
78
79         switch (operation) {
80             case EQ:
81                 toscaConstraint.setEqual(compareTo);
82                 break;
83
84             case GT:
85                 toscaConstraint.setGreaterThan(compareTo);
86                 break;
87
88             case GE:
89                 toscaConstraint.setGreaterOrEqual(compareTo);
90                 break;
91
92             case LT:
93                 toscaConstraint.setLessThan(compareTo);
94                 break;
95
96             case LE:
97                 toscaConstraint.setLessOrEqual(compareTo);
98                 break;
99
100             default:
101                 // Can't happen
102         }
103
104         return toscaConstraint;
105     }
106
107     @Override
108     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
109         // @formatter:off
110         if (toscaConstraint.getEqual() != null) {
111             operation = JpaToscaConstraintOperation.EQ;
112             compareTo = toscaConstraint.getEqual();
113         } else  if (toscaConstraint.getGreaterThan() != null) {
114             operation = JpaToscaConstraintOperation.GT;
115             compareTo = toscaConstraint.getGreaterThan();
116         } else  if (toscaConstraint.getGreaterOrEqual() != null) {
117             operation = JpaToscaConstraintOperation.GE;
118             compareTo = toscaConstraint.getGreaterOrEqual();
119         } else  if (toscaConstraint.getLessThan() != null) {
120             operation = JpaToscaConstraintOperation.LT;
121             compareTo = toscaConstraint.getLessThan();
122         } else  if (toscaConstraint.getLessOrEqual() != null) {
123             operation = JpaToscaConstraintOperation.LE;
124             compareTo = toscaConstraint.getLessOrEqual();
125         }
126         // @formatter:on
127     }
128
129     @Override
130     public int compareTo(JpaToscaConstraint otherConstraint) {
131         if (otherConstraint == null) {
132             return -1;
133         }
134         if (this == otherConstraint) {
135             return 0;
136         }
137         if (getClass() != otherConstraint.getClass()) {
138             return getClass().getName().compareTo(otherConstraint.getClass().getName());
139         }
140
141         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
142
143         int result = ObjectUtils.compare(operation, other.operation);
144         if (result != 0) {
145             return result;
146         }
147
148         return ObjectUtils.compare(compareTo, other.compareTo);
149     }
150 }