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