2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.model.tosca.validators;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 public class IntegerValidator implements PropertyTypeValidator {
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};
35 private IntegerValidator() {
38 public static IntegerValidator getInstance() {
39 return integerValidator;
43 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
44 if (value == null || value.isEmpty()) {
47 for (PatternBase patternBase : patterns) {
48 Matcher matcher = patternBase.pattern.matcher(value);
50 if (matcher.matches()) {
52 parsed = Long.parseLong(matcher.group(2), patternBase.base);
53 if (matcher.group(1) != null && matcher.group(1).compareTo("-") == 0) {
56 return (Integer.MIN_VALUE <= parsed && parsed <= (Integer.MAX_VALUE)) ? true : false;
57 } catch (NumberFormatException e) {
66 public boolean isValid(String value, String innerType) {
67 return isValid(value, innerType, null);
70 private class PatternBase {
75 public PatternBase(Pattern pattern, Integer base) {
76 this.pattern = pattern;