EMMA Coverage Report (generated Sun May 02 20:42:29 CEST 2010)
[all classes][hu.netmind.beankeeper.model.impl]

COVERAGE SUMMARY FOR SOURCE FILE [TypeUtils.java]

nameclass, %method, %block, %line, %
TypeUtils.java100% (1/1)50%  (1/2)81%  (206/255)81%  (38/47)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TypeUtils100% (1/1)50%  (1/2)81%  (206/255)81%  (38/47)
TypeUtils (): void 0%   (0/1)0%   (0/3)0%   (0/1)
getTypeValue (Object, Class): Object 100% (1/1)82%  (206/252)83%  (38/46)

1/**
2 * Copyright (C) 2006 NetMind Consulting Bt.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19package hu.netmind.beankeeper.model.impl;
20 
21import java.util.*;
22 
23/**
24 * Type, class and value conversion methods.
25 */
26public class TypeUtils
27{
28   /**
29    * Convert a value of a type to a given target type.
30    */
31   public static Object getTypeValue(Object value, Class attrClass)
32   {
33      Class valueClass = null;
34      // Check if value is null, note that primitive types will
35      // have a sane default if confronted with null
36      if ( value == null )
37      {
38         if ( attrClass.equals(byte.class) )
39            return new Byte((byte) 0);
40         else if ( attrClass.equals(double.class) )
41            return new Double(0);
42         else if ( attrClass.equals(float.class) )
43            return new Float(0);
44         else if ( attrClass.equals(int.class) )
45            return new Integer(0);
46         else if ( attrClass.equals(long.class) )
47            return new Long(0);
48         else if ( attrClass.equals(short.class) )
49            return new Short((short) 0);
50         else if ( attrClass.equals(boolean.class) )
51            return new Boolean(false);
52         else if ( attrClass.equals(char.class) )
53            return new Character('\u0000');
54         return null;
55      }
56      valueClass = value.getClass();
57      // Handle primitive types with exceptional circumstances
58      if ( (attrClass.equals(Character.class)) || (attrClass.equals(char.class)) 
59            && (valueClass.equals(String.class)) )
60      {
61         if ( ((String) value).length() > 0 )
62            return new Character(((String) value).charAt(0));
63      }
64      // Byte array
65      else if ( attrClass.equals(byte[].class) )
66         return value;
67      // Boolean
68      else if ( (attrClass.equals(Boolean.class) || attrClass.equals(boolean.class))
69            && ((valueClass.equals(Integer.class)) || 
70               (valueClass.equals(Long.class))) )
71      {
72         return new Boolean(((Number) value).intValue() > 0);
73      }
74      // Easy primitive classes
75      else if ( valueClass.equals(String.class) || 
76           valueClass.equals(Date.class) || 
77           valueClass.equals(Boolean.class)
78         )
79         return value;
80      // Container classes
81      else if ( (value instanceof Map) || (value instanceof List) )
82         return value;
83      // Not easy number classes
84      else if ( value instanceof Number )
85      {
86         Number number = (Number) value;
87         if ( attrClass.equals(Byte.class) || attrClass.equals(byte.class) )
88            return new Byte(number.byteValue());
89         if ( attrClass.equals(Double.class) || attrClass.equals(double.class) )
90            return new Double(number.doubleValue());
91         if ( attrClass.equals(Float.class) || attrClass.equals(float.class) )
92            return new Float(number.floatValue());
93         if ( attrClass.equals(Integer.class) || attrClass.equals(int.class) )
94            return new Integer(number.intValue());
95         if ( attrClass.equals(Long.class) || attrClass.equals(long.class) )
96            return new Long(number.longValue());
97         if ( attrClass.equals(Short.class) || attrClass.equals(short.class) )
98            return new Short(number.shortValue());
99      }
100      // All other objects
101      return value;
102   }
103 
104}

[all classes][hu.netmind.beankeeper.model.impl]
EMMA 2.0.5312debian (C) Vladimir Roubtsov