KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > utility > NumericConverterImpl


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  * NumericConverterImpl.java
26  *
27  * Created on March 21, 2003
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.utility;
31
32 import java.math.BigDecimal JavaDoc;
33 import java.math.BigInteger JavaDoc;
34
35 /**
36  * This is a concrete implementation class for numeric conversion to BigDecimal
37  * or BigInteger. For conversion to BigInteger, we truncate the fraction
38  * part of the number.
39  *
40  * @author Shing Wai Chan
41  */

42 public class NumericConverterImpl implements NumericConverter {
43      /**
44       * @param policy for determine mechanism for conversion to BigDecimal
45       * and BigInteger
46       */

47      public NumericConverterImpl() {
48      }
49
50      /**
51       * To convert BigInteger to BigDecimal.
52       * @param bInteger the BigInteger to be converted
53       * @return converted BigDecimal
54       */

55      public BigDecimal JavaDoc toBigDecimal(BigInteger JavaDoc bInteger) {
56           return (bInteger == null) ? null : new BigDecimal JavaDoc(bInteger);
57      }
58
59      /**
60       * To convert Double to BigDecimal.
61       * @param d the Double to be converted
62       * @return converted BigDecimal
63       */

64      public BigDecimal JavaDoc toBigDecimal(Double JavaDoc d) {
65           return (d == null) ? null : new BigDecimal JavaDoc(d.toString());
66      }
67
68      /**
69       * To convert Float to BigDecimal.
70       * @param f the Float to be converted
71       * @return converted BigDecimal
72       */

73      public BigDecimal JavaDoc toBigDecimal(Float JavaDoc f) {
74           return (f == null) ? null : new BigDecimal JavaDoc(f.toString());
75      }
76
77      /**
78       * To convert Number other than BigInteger, Double and Float to BigDecimal.
79       * @param n the Number to be converted
80       * @return converted BigDecimal
81       */

82      public BigDecimal JavaDoc toBigDecimal(Number JavaDoc n) {
83           return (n == null) ? null : new BigDecimal JavaDoc(n.toString());
84      }
85
86      /**
87       * To convert BigDecimal to BigInteger.
88       * @param bDecimal the BigDecimal to be converted
89       * @return converted BigInteger
90       */

91      public BigInteger JavaDoc toBigInteger(BigDecimal JavaDoc bDecimal) {
92           return (bDecimal == null) ? null : bDecimal.toBigInteger();
93      }
94
95      /**
96       * To convert Double to BigInteger.
97       * @param d the Double to be converted
98       * @return converted BigInteger
99       */

100      public BigInteger JavaDoc toBigInteger(Double JavaDoc d) {
101           return (d == null) ? null : (new BigDecimal JavaDoc(d.toString())).toBigInteger();
102      }
103
104      /**
105       * To convert Float to BigInteger.
106       * @param f the Float to be converted
107       * @return converted BigInteger
108       */

109      public BigInteger JavaDoc toBigInteger(Float JavaDoc f) {
110           return (f == null) ? null : (new BigDecimal JavaDoc(f.toString())).toBigInteger();
111      }
112
113      /**
114       * To convert Number other than BigDecimal, Double and Float to BigInteger.
115       * @param n the Number to be converted
116       * @return converted BigInteger
117       */

118      public BigInteger JavaDoc toBigInteger(Number JavaDoc n) {
119           return (n == null) ? null : BigInteger.valueOf(n.longValue());
120      }
121 }
122
Popular Tags