View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.common.model.accounting;
17  
18  import java.math.BigDecimal;
19  import java.util.Currency;
20  import java.util.Locale;
21  
22  import net.sf.oness.common.all.BaseObject;
23  
24  /***
25   * Class for Money
26   * 
27   * @see BigDecimal
28   * @see Currency
29   * 
30   * @author Carlos Sanchez
31   * @version $Revision: 1.1 $
32   */
33  public class Money extends BaseObject implements Comparable {
34  
35      private BigDecimal amount;
36      private Currency currency;
37  
38      /***
39       * Create a money object with an amount of 0 and the currency
40       * of the default locale as returned by <code>Locale.getDefault()</code>
41       * 
42       * @see Locale#getDefault()
43       */
44      public Money() {
45          this.amount = new BigDecimal(0);
46          this.currency = Currency.getInstance(Locale.getDefault());
47      }
48  
49      /***
50       * Create a money object with the specified amount and currency
51       * 
52       * @param amount
53       * @param currency
54       */
55      public Money(BigDecimal amount, Currency currency) {
56          this.amount = amount;
57          this.currency = currency;
58      }
59  
60      /***
61       * Create a money object with an amount of 0 and the specified 
62       * currency
63       * 
64       * @param currency
65       */
66      public Money(Currency currency) {
67          this.amount = new BigDecimal(0);
68          this.currency = currency;
69      }
70  
71      public BigDecimal getAmount() {
72          return amount;
73      }
74  
75      public Currency getCurrency() {
76          return currency;
77      }
78  
79      /***
80       * 
81       * @param arg
82       * @return
83       * @throws IllegalArgumentException if the currencies are not equal
84       * @see BigDecimal#add(BigDecimal)
85       */
86      public Money add(Money arg) throws IllegalArgumentException {
87          checkCurrency(arg);
88          return new Money(amount.add(arg.amount), currency);
89      }
90  
91      /***
92       * 
93       * @param arg
94       * @return
95       * @throws IllegalArgumentException if the currencies are not equal
96       * @see BigDecimal#subtract(BigDecimal)
97       */
98      public Money subtract(Money arg) throws IllegalArgumentException {
99          checkCurrency(arg);
100         return this.add(arg.negate());
101     }
102 
103     /***
104      * 
105      * @return
106      * @throws IllegalArgumentException if the currencies are not equal
107      */
108     public Money negate() {
109         return new Money(amount.negate(), currency);
110     }
111 
112     public Money multiply(long arg) {
113         return new Money(amount.multiply(BigDecimal.valueOf(arg)), currency);
114     }
115 
116     /***
117      * @throws IllegalArgumentException if the currencies are not equal
118      * @see java.lang.Comparable#compareTo(java.lang.Object)
119      * @see BigDecimal#compareTo(Object)
120      */
121     public int compareTo(Object arg)
122         throws IllegalArgumentException, ClassCastException {
123         Money moneyArg = (Money) arg;
124         checkCurrency(moneyArg);
125         return amount.compareTo(moneyArg.amount);
126     }
127 
128     /***
129      * 
130      * @param arg
131      * @return
132      * @throws IllegalArgumentException if the currencies are not equal
133      */
134     public boolean greaterThan(Money arg) {
135         return (this.compareTo(arg) > 0);
136     }
137 
138     /***
139      * 
140      * @param arg
141      * @return
142      * @throws IllegalArgumentException if the currencies are not equal
143      */
144     public boolean lessThan(Money arg) {
145         return (this.compareTo(arg) < 0);
146     }
147 
148     /***
149      * 
150      * @param arg
151      * @throws IllegalArgumentException if the currencies are not equal
152      */
153     private void checkCurrency(Money arg) throws IllegalArgumentException {
154         if (!currency.equals(arg.currency))
155             throw new IllegalArgumentException("Not the same currency");
156     }
157 
158 }