4207 |
04 Apr 08 |
nicklas |
1 |
/** |
4207 |
04 Apr 08 |
nicklas |
$Id$ |
4207 |
04 Apr 08 |
nicklas |
3 |
|
4207 |
04 Apr 08 |
nicklas |
Copyright (C) Authors contributing to this file. |
4207 |
04 Apr 08 |
nicklas |
5 |
|
4207 |
04 Apr 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4207 |
04 Apr 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4207 |
04 Apr 08 |
nicklas |
8 |
|
4207 |
04 Apr 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4207 |
04 Apr 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
4207 |
04 Apr 08 |
nicklas |
of the License, or (at your option) any later version. |
4207 |
04 Apr 08 |
nicklas |
13 |
|
4207 |
04 Apr 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4207 |
04 Apr 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4207 |
04 Apr 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4207 |
04 Apr 08 |
nicklas |
GNU General Public License for more details. |
4207 |
04 Apr 08 |
nicklas |
18 |
|
4207 |
04 Apr 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4207 |
04 Apr 08 |
nicklas |
21 |
*/ |
4207 |
04 Apr 08 |
nicklas |
22 |
package net.sf.basedb.util.extensions.debug; |
4207 |
04 Apr 08 |
nicklas |
23 |
|
4207 |
04 Apr 08 |
nicklas |
24 |
import java.util.HashMap; |
4207 |
04 Apr 08 |
nicklas |
25 |
import java.util.Map; |
4207 |
04 Apr 08 |
nicklas |
26 |
|
4207 |
04 Apr 08 |
nicklas |
27 |
import net.sf.basedb.core.Type; |
4207 |
04 Apr 08 |
nicklas |
28 |
|
4207 |
04 Apr 08 |
nicklas |
29 |
/** |
4208 |
07 Apr 08 |
nicklas |
Convert strings to other data types. This class is experimental |
4208 |
07 Apr 08 |
nicklas |
only and may change in the future. |
4207 |
04 Apr 08 |
nicklas |
32 |
|
4207 |
04 Apr 08 |
nicklas |
@author nicklas |
4207 |
04 Apr 08 |
nicklas |
@version 2.7 |
4207 |
04 Apr 08 |
nicklas |
@base.modified $Date$ |
4208 |
07 Apr 08 |
nicklas |
36 |
*/ |
4207 |
04 Apr 08 |
nicklas |
37 |
public class StringConverter |
4207 |
04 Apr 08 |
nicklas |
38 |
{ |
4207 |
04 Apr 08 |
nicklas |
39 |
|
6875 |
20 Apr 15 |
nicklas |
40 |
private static Map<Class<?>, StringConverter> converter; |
4207 |
04 Apr 08 |
nicklas |
41 |
|
4207 |
04 Apr 08 |
nicklas |
42 |
static |
4207 |
04 Apr 08 |
nicklas |
43 |
{ |
6875 |
20 Apr 15 |
nicklas |
44 |
converter = new HashMap<Class<?>, StringConverter>(); |
4207 |
04 Apr 08 |
nicklas |
45 |
converter.put(int.class, new StringConverter(Type.INT, 0)); |
4207 |
04 Apr 08 |
nicklas |
46 |
converter.put(Integer.class, new StringConverter(Type.INT, null)); |
4207 |
04 Apr 08 |
nicklas |
47 |
converter.put(long.class, new StringConverter(Type.LONG, 0l)); |
4207 |
04 Apr 08 |
nicklas |
48 |
converter.put(Long.class, new StringConverter(Type.LONG, null)); |
4207 |
04 Apr 08 |
nicklas |
49 |
converter.put(float.class, new StringConverter(Type.FLOAT, 0f)); |
4207 |
04 Apr 08 |
nicklas |
50 |
converter.put(Float.class, new StringConverter(Type.FLOAT, null)); |
4207 |
04 Apr 08 |
nicklas |
51 |
converter.put(double.class, new StringConverter(Type.DOUBLE, 0d)); |
4207 |
04 Apr 08 |
nicklas |
52 |
converter.put(Double.class, new StringConverter(Type.DOUBLE, null)); |
4207 |
04 Apr 08 |
nicklas |
53 |
converter.put(boolean.class, new StringConverter(Type.BOOLEAN, false)); |
4207 |
04 Apr 08 |
nicklas |
54 |
converter.put(Boolean.class, new StringConverter(Type.BOOLEAN, null)); |
4207 |
04 Apr 08 |
nicklas |
55 |
converter.put(String.class, new StringConverter(Type.STRING, null)); |
4207 |
04 Apr 08 |
nicklas |
56 |
} |
4207 |
04 Apr 08 |
nicklas |
57 |
|
6875 |
20 Apr 15 |
nicklas |
58 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
4207 |
04 Apr 08 |
nicklas |
59 |
public static Object convertString(Class toClass, String value) |
4207 |
04 Apr 08 |
nicklas |
60 |
{ |
4207 |
04 Apr 08 |
nicklas |
61 |
StringConverter c = converter.get(toClass); |
6028 |
26 Mar 12 |
nicklas |
62 |
if (c == null && toClass.isEnum()) |
6028 |
26 Mar 12 |
nicklas |
63 |
{ |
6028 |
26 Mar 12 |
nicklas |
64 |
try |
6028 |
26 Mar 12 |
nicklas |
65 |
{ |
6028 |
26 Mar 12 |
nicklas |
66 |
return Enum.valueOf(toClass, value); |
6028 |
26 Mar 12 |
nicklas |
67 |
} |
6028 |
26 Mar 12 |
nicklas |
68 |
catch (RuntimeException ex) |
6028 |
26 Mar 12 |
nicklas |
69 |
{} |
6028 |
26 Mar 12 |
nicklas |
70 |
} |
4207 |
04 Apr 08 |
nicklas |
71 |
return c == null ? null : c.convert(value); |
4207 |
04 Apr 08 |
nicklas |
72 |
} |
4207 |
04 Apr 08 |
nicklas |
73 |
|
4207 |
04 Apr 08 |
nicklas |
74 |
private final Type type; |
4207 |
04 Apr 08 |
nicklas |
75 |
private final Object defaultValue; |
4207 |
04 Apr 08 |
nicklas |
76 |
|
4207 |
04 Apr 08 |
nicklas |
77 |
private StringConverter(Type type, Object defaultValue) |
4207 |
04 Apr 08 |
nicklas |
78 |
{ |
4207 |
04 Apr 08 |
nicklas |
79 |
this.type = type; |
4207 |
04 Apr 08 |
nicklas |
80 |
this.defaultValue = defaultValue; |
4207 |
04 Apr 08 |
nicklas |
81 |
} |
4207 |
04 Apr 08 |
nicklas |
82 |
|
4207 |
04 Apr 08 |
nicklas |
83 |
private Object convert(String value) |
4207 |
04 Apr 08 |
nicklas |
84 |
{ |
4207 |
04 Apr 08 |
nicklas |
85 |
if (value == null) return defaultValue; |
4207 |
04 Apr 08 |
nicklas |
86 |
return type.parseString(value); |
4207 |
04 Apr 08 |
nicklas |
87 |
} |
4207 |
04 Apr 08 |
nicklas |
88 |
|
4207 |
04 Apr 08 |
nicklas |
89 |
} |