KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > entity > MonetaryAmountUserType


1 //$Id: MonetaryAmountUserType.java,v 1.1 2005/05/16 17:43:08 epbernard Exp $
2
package org.hibernate.test.annotations.entity;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.util.Currency JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11 import org.hibernate.usertype.CompositeUserType;
12 import org.hibernate.type.Type;
13 import org.hibernate.Hibernate;
14 import org.hibernate.HibernateException;
15 import org.hibernate.engine.SessionImplementor;
16
17 /**
18  * @author Emmanuel Bernard
19  */

20 public class MonetaryAmountUserType 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         MonetaryAmount ma = (MonetaryAmount) 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         MonetaryAmount ma = (MonetaryAmount) 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 MonetaryAmount.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         MonetaryAmount mx = (MonetaryAmount) x;
54         MonetaryAmount my = (MonetaryAmount) 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 ( (MonetaryAmount) 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 MonetaryAmount(amt, cur);
69     }
70
71     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index,
72             SessionImplementor session) throws HibernateException, SQLException JavaDoc {
73         MonetaryAmount ma = (MonetaryAmount) 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         MonetaryAmount ma = (MonetaryAmount) value;
82         return new MonetaryAmount( 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