]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveUtils.java
c5a3a8605b950b4e584472c00bbb70d6be98a9cb
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / PrimitiveUtils.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 /** Converts to and from primitive types. */
20 public class PrimitiveUtils {
21 public final static String TYPE_STRING = "string";
22 public final static String TYPE_INTEGER = "integer";
23 public final static String TYPE_LONG = "long";
24 public final static String TYPE_FLOAT = "float";
25 public final static String TYPE_DOUBLE = "double";
26 public final static String TYPE_BOOLEAN = "boolean";
27
28 private PrimitiveUtils() {
29
30 }
31
32 /** @return the class or null if the provided type is not a primitive */
33 public static Class<?> typeAsClass(String type) {
34 if (TYPE_STRING.equals(type))
35 return String.class;
36 else if (TYPE_INTEGER.equals(type))
37 return Integer.class;
38 else if (TYPE_LONG.equals(type))
39 return Long.class;
40 else if (TYPE_FLOAT.equals(type))
41 return Float.class;
42 else if (TYPE_DOUBLE.equals(type))
43 return Double.class;
44 else if (TYPE_BOOLEAN.equals(type))
45 return Boolean.class;
46 else
47 return null;
48 }
49
50 /** @return the type or null if the provided class is not a primitive */
51 public static String classAsType(Class<?> clss) {
52 if (String.class.isAssignableFrom(clss))
53 return TYPE_STRING;
54 else if (Integer.class.isAssignableFrom(clss))
55 return TYPE_INTEGER;
56 else if (Long.class.isAssignableFrom(clss))
57 return TYPE_LONG;
58 else if (Float.class.isAssignableFrom(clss))
59 return TYPE_FLOAT;
60 else if (Double.class.isAssignableFrom(clss))
61 return TYPE_DOUBLE;
62 else if (Boolean.class.isAssignableFrom(clss))
63 return TYPE_BOOLEAN;
64 else
65 return null;
66 }
67
68 public static Object convert(String type, String str) {
69 if (TYPE_STRING.equals(type)) {
70 return str;
71 } else if (TYPE_INTEGER.equals(type)) {
72 return (Integer.parseInt(str));
73 } else if (TYPE_LONG.equals(type)) {
74 return (Long.parseLong(str));
75 } else if (TYPE_FLOAT.equals(type)) {
76 return (Float.parseFloat(str));
77 } else if (TYPE_DOUBLE.equals(type)) {
78 return (Double.parseDouble(str));
79 } else if (TYPE_BOOLEAN.equals(type)) {
80 return (Boolean.parseBoolean(str));
81 } else {
82 return str;
83 }
84 }
85
86 }