KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > BigIntegerType


1 //$Id: BigIntegerType.java,v 1.3 2005/05/20 19:54:10 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.math.BigInteger JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Types JavaDoc;
10
11 import org.hibernate.EntityMode;
12 import org.hibernate.HibernateException;
13
14 /**
15  * <tt>big_integer</tt>: A type that maps an SQL NUMERIC to a
16  * <tt>java.math.BigInteger</tt>
17  * @see java.math.BigInteger
18  * @author Gavin King
19  */

20 public class BigIntegerType extends ImmutableType implements DiscriminatorType {
21
22     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
23         return value.toString();
24     }
25
26     public Object JavaDoc stringToObject(String JavaDoc xml) throws Exception JavaDoc {
27         return new BigInteger JavaDoc(xml);
28     }
29
30     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name)
31     throws HibernateException, SQLException JavaDoc {
32         //return rs.getBigDecimal(name).toBigIntegerExact(); this 1.5 only.
33
BigDecimal JavaDoc bigDecimal = rs.getBigDecimal(name);
34         return bigDecimal==null ? null :
35             bigDecimal.setScale(0, BigDecimal.ROUND_UNNECESSARY).unscaledValue();
36     }
37
38     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
39     throws HibernateException, SQLException JavaDoc {
40         st.setBigDecimal( index, new BigDecimal JavaDoc( (BigInteger JavaDoc) value ) );
41     }
42
43     public int sqlType() {
44         return Types.NUMERIC;
45     }
46
47     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
48         return value.toString();
49     }
50
51     public Class JavaDoc getReturnedClass() {
52         return BigInteger JavaDoc.class;
53     }
54
55     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
56         return x==y || ( x!=null && y!=null && ( (BigInteger JavaDoc) x ).compareTo( (BigInteger JavaDoc) y )==0 );
57     }
58
59     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
60         return ( (BigInteger JavaDoc) x ).intValue();
61     }
62
63     public String JavaDoc getName() {
64         return "big_integer";
65     }
66
67     public Object JavaDoc fromStringValue(String JavaDoc xml) {
68         return new BigInteger JavaDoc(xml);
69     }
70
71
72 }
73
74
75
76
77
78
79
Popular Tags