Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / GreaterOrEqual.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.elements.constraints;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.functions.Function;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26
27 import java.util.Arrays;
28 import java.util.Date;
29
30 public class GreaterOrEqual extends Constraint {
31     // Constraint class for "greater_or_equal"
32
33     // Constrains a property or parameter to a value greater than or equal
34     // to ('>=') the value declared.
35
36     protected void setValues() {
37
38         setConstraintKey(GREATER_OR_EQUAL);
39
40         // timestamps are loaded as Date objects
41         addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
42         //validTypes.add("datetime.date");
43         //validTypes.add("datetime.time");
44         //validTypes.add("datetime.datetime");
45
46         validPropTypes.add(Schema.INTEGER);
47         validPropTypes.add(Schema.FLOAT);
48         validPropTypes.add(Schema.TIMESTAMP);
49         validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
50         validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
51         validPropTypes.add(Schema.SCALAR_UNIT_TIME);
52
53     }
54
55     public GreaterOrEqual(String name, String type, Object c) {
56         super(name, type, c);
57
58         if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
59             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE104", "InvalidSchemaError: The property \"greater_or_equal\" expects comparable values"));
60         }
61     }
62
63
64     @Override
65     protected boolean isValid(Object value) {
66         if (Function.isFunction(value)) {
67             return true;
68         }
69
70         // timestamps
71         if (value instanceof Date) {
72             if (constraintValue instanceof Date) {
73                 return !((Date) value).before((Date) constraintValue);
74             }
75             return false;
76         }
77         // all others
78         Double n1 = new Double(value.toString());
79         Double n2 = new Double(constraintValue.toString());
80         return n1 >= n2;
81     }
82
83     protected String errMsg(Object value) {
84         return String.format("The value \"%s\" of property \"%s\" must be greater or equal to \"%s\"",
85                 valueMsg, propertyName, constraintValueMsg);
86     }
87 }
88
89 /*python
90
91 class GreaterOrEqual(Constraint):
92 """Constraint class for "greater_or_equal"
93
94 Constrains a property or parameter to a value greater than or equal
95 to ('>=') the value declared.
96 """
97
98 constraint_key = Constraint.GREATER_OR_EQUAL
99
100 valid_types = (int, float, datetime.date,
101                datetime.time, datetime.datetime)
102
103 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
104                     Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
105                     Schema.SCALAR_UNIT_TIME)
106
107 def __init__(self, property_name, property_type, constraint):
108     super(GreaterOrEqual, self).__init__(property_name, property_type,
109                                          constraint)
110     if not isinstance(self.constraint_value, self.valid_types):
111         ThreadLocalsHolder.getCollector().appendException(
112             InvalidSchemaError(message=_('The property '
113                                          '"greater_or_equal" expects '
114                                          'comparable values.')))
115
116 def _is_valid(self, value):
117     if toscaparser.functions.is_function(value) or \
118        value >= self.constraint_value:
119         return True
120     return False
121
122 def _err_msg(self, value):
123     return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
124               'greater than or equal to "%(cvalue)s".') %
125             dict(pname=self.property_name,
126                  pvalue=self.value_msg,
127                  cvalue=self.constraint_value_msg))
128
129
130 */