KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > sql > MonetaryAmount


1 package org.hibernate.test.sql;
2
3 import java.math.BigDecimal JavaDoc;
4 import java.util.Currency JavaDoc;
5 import java.io.Serializable JavaDoc;
6
7 /**
8  * Represents a monetary amount as value and currency.
9  *
10  * @author Gavin King <gavin@hibernate.org>
11  * @author Christian Bauer <christian@hibernate.org>
12  */

13 public class MonetaryAmount implements Serializable JavaDoc {
14
15     private final BigDecimal JavaDoc value;
16     private final Currency JavaDoc currency;
17
18     public MonetaryAmount(BigDecimal JavaDoc value, Currency JavaDoc currency) {
19         this.value = value;
20         this.currency = currency;
21     }
22
23     public Currency JavaDoc getCurrency() {
24         return currency;
25     }
26
27     public BigDecimal JavaDoc getValue() {
28         return value;
29     }
30
31     // ********************** Common Methods ********************** //
32

33     public boolean equals(Object JavaDoc o) {
34         if (this == o) return true;
35         if (!(o instanceof MonetaryAmount)) return false;
36
37         final MonetaryAmount monetaryAmount = (MonetaryAmount) o;
38
39         if (!currency.equals(monetaryAmount.currency)) return false;
40         if (!value.equals(monetaryAmount.value)) return false;
41
42         return true;
43     }
44
45     public int hashCode() {
46         int result;
47         result = value.hashCode();
48         result = 29 * result + currency.hashCode();
49         return result;
50     }
51
52     public String JavaDoc toString() {
53         return "Value: '" + getValue() + "', " +
54                 "Currency: '" + getCurrency() + "'";
55     }
56
57     public int compareTo(Object JavaDoc o) {
58         if (o instanceof MonetaryAmount) {
59             // TODO: This would actually require some currency conversion magic
60
return this.getValue().compareTo(((MonetaryAmount) o).getValue());
61         }
62         return 0;
63     }
64
65     // ********************** Business Methods ********************** //
66

67     public static MonetaryAmount fromString(String JavaDoc amount, String JavaDoc currencyCode) {
68         return new MonetaryAmount(new BigDecimal JavaDoc(amount),
69                                   Currency.getInstance(currencyCode));
70     }
71
72     public static MonetaryAmount convert(MonetaryAmount amount,
73                                          Currency JavaDoc toConcurrency) {
74         // TODO: This requires some conversion magic and is therefore broken
75
return new MonetaryAmount(amount.getValue(), toConcurrency);
76     }
77
78 }
79
Popular Tags