]>
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) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.core
.execution
;
18 /** Converts to and from primitive types. */
19 public class PrimitiveUtils
{
21 * @deprecated Use {@link PrimitiveAccessor#TYPE_STRING} instead
23 public final static String TYPE_STRING
= PrimitiveAccessor
.TYPE_STRING
;
25 * @deprecated Use {@link PrimitiveAccessor#TYPE_INTEGER} instead
27 public final static String TYPE_INTEGER
= PrimitiveAccessor
.TYPE_INTEGER
;
29 * @deprecated Use {@link PrimitiveAccessor#TYPE_LONG} instead
31 public final static String TYPE_LONG
= PrimitiveAccessor
.TYPE_LONG
;
33 * @deprecated Use {@link PrimitiveAccessor#TYPE_FLOAT} instead
35 public final static String TYPE_FLOAT
= PrimitiveAccessor
.TYPE_FLOAT
;
37 * @deprecated Use {@link PrimitiveAccessor#TYPE_DOUBLE} instead
39 public final static String TYPE_DOUBLE
= PrimitiveAccessor
.TYPE_DOUBLE
;
41 * @deprecated Use {@link PrimitiveAccessor#TYPE_BOOLEAN} instead
43 public final static String TYPE_BOOLEAN
= PrimitiveAccessor
.TYPE_BOOLEAN
;
45 private PrimitiveUtils() {
49 /** @return the class or null if the provided type is not a primitive */
50 public static Class
<?
> typeAsClass(String type
) {
51 if (PrimitiveAccessor
.TYPE_STRING
.equals(type
))
53 else if (PrimitiveAccessor
.TYPE_INTEGER
.equals(type
))
55 else if (PrimitiveAccessor
.TYPE_LONG
.equals(type
))
57 else if (PrimitiveAccessor
.TYPE_FLOAT
.equals(type
))
59 else if (PrimitiveAccessor
.TYPE_DOUBLE
.equals(type
))
61 else if (PrimitiveAccessor
.TYPE_BOOLEAN
.equals(type
))
67 /** @return the type or null if the provided class is not a primitive */
68 public static String
classAsType(Class
<?
> clss
) {
69 if (String
.class.isAssignableFrom(clss
))
70 return PrimitiveAccessor
.TYPE_STRING
;
71 else if (Integer
.class.isAssignableFrom(clss
))
72 return PrimitiveAccessor
.TYPE_INTEGER
;
73 else if (Long
.class.isAssignableFrom(clss
))
74 return PrimitiveAccessor
.TYPE_LONG
;
75 else if (Float
.class.isAssignableFrom(clss
))
76 return PrimitiveAccessor
.TYPE_FLOAT
;
77 else if (Double
.class.isAssignableFrom(clss
))
78 return PrimitiveAccessor
.TYPE_DOUBLE
;
79 else if (Boolean
.class.isAssignableFrom(clss
))
80 return PrimitiveAccessor
.TYPE_BOOLEAN
;
85 public static Object
convert(String type
, String str
) {
86 if (PrimitiveAccessor
.TYPE_STRING
.equals(type
)) {
88 } else if (PrimitiveAccessor
.TYPE_INTEGER
.equals(type
)) {
89 return (Integer
.parseInt(str
));
90 } else if (PrimitiveAccessor
.TYPE_LONG
.equals(type
)) {
91 return (Long
.parseLong(str
));
92 } else if (PrimitiveAccessor
.TYPE_FLOAT
.equals(type
)) {
93 return (Float
.parseFloat(str
));
94 } else if (PrimitiveAccessor
.TYPE_DOUBLE
.equals(type
)) {
95 return (Double
.parseDouble(str
));
96 } else if (PrimitiveAccessor
.TYPE_BOOLEAN
.equals(type
)) {
97 return (Boolean
.parseBoolean(str
));