1 package org.hibernate.ce.auction.model; 2 3 import java.io.Serializable ; 4 import java.math.BigDecimal ; 5 import java.util.Currency ; 6 7 13 public class MonetaryAmount implements Serializable { 14 15 private final BigDecimal value; 16 private final Currency currency; 17 18 public MonetaryAmount(BigDecimal value, Currency currency) { 19 this.value = value; 20 this.currency = currency; 21 } 22 23 public Currency getCurrency() { 24 return currency; 25 } 26 27 public BigDecimal getValue() { 28 return value; 29 } 30 31 33 public boolean equals(Object 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 toString() { 53 return "Value: '" + getValue() + "', " + 54 "Currency: '" + getCurrency() + "'"; 55 } 56 57 public int compareTo(Object o) { 58 if (o instanceof MonetaryAmount) { 59 return this.getValue().compareTo(((MonetaryAmount) o).getValue()); 61 } 62 return 0; 63 } 64 65 67 public static MonetaryAmount fromString(String amount, String currencyCode) { 68 return new MonetaryAmount(new BigDecimal (amount), 69 Currency.getInstance(currencyCode)); 70 } 71 72 public static MonetaryAmount convert(MonetaryAmount amount, 73 Currency toConcurrency) { 74 return new MonetaryAmount(amount.getValue(), toConcurrency); 76 } 77 78 } 79 | Popular Tags |