001: package net.sourceforge.jaxor.example;
002:
003: import java.io.Serializable;
004: import java.math.BigDecimal;
005:
006: /**
007: * This utility implements the Money pattern to more easily deal with
008: * fixed precision numeric values. This class can represent either
009: * valid or null values, depending on the value passed to the
010: * constructor.
011: */
012:
013: public class Money implements Comparable, Serializable {
014: public static final Money ZERO = new Money("0");
015:
016: private BigDecimal amount;
017:
018: public Money(String amount) {
019: if (amount != null)
020: this .amount = init(new BigDecimal(amount));
021: else
022: this .amount = null;
023: }
024:
025: public Money(BigDecimal amount) {
026: this .amount = init(amount);
027: }
028:
029: /**
030: * This method is necessary because parameters to the constructors
031: * can be null for NULL database values.
032: *
033: * @param amount a BigDecimal or null
034: * @return either null or an appropriately initialized BigDecimal
035: */
036:
037: private BigDecimal init(BigDecimal amount) {
038: if (amount != null)
039: return amount.setScale(2, BigDecimal.ROUND_HALF_UP);
040:
041: return null;
042: }
043:
044: public BigDecimal getAmount() {
045: return amount;
046: }
047:
048: public int compareTo(Object obj) {
049: Money other = (Money) obj;
050:
051: if (other == null || amount == null)
052: return -1;
053:
054: return amount.compareTo(other.amount);
055: }
056:
057: /**
058: * Check if money is less than and not equal to zero
059: * @return true if < zero, false if EQUAL or greater than zero
060: */
061:
062: public boolean isLessThanZero() {
063: if (compareTo(Money.ZERO) == -1)
064: return true;
065: return false;
066: }
067:
068: public Money add(Money other) {
069: if (other != null && amount != null) {
070: return new Money(amount.add(other.amount));
071: }
072: return this ;
073: }
074:
075: public Money subtract(Money other) {
076: if (other != null && amount != null) {
077: return new Money(amount.subtract(other.amount));
078: }
079: return this ;
080: }
081:
082: public Money multiply(int value) {
083: if (amount == null)
084: return this ;
085:
086: return new Money(amount.multiply(new BigDecimal(String
087: .valueOf(value))));
088: }
089:
090: public Money multiply(BigDecimal value, int roundingMode) {
091: if (amount == null || value == null)
092: return this ;
093:
094: return new Money(amount.multiply(value).setScale(
095: amount.scale(), roundingMode));
096: }
097:
098: public boolean equals(Object o) {
099: if (!(o instanceof Money))
100: return false;
101:
102: Money other = (Money) o;
103: if (amount != null)
104: return other.amount.equals(amount);
105:
106: return false;
107: }
108:
109: public int hashCode() {
110: if (amount == null)
111: return -1;
112:
113: return amount.hashCode();
114: }
115:
116: public String toString() {
117: if (amount == null)
118: return null;
119:
120: StringBuffer result = new StringBuffer("$");
121: result.append(amount.toString());
122: return result.toString();
123: }
124: }
|