KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > query > util > type > MathType


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MathType.java
26  *
27  * Created on August 24, 2001
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.query.util.type;
31
32 import java.math.BigDecimal JavaDoc;
33 import java.math.BigInteger JavaDoc;
34
35 /**
36  * This class represents the types java.math.BigDecimal and java.math.BigInteger.
37  *
38  *
39  * @author Michael Bouschen
40  * @version 0.1
41  */

42 public class MathType
43     extends ClassType
44     implements NumberType
45 {
46     /**
47      *
48      */

49     public MathType(String JavaDoc name, Class JavaDoc clazz, int enumType, TypeTable typetab)
50     {
51         super(name, clazz, enumType, typetab);
52     }
53     
54     /**
55      * A numeric wrapper class type defines an ordering.
56      */

57     public boolean isOrderable()
58     {
59         return true;
60     }
61
62     /**
63      * Converts the specified value into a value of this numeric type.
64      * E.g. an Integer is converted into a BigDecimal, if this represents
65      * the type BigDecimal.
66      * @param value value to be converted
67      * @return converted value
68      */

69     public Number JavaDoc getValue(Number JavaDoc value)
70     {
71         Number JavaDoc ret = null;
72
73         if (value == null)
74             ret = null;
75         else if ("java.math.BigDecimal".equals(getName()))
76         {
77             if (value instanceof BigDecimal JavaDoc)
78                 ret = value;
79             else if (value instanceof BigInteger JavaDoc)
80                 ret = new BigDecimal JavaDoc((BigInteger JavaDoc)value);
81             else if (value instanceof Double JavaDoc)
82                 ret = new BigDecimal JavaDoc(((Double JavaDoc)value).toString());
83             else if (value instanceof Float JavaDoc)
84                 ret = new BigDecimal JavaDoc(((Float JavaDoc)value).toString());
85             else if (value instanceof Number JavaDoc)
86                 ret = BigDecimal.valueOf(((Number JavaDoc)value).longValue());
87         }
88         else if ("java.math.BigInteger".equals(getName()))
89         {
90             if (value instanceof BigInteger JavaDoc)
91                 ret = value;
92             else if (value instanceof Double JavaDoc)
93                 ret = (new BigDecimal JavaDoc(((Double JavaDoc)value).toString())).toBigInteger();
94             else if (value instanceof Float JavaDoc)
95                 ret = (new BigDecimal JavaDoc(((Float JavaDoc)value).toString())).toBigInteger();
96             else if (value instanceof Number JavaDoc)
97                 ret = BigInteger.valueOf(((Number JavaDoc)value).longValue());
98         }
99         
100         return ret;
101     }
102
103     /**
104      * Returns -value.
105      * @param value value to be negated
106      * @return -value
107      */

108     public Number JavaDoc negate(Number JavaDoc value)
109     {
110         Number JavaDoc ret = null;
111
112         if (value == null)
113             ret = null;
114         else if ("java.math.BigDecimal".equals(getName()))
115         {
116             if (value instanceof BigDecimal JavaDoc)
117                 ret = ((BigDecimal JavaDoc)value).negate();
118             else if (value instanceof BigInteger JavaDoc)
119                 ret = new BigDecimal JavaDoc(((BigInteger JavaDoc)value).negate());
120             else if (value instanceof Double JavaDoc)
121                 ret = (new BigDecimal JavaDoc(((Double JavaDoc)value).toString())).negate();
122             else if (value instanceof Float JavaDoc)
123                 ret = (new BigDecimal JavaDoc(((Float JavaDoc)value).toString())).negate();
124             else if (value instanceof Number JavaDoc)
125                 ret = BigDecimal.valueOf(-((Number JavaDoc)value).longValue());
126         }
127         else if ("java.math.BigInteger".equals(getName()))
128         {
129             if (value instanceof BigInteger JavaDoc)
130                 ret = ((BigInteger JavaDoc)value).negate();
131             else if (value instanceof Double JavaDoc)
132                 ret = (new BigDecimal JavaDoc(((Double JavaDoc)value).toString())).negate().toBigInteger();
133             else if (value instanceof Float JavaDoc)
134                 ret = (new BigDecimal JavaDoc(((Float JavaDoc)value).toString())).negate().toBigInteger();
135             else if (value instanceof Number JavaDoc)
136                 ret = BigInteger.valueOf(-((Number JavaDoc)value).longValue());
137         }
138         
139         return ret;
140     }
141     
142 }
143
Popular Tags