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