]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org/argeo/slc/primitive/PrimitiveUtils.java
1 package org
.argeo
.slc
.primitive
;
3 /** Converts to and from primitive types. */
4 public class PrimitiveUtils
{
6 * @deprecated Use {@link PrimitiveAccessor#TYPE_STRING} instead
8 public final static String TYPE_STRING
= PrimitiveAccessor
.TYPE_STRING
;
10 * @deprecated Use {@link PrimitiveAccessor#TYPE_INTEGER} instead
12 public final static String TYPE_INTEGER
= PrimitiveAccessor
.TYPE_INTEGER
;
14 * @deprecated Use {@link PrimitiveAccessor#TYPE_LONG} instead
16 public final static String TYPE_LONG
= PrimitiveAccessor
.TYPE_LONG
;
18 * @deprecated Use {@link PrimitiveAccessor#TYPE_FLOAT} instead
20 public final static String TYPE_FLOAT
= PrimitiveAccessor
.TYPE_FLOAT
;
22 * @deprecated Use {@link PrimitiveAccessor#TYPE_DOUBLE} instead
24 public final static String TYPE_DOUBLE
= PrimitiveAccessor
.TYPE_DOUBLE
;
26 * @deprecated Use {@link PrimitiveAccessor#TYPE_BOOLEAN} instead
28 public final static String TYPE_BOOLEAN
= PrimitiveAccessor
.TYPE_BOOLEAN
;
30 private PrimitiveUtils() {
34 /** @return the class or null if the provided type is not a primitive */
35 public static Class
<?
> typeAsClass(String type
) {
36 if (PrimitiveAccessor
.TYPE_STRING
.equals(type
))
38 else if (PrimitiveAccessor
.TYPE_PASSWORD
.equals(type
))
40 else if (PrimitiveAccessor
.TYPE_INTEGER
.equals(type
))
42 else if (PrimitiveAccessor
.TYPE_LONG
.equals(type
))
44 else if (PrimitiveAccessor
.TYPE_FLOAT
.equals(type
))
46 else if (PrimitiveAccessor
.TYPE_DOUBLE
.equals(type
))
48 else if (PrimitiveAccessor
.TYPE_BOOLEAN
.equals(type
))
54 /** @return the type or null if the provided class is not a primitive */
55 public static String
classAsType(Class
<?
> clss
) {
56 if (String
.class.isAssignableFrom(clss
))
57 return PrimitiveAccessor
.TYPE_STRING
;
58 else if (char[].class.isAssignableFrom(clss
))
59 return PrimitiveAccessor
.TYPE_PASSWORD
;
60 else if (Integer
.class.isAssignableFrom(clss
))
61 return PrimitiveAccessor
.TYPE_INTEGER
;
62 else if (Long
.class.isAssignableFrom(clss
))
63 return PrimitiveAccessor
.TYPE_LONG
;
64 else if (Float
.class.isAssignableFrom(clss
))
65 return PrimitiveAccessor
.TYPE_FLOAT
;
66 else if (Double
.class.isAssignableFrom(clss
))
67 return PrimitiveAccessor
.TYPE_DOUBLE
;
68 else if (Boolean
.class.isAssignableFrom(clss
))
69 return PrimitiveAccessor
.TYPE_BOOLEAN
;
74 /** Parse string as an object. Passwords are returned as String.*/
75 public static Object
convert(String type
, String str
) {
76 if (PrimitiveAccessor
.TYPE_STRING
.equals(type
)) {
78 } else if (PrimitiveAccessor
.TYPE_PASSWORD
.equals(type
)) {
80 } else if (PrimitiveAccessor
.TYPE_INTEGER
.equals(type
)) {
81 return (Integer
.parseInt(str
));
82 } else if (PrimitiveAccessor
.TYPE_LONG
.equals(type
)) {
83 return (Long
.parseLong(str
));
84 } else if (PrimitiveAccessor
.TYPE_FLOAT
.equals(type
)) {
85 return (Float
.parseFloat(str
));
86 } else if (PrimitiveAccessor
.TYPE_DOUBLE
.equals(type
)) {
87 return (Double
.parseDouble(str
));
88 } else if (PrimitiveAccessor
.TYPE_BOOLEAN
.equals(type
)) {
89 return (Boolean
.parseBoolean(str
));