Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / Length.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.Collections;
27
28 public class Length extends Constraint {
29     // Constraint class for "length"
30
31     // Constrains the property or parameter to a value of a given length.
32
33     @Override
34     protected void setValues() {
35
36         setConstraintKey(LENGTH);
37         addValidTypes(Collections.singletonList("Integer"));
38
39         validPropTypes.add(Schema.STRING);
40
41     }
42
43     public Length(String name, String type, Object c) {
44         super(name, type, c);
45
46         if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
47             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE109", "InvalidSchemaError: The property \"length\" expects an integer"));
48         }
49     }
50
51     @Override
52     protected boolean isValid(Object value) {
53         if (value instanceof String && constraintValue instanceof Integer &&
54                 ((String) value).length() == (Integer) constraintValue) {
55             return true;
56         }
57         return false;
58     }
59
60     @Override
61     protected String errMsg(Object value) {
62         return String.format("Length of value \"%s\" of property \"%s\" must be equal to \"%s\"",
63                 value.toString(), propertyName, constraintValue.toString());
64     }
65
66 }
67
68 /*python
69         class Length(Constraint):
70         """Constraint class for "length"
71         
72         Constrains the property or parameter to a value of a given length.
73         """
74         
75         constraint_key = Constraint.LENGTH
76         
77         valid_types = (int, )
78         
79         valid_prop_types = (Schema.STRING, )
80         
81         def __init__(self, property_name, property_type, constraint):
82             super(Length, self).__init__(property_name, property_type, constraint)
83             if not isinstance(self.constraint_value, self.valid_types):
84                 ValidationIsshueCollector.appendException(
85                     InvalidSchemaError(message=_('The property "length" expects '
86                                                  'an integer.')))
87         
88         def _is_valid(self, value):
89             if isinstance(value, str) and len(value) == self.constraint_value:
90                 return True
91         
92             return False
93         
94         def _err_msg(self, value):
95             return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
96                       'must be equal to "%(cvalue)s".') %
97                     dict(pname=self.property_name,
98                          pvalue=value,
99                          cvalue=self.constraint_value))
100 */