Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / IntegerValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.openecomp.sdc.be.model.tosca.validators;
21
22 import java.util.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.openecomp.sdc.be.model.DataTypeDefinition;
26
27 public class IntegerValidator implements PropertyTypeValidator {
28
29     private static IntegerValidator integerValidator = new IntegerValidator();
30     private PatternBase base8Pattern = new PatternBase(Pattern.compile("([-+])?0o([0-7]+)"), 8);
31     private PatternBase base10Pattern = new PatternBase(Pattern.compile("([-+])?(0|[1-9][0-9]*)"), 10);
32     private PatternBase base16Pattern = new PatternBase(Pattern.compile("([-+])?0x([0-9a-fA-F]+)"), 16);
33     private PatternBase[] patterns = {base10Pattern, base8Pattern, base16Pattern};
34
35     private IntegerValidator() {
36     }
37
38     public static IntegerValidator getInstance() {
39         return integerValidator;
40     }
41
42     @Override
43     public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
44         if (value == null || value.isEmpty()) {
45             return true;
46         }
47         for (PatternBase patternBase : patterns) {
48             Matcher matcher = patternBase.pattern.matcher(value);
49             Long parsed = null;
50             if (matcher.matches()) {
51                 try {
52                     parsed = Long.parseLong(matcher.group(2), patternBase.base);
53                     if (matcher.group(1) != null && matcher.group(1).compareTo("-") == 0) {
54                         parsed *= -1;
55                     }
56                     return (Integer.MIN_VALUE <= parsed && parsed <= (Integer.MAX_VALUE)) ? true : false;
57                 } catch (NumberFormatException e) {
58                     return false;
59                 }
60             }
61         }
62         return false;
63     }
64
65     @Override
66     public boolean isValid(String value, String innerType) {
67         return isValid(value, innerType, null);
68     }
69
70     private class PatternBase {
71
72         Pattern pattern;
73         Integer base;
74
75         public PatternBase(Pattern pattern, Integer base) {
76             this.pattern = pattern;
77             this.base = base;
78         }
79     }
80 }