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=========================================================
21 package org.openecomp.sdc.be.model.tosca;
23 import java.text.DateFormat;
24 import java.text.ParseException;
25 import java.util.Locale;
28 * The primitive type that TOSCA YAML supports.
32 public enum ToscaType {
33 STRING, INTEGER, FLOAT, BOOLEAN, TIMESTAMP, VERSION;
35 // private static final Pattern TIMESTAMP_REGEX = Pattern
36 // .compile("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?([Tt]|[
37 // \\t]+)[0-9][0-9]?:[0-9][0-9]:[0-9][0-9](\\.[0-9]*)?(([ \\t]*)Z|([
38 // \\t]*)[-+][0-9][0-9]?(:[0-9][0-9])?)?");
40 public static ToscaType fromYamlTypeName(String typeName) {
41 if (typeName == null) {
45 return ToscaType.valueOf(typeName.toUpperCase());
46 } catch (IllegalArgumentException e) {
51 public boolean isValidValue(String value) {
54 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
56 return isFloat(value);
58 return isInteger(value);
63 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
65 } catch (ParseException e) {
69 return VersionUtil.isValid(value);
75 private boolean isFloat(String value) {
78 } catch (NumberFormatException e) {
84 private boolean isInteger(String value) {
87 } catch (NumberFormatException e) {
93 public Object convert(String value) {
98 return Boolean.valueOf(value);
100 return Double.valueOf(value);
102 return Long.valueOf(value);
105 return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
106 } catch (ParseException e) {
107 throw new IllegalArgumentException("Value must be a valid timestamp", e);
110 return VersionUtil.parseVersion(value);
117 public String toString() {
118 return name().toLowerCase();