KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > cut > MonetoryAmountUserType


1 //$Id: MonetoryAmountUserType.java,v 1.2 2005/03/29 03:17:49 oneovthafew Exp $
2
package org.hibernate.test.cut;
3
4 import java.io.Serializable JavaDoc;
5 import java.math.BigDecimal JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.Currency JavaDoc;
10
11 import org.hibernate.Hibernate;
12 import org.hibernate.HibernateException;
13 import org.hibernate.engine.SessionImplementor;
14 import org.hibernate.type.Type;
15 import org.hibernate.usertype.CompositeUserType;
16
17 /**
18  * @author Gavin King
19  */

20 public class MonetoryAmountUserType implements CompositeUserType {
21
22     public String JavaDoc[] getPropertyNames() {
23         return new String JavaDoc[] { "amount", "currency" };
24     }
25
26     public Type[] getPropertyTypes() {
27         return new Type[] { Hibernate.BIG_DECIMAL, Hibernate.CURRENCY };
28     }
29
30     public Object JavaDoc getPropertyValue(Object JavaDoc component, int property) throws HibernateException {
31         MonetoryAmount ma = (MonetoryAmount) component;
32         return property==0 ? (Object JavaDoc) ma.getAmount() : (Object JavaDoc) ma.getCurrency();
33     }
34
35     public void setPropertyValue(Object JavaDoc component, int property, Object JavaDoc value)
36             throws HibernateException {
37         MonetoryAmount ma = (MonetoryAmount) component;
38         if ( property==0 ) {
39             ma.setAmount( (BigDecimal JavaDoc) value );
40         }
41         else {
42             ma.setCurrency( (Currency JavaDoc) value );
43         }
44     }
45
46     public Class JavaDoc returnedClass() {
47         return MonetoryAmount.class;
48     }
49
50     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
51         if (x==y) return true;
52         if (x==null || y==null) return false;
53         MonetoryAmount mx = (MonetoryAmount) x;
54         MonetoryAmount my = (MonetoryAmount) y;
55         return mx.getAmount().equals( my.getAmount() ) &&
56             mx.getCurrency().equals( my.getCurrency() );
57     }
58
59     public int hashCode(Object JavaDoc x) throws HibernateException {
60         return ( (MonetoryAmount) x ).getAmount().hashCode();
61     }
62
63     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner)
64             throws HibernateException, SQLException JavaDoc {
65         BigDecimal JavaDoc amt = (BigDecimal JavaDoc) Hibernate.BIG_DECIMAL.nullSafeGet( rs, names[0] );
66         Currency JavaDoc cur = (Currency JavaDoc) Hibernate.CURRENCY.nullSafeGet( rs, names[1] );
67         if (amt==null) return null;
68         return new MonetoryAmount(amt, cur);
69     }
70
71     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index,
72             SessionImplementor session) throws HibernateException, SQLException JavaDoc {
73         MonetoryAmount ma = (MonetoryAmount) value;
74         BigDecimal JavaDoc amt = ma == null ? null : ma.getAmount();
75         Currency JavaDoc cur = ma == null ? null : ma.getCurrency();
76         Hibernate.BIG_DECIMAL.nullSafeSet(st, amt, index);
77         Hibernate.CURRENCY.nullSafeSet(st, cur, index+1);
78     }
79
80     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
81         MonetoryAmount ma = (MonetoryAmount) value;
82         return new MonetoryAmount( ma.getAmount(), ma.getCurrency() );
83     }
84
85     public boolean isMutable() {
86         return true;
87     }
88
89     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session)
90             throws HibernateException {
91         return (Serializable JavaDoc) deepCopy(value);
92     }
93
94     public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor session, Object JavaDoc owner)
95             throws HibernateException {
96         return deepCopy(cached);
97     }
98
99     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, SessionImplementor session, Object JavaDoc owner)
100             throws HibernateException {
101         return deepCopy(original); //TODO: improve
102
}
103
104 }
105
Popular Tags