re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / ToscaType.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
21 package org.openecomp.sdc.be.model.tosca;
22
23 import java.text.DateFormat;
24 import java.text.ParseException;
25 import java.util.Locale;
26
27 /**
28  * The primitive type that TOSCA YAML supports.
29  * 
30  * @author mkv
31  */
32 public enum ToscaType {
33     STRING, INTEGER, FLOAT, BOOLEAN, TIMESTAMP, VERSION;
34
35     public boolean isValidValue(String value) {
36         switch (this) {
37         case BOOLEAN:
38             return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
39         case FLOAT:
40             return isFloat(value);
41         case INTEGER:
42             return isInteger(value);
43         case STRING:
44             return true;
45         case TIMESTAMP:
46             try {
47                 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
48                 return true;
49             } catch (ParseException e) {
50                 return false;
51             }
52         case VERSION:
53             return VersionUtil.isValid(value);
54         default:
55             return false;
56         }
57     }
58
59     private boolean isFloat(String value) {
60         try {
61             Float.valueOf(value);
62         } catch (NumberFormatException e) {
63             return false;
64         }
65         return true;
66     }
67
68     private boolean isInteger(String value) {
69         try {
70             Long.valueOf(value);
71         } catch (NumberFormatException e) {
72             return false;
73         }
74         return true;
75     }
76
77     public Object convert(String value) {
78         switch (this) {
79         case STRING:
80             return value;
81         case BOOLEAN:
82             return Boolean.valueOf(value);
83         case FLOAT:
84             return Double.valueOf(value);
85         case INTEGER:
86             return Long.valueOf(value);
87         case TIMESTAMP:
88             try {
89                 return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
90             } catch (ParseException e) {
91                 throw new IllegalArgumentException("Value must be a valid timestamp", e);
92             }
93         case VERSION:
94             return VersionUtil.parseVersion(value);
95         default:
96             return null;
97         }
98     }
99
100     @Override
101     public String toString() {
102         return name().toLowerCase();
103     }
104 }