Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../../img/srcFileCovDistChart9.png 0% of files have more coverage
50   156   9   16,67
10   78   0,18   3
3     3  
1    
1,6% of code in this file is excluded from these metrics.
 
  ConverterManager       Line # 38 50 9 90,5% 0.9047619
 
  (2)
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23   
24    package net.smartlab.web.bean;
25   
26    import java.math.BigDecimal;
27    import java.util.HashMap;
28    import java.util.Locale;
29    import java.util.Map;
30    import net.smartlab.web.Enumeration;
31    import net.smartlab.web.StringEnumeration;
32   
33    /**
34    * TODO documentation
35    *
36    * @author rlogiacco
37    */
 
38    public class ConverterManager {
39   
40    /**
41    * @link aggregation <{net.smartlab.web.bean.Converter}>
42    * @directed directed
43    * @supplierCardinality 0..*
44    */
45    private Map converters = new HashMap();
46   
47    /**
48    * TODO documentation
49    *
50    * @param type
51    * @param value
52    * @param locale
53    * @return
54    * @throws ConversionException
55    */
 
56  52 toggle public Object convert(Class type, Object value, Locale locale) throws ConversionException {
57  52 if (value != null) {
58  28 try {
59  28 if (type.isArray()) {
60  14 return this.getConverter(type).convert(type, value, locale);
61    } else {
62  14 if (type.equals(String.class)) {
63  7 return this.getConverter(value.getClass()).convert(type, value, locale);
64    } else {
65  7 return this.getConverter(type).convert(type, value, locale);
66    }
67    }
68    } catch (NullPointerException npe) {
69    throw new ConversionException("unavailable converter for " + type);
70    }
71    } else {
72  24 return null;
73    }
74    }
75   
 
76  28 toggle private Converter getConverter(Class type) throws ConversionException {
77  28 Converter converter = (Converter)converters.get(type);
78  28 if (converter == null) {
79  0 if (type.equals(Object.class)) {
80  0 throw new ConversionException();
81    } else {
82  0 return getConverter(type.getSuperclass());
83    }
84    } else {
85  28 return converter;
86    }
87    }
88   
89    /**
90    * TODO documentation
91    *
92    * @return
93    */
 
94  1 toggle static public ConverterManager getDefault() {
95  1 ConverterManager manager = new ConverterManager();
96   
97    // Booleans
98  1 manager.converters.put(java.lang.Boolean.TYPE, new BooleanConverter(false));
99  1 manager.converters.put(java.lang.Boolean.class, new BooleanConverter(false));
100  1 manager.converters.put(boolean[].class, new BooleanArrayConverter());
101   
102    // Chars
103  1 manager.converters.put(java.lang.Character.TYPE, new CharConverter());
104  1 manager.converters.put(java.lang.Character.class, new CharConverter());
105  1 manager.converters.put(char[].class, new CharArrayConverter());
106   
107    // Bytes
108  1 manager.converters.put(java.lang.Byte.TYPE, new ByteConverter((byte)0));
109  1 manager.converters.put(java.lang.Byte.class, new ByteConverter((byte)0));
110  1 manager.converters.put(byte[].class, new ByteArrayConverter());
111   
112    // Shorts
113  1 manager.converters.put(java.lang.Short.TYPE, new ShortConverter((short)0));
114  1 manager.converters.put(java.lang.Short.class, new ShortConverter((short)0));
115  1 manager.converters.put(short[].class, new ShortArrayConverter());
116   
117    // Integers
118  1 manager.converters.put(java.lang.Integer.TYPE, new IntConverter(0));
119  1 manager.converters.put(java.lang.Integer.class, new IntConverter(0));
120  1 manager.converters.put(int[].class, new IntArrayConverter());
121   
122    // Longs
123  1 manager.converters.put(java.lang.Long.TYPE, new LongConverter(0));
124  1 manager.converters.put(java.lang.Long.class, new LongConverter(0));
125  1 manager.converters.put(long[].class, new LongArrayConverter());
126   
127    // Floats
128  1 manager.converters.put(java.lang.Float.TYPE, new FloatConverter(0));
129  1 manager.converters.put(java.lang.Float.class, new FloatConverter(0));
130  1 manager.converters.put(float[].class, new FloatArrayConverter());
131   
132    // Doubles
133  1 manager.converters.put(java.lang.Double.TYPE, new DoubleConverter(0));
134  1 manager.converters.put(java.lang.Double.class, new DoubleConverter(0));
135  1 manager.converters.put(double[].class, new DoubleArrayConverter());
136   
137    // Strings
138  1 manager.converters.put(java.lang.String.class, new StringConverter());
139  1 manager.converters.put(String[].class, new StringArrayConverter());
140   
141    // Dates and times
142  1 manager.converters.put(java.util.Date.class, new DateConverter());
143  1 manager.converters.put(java.sql.Date.class, new DateConverter());
144  1 manager.converters.put(java.sql.Time.class, new TimeConverter());
145  1 manager.converters.put(java.sql.Timestamp.class, new DateTimeConverter());
146   
147    // Enumerations
148  1 manager.converters.put(Enumeration.class, new EnumerationConverter());
149  1 manager.converters.put(StringEnumeration.class, new StringEnumerationConverter());
150   
151    // Currencies
152  1 manager.converters.put(BigDecimal.class, new BigDecimalConverter());
153  1 manager.converters.put(BigDecimal[].class, new BigDecimalArrayConverter());
154  1 return manager;
155    }
156    }