]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveUtils.java
Improve diff issue
[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 /**
22 * @deprecated Use {@link PrimitiveAccessor#TYPE_STRING} instead
23 */
24 public final static String TYPE_STRING = PrimitiveAccessor.TYPE_STRING;
25 /**
26 * @deprecated Use {@link PrimitiveAccessor#TYPE_INTEGER} instead
27 */
28 public final static String TYPE_INTEGER = PrimitiveAccessor.TYPE_INTEGER;
29 /**
30 * @deprecated Use {@link PrimitiveAccessor#TYPE_LONG} instead
31 */
32 public final static String TYPE_LONG = PrimitiveAccessor.TYPE_LONG;
33 /**
34 * @deprecated Use {@link PrimitiveAccessor#TYPE_FLOAT} instead
35 */
36 public final static String TYPE_FLOAT = PrimitiveAccessor.TYPE_FLOAT;
37 /**
38 * @deprecated Use {@link PrimitiveAccessor#TYPE_DOUBLE} instead
39 */
40 public final static String TYPE_DOUBLE = PrimitiveAccessor.TYPE_DOUBLE;
41 /**
42 * @deprecated Use {@link PrimitiveAccessor#TYPE_BOOLEAN} instead
43 */
44 public final static String TYPE_BOOLEAN = PrimitiveAccessor.TYPE_BOOLEAN;
45
46 private PrimitiveUtils() {
47
48 }
49
50 /** @return the class or null if the provided type is not a primitive */
51 public static Class<?> typeAsClass(String type) {
52 if (PrimitiveAccessor.TYPE_STRING.equals(type))
53 return String.class;
54 else if (PrimitiveAccessor.TYPE_INTEGER.equals(type))
55 return Integer.class;
56 else if (PrimitiveAccessor.TYPE_LONG.equals(type))
57 return Long.class;
58 else if (PrimitiveAccessor.TYPE_FLOAT.equals(type))
59 return Float.class;
60 else if (PrimitiveAccessor.TYPE_DOUBLE.equals(type))
61 return Double.class;
62 else if (PrimitiveAccessor.TYPE_BOOLEAN.equals(type))
63 return Boolean.class;
64 else
65 return null;
66 }
67
68 /** @return the type or null if the provided class is not a primitive */
69 public static String classAsType(Class<?> clss) {
70 if (String.class.isAssignableFrom(clss))
71 return PrimitiveAccessor.TYPE_STRING;
72 else if (Integer.class.isAssignableFrom(clss))
73 return PrimitiveAccessor.TYPE_INTEGER;
74 else if (Long.class.isAssignableFrom(clss))
75 return PrimitiveAccessor.TYPE_LONG;
76 else if (Float.class.isAssignableFrom(clss))
77 return PrimitiveAccessor.TYPE_FLOAT;
78 else if (Double.class.isAssignableFrom(clss))
79 return PrimitiveAccessor.TYPE_DOUBLE;
80 else if (Boolean.class.isAssignableFrom(clss))
81 return PrimitiveAccessor.TYPE_BOOLEAN;
82 else
83 return null;
84 }
85
86 public static Object convert(String type, String str) {
87 if (PrimitiveAccessor.TYPE_STRING.equals(type)) {
88 return str;
89 } else if (PrimitiveAccessor.TYPE_INTEGER.equals(type)) {
90 return (Integer.parseInt(str));
91 } else if (PrimitiveAccessor.TYPE_LONG.equals(type)) {
92 return (Long.parseLong(str));
93 } else if (PrimitiveAccessor.TYPE_FLOAT.equals(type)) {
94 return (Float.parseFloat(str));
95 } else if (PrimitiveAccessor.TYPE_DOUBLE.equals(type)) {
96 return (Double.parseDouble(str));
97 } else if (PrimitiveAccessor.TYPE_BOOLEAN.equals(type)) {
98 return (Boolean.parseBoolean(str));
99 } else {
100 return str;
101 }
102 }
103
104 }