Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / LessThan.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.utils.ThreadLocalsHolder;
25
26 import java.util.Arrays;
27 import java.util.Date;
28
29 public class LessThan extends Constraint {
30
31     @Override
32     protected void setValues() {
33
34         setConstraintKey(LESS_THAN);
35         // timestamps are loaded as Date objects
36         addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
37         //validTypes.add("datetime.date");
38         //validTypes.add("datetime.time");
39         //validTypes.add("datetime.datetime");
40
41
42         validPropTypes.add(Schema.INTEGER);
43         validPropTypes.add(Schema.FLOAT);
44         validPropTypes.add(Schema.TIMESTAMP);
45         validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
46         validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
47         validPropTypes.add(Schema.SCALAR_UNIT_TIME);
48
49     }
50
51     public LessThan(String name, String type, Object c) {
52         super(name, type, c);
53
54         if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
55             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE111", "InvalidSchemaError: The property \"less_than\" expects comparable values"));
56         }
57     }
58
59     @Override
60     protected boolean isValid(Object value) {
61
62         // timestamps
63         if (value instanceof Date) {
64             if (constraintValue instanceof Date) {
65                 return ((Date) value).before((Date) constraintValue);
66             }
67             return false;
68         }
69
70         Double n1 = new Double(value.toString());
71         Double n2 = new Double(constraintValue.toString());
72         return n1 < n2;
73     }
74
75     @Override
76     protected String errMsg(Object value) {
77         return String.format("The value \"%s\" of property \"%s\" must be less than \"%s\"",
78                 valueMsg, propertyName, constraintValueMsg);
79     }
80
81 }
82
83 /*python
84
85 class LessThan(Constraint):
86 """Constraint class for "less_than"
87
88 Constrains a property or parameter to a value less than ('<')
89 the value declared.
90 """
91
92 constraint_key = Constraint.LESS_THAN
93
94 valid_types = (int, float, datetime.date,
95                datetime.time, datetime.datetime)
96
97 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
98                     Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
99                     Schema.SCALAR_UNIT_TIME)
100
101 def __init__(self, property_name, property_type, constraint):
102     super(LessThan, self).__init__(property_name, property_type,
103                                    constraint)
104     if not isinstance(self.constraint_value, self.valid_types):
105         ValidationIsshueCollector.appendException(
106             InvalidSchemaError(message=_('The property "less_than" '
107                                          'expects comparable values.')))
108
109 def _is_valid(self, value):
110     if value < self.constraint_value:
111         return True
112
113     return False
114
115 def _err_msg(self, value):
116     return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
117               'less than "%(cvalue)s".') %
118             dict(pname=self.property_name,
119                  pvalue=self.value_msg,
120                  cvalue=self.constraint_value_msg))
121 */