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