001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: Currency.java,v 1.2 2006-06-15 13:47:00 sinisa Exp $
022: */
023:
024: package com.lutris.util;
025:
026: import java.math.BigDecimal;
027:
028: /**
029: * Object to store and manipulate money.
030: */
031: public class Currency extends BigDecimal {
032: /**
033: * Construct a Currency object of value zero.
034: */
035: public Currency() {
036: super (0);
037: }
038:
039: /**
040: * Construct a Currency object from a BigDecimal or Currency object.
041: * A null value result in an object containing zero.
042: */
043: public Currency(BigDecimal value) {
044: super ((value == null) ? 0.0 : value.doubleValue());
045: }
046:
047: /**
048: * Construct a Currency object from a double.
049: */
050: public Currency(double value) {
051: super (value);
052: }
053:
054: /**
055: * Construct a Currency object from a float.
056: */
057: public Currency(float value) {
058: super ((double) value);
059: }
060:
061: /**
062: * Construct a Currency object from a String.
063: */
064: public Currency(String value) {
065: this (Double.valueOf(value).doubleValue());
066: }
067:
068: /**
069: * Check if equal to a float value.
070: */
071: public boolean equals(float value) {
072: return compareTo(new Currency(value)) == 0;
073: }
074:
075: /**
076: * Check if equal to a double value.
077: */
078: public boolean equals(double value) {
079: return compareTo(new Currency(value)) == 0;
080: }
081:
082: /**
083: * Returns a Currency whose value is (this + val).
084: */
085: public Currency add(Currency val) {
086: return new Currency(super .add(val));
087: }
088:
089: /**
090: * Returns a Currency whose value is (this - val).
091: */
092: public Currency subtract(Currency val) {
093: return new Currency(super .subtract(val));
094: }
095:
096: /**
097: * Returns a Currency whose value is (this * val)
098: */
099: public Currency multiply(Currency val) {
100: return new Currency(super .multiply(val));
101: }
102:
103: /**
104: * Returns a Currency whose value is (this * val)
105: */
106: public Currency multiply(int val) {
107: return multiply(new Currency(val));
108: }
109:
110: /**
111: * Returns a Currency whose value is (this / val).
112: */
113: public Currency divide(Currency val) throws ArithmeticException,
114: IllegalArgumentException {
115: return new Currency(super .divide(val, 2, ROUND_HALF_UP));
116: }
117:
118: /**
119: * Returns a Currency whose value is (this / val).
120: */
121: public Currency divide(int val) throws ArithmeticException,
122: IllegalArgumentException {
123: return divide(new Currency((double) val));
124: }
125:
126: /**
127: * Returns a Currency whose value is the absolute value of this
128: * number.
129: */
130: public Currency absCurrency() {
131: return (signum() < 0 ? negateCurrency() : this );
132: }
133:
134: /**
135: * Returns a Currency whose value is -1 * this.
136: */
137: public Currency negateCurrency() {
138: return new Currency(negate());
139: }
140:
141: /**
142: * Convert to string with two decimals.
143: */
144: public String toString() {
145: if (scale() == 2) {
146: return super .toString();
147: } else {
148: return setScale(2, ROUND_HALF_UP).toString();
149: }
150: }
151: }
|