]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveUtils.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
;
19 /** Converts to and from primitive types. */
20 public class PrimitiveUtils
{
22 * @deprecated Use {@link PrimitiveAccessor#TYPE_STRING} instead
24 public final static String TYPE_STRING
= PrimitiveAccessor
.TYPE_STRING
;
26 * @deprecated Use {@link PrimitiveAccessor#TYPE_INTEGER} instead
28 public final static String TYPE_INTEGER
= PrimitiveAccessor
.TYPE_INTEGER
;
30 * @deprecated Use {@link PrimitiveAccessor#TYPE_LONG} instead
32 public final static String TYPE_LONG
= PrimitiveAccessor
.TYPE_LONG
;
34 * @deprecated Use {@link PrimitiveAccessor#TYPE_FLOAT} instead
36 public final static String TYPE_FLOAT
= PrimitiveAccessor
.TYPE_FLOAT
;
38 * @deprecated Use {@link PrimitiveAccessor#TYPE_DOUBLE} instead
40 public final static String TYPE_DOUBLE
= PrimitiveAccessor
.TYPE_DOUBLE
;
42 * @deprecated Use {@link PrimitiveAccessor#TYPE_BOOLEAN} instead
44 public final static String TYPE_BOOLEAN
= PrimitiveAccessor
.TYPE_BOOLEAN
;
46 private PrimitiveUtils() {
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
))
54 else if (PrimitiveAccessor
.TYPE_INTEGER
.equals(type
))
56 else if (PrimitiveAccessor
.TYPE_LONG
.equals(type
))
58 else if (PrimitiveAccessor
.TYPE_FLOAT
.equals(type
))
60 else if (PrimitiveAccessor
.TYPE_DOUBLE
.equals(type
))
62 else if (PrimitiveAccessor
.TYPE_BOOLEAN
.equals(type
))
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
;
86 public static Object
convert(String type
, String str
) {
87 if (PrimitiveAccessor
.TYPE_STRING
.equals(type
)) {
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
));