KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > Coercion4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation;
22
23 /**
24  * @sharpen.ignore
25  */

26 public class Coercion4 {
27     public static Object JavaDoc toSByte(Object JavaDoc obj) {
28         if(obj instanceof Byte JavaDoc){
29             return obj;
30         }
31         if(obj instanceof Number JavaDoc){
32             Number JavaDoc number=(Number JavaDoc)obj;
33             if(number.byteValue()==number.doubleValue()) {
34                 return new Byte JavaDoc((number).byteValue());
35             }
36         }
37         return No4.INSTANCE;
38     }
39
40     public static Object JavaDoc toShort(Object JavaDoc obj) {
41         if(obj instanceof Short JavaDoc){
42             return obj;
43         }
44         if(obj instanceof Number JavaDoc){
45             Number JavaDoc number=(Number JavaDoc)obj;
46             if(number.shortValue()==number.doubleValue()) {
47                 return new Short JavaDoc((number).shortValue());
48             }
49         }
50         return No4.INSTANCE;
51     }
52
53     public static Object JavaDoc toInt(Object JavaDoc obj) {
54         if(obj instanceof Integer JavaDoc){
55             return obj;
56         }
57         if(obj instanceof Number JavaDoc){
58             Number JavaDoc number=(Number JavaDoc)obj;
59             if(number.intValue()==number.doubleValue()) {
60                 return new Integer JavaDoc((number).intValue());
61             }
62         }
63         return No4.INSTANCE;
64     }
65
66     public static Object JavaDoc toLong(Object JavaDoc obj) {
67         if(obj instanceof Long JavaDoc){
68             return obj;
69         }
70         if(obj instanceof Number JavaDoc){
71             Number JavaDoc number=(Number JavaDoc)obj;
72             if(number.longValue()==number.doubleValue()) {
73                 return new Long JavaDoc((number).longValue());
74             }
75         }
76         return No4.INSTANCE;
77     }
78
79     public static Object JavaDoc toFloat(Object JavaDoc obj) {
80         if(obj instanceof Float JavaDoc){
81             return obj;
82         }
83         if(obj instanceof Number JavaDoc){
84             Number JavaDoc number=(Number JavaDoc)obj;
85             if(number.floatValue()==number.doubleValue()) {
86                 return new Float JavaDoc((number).floatValue());
87             }
88         }
89         return No4.INSTANCE;
90     }
91
92     public static Object JavaDoc toDouble(Object JavaDoc obj) {
93         if(obj instanceof Double JavaDoc){
94             return obj;
95         }
96         if(obj instanceof Number JavaDoc){
97             Number JavaDoc number=(Number JavaDoc)obj;
98             return new Double JavaDoc((number).doubleValue());
99         }
100         return No4.INSTANCE;
101     }
102 }
103
Popular Tags