You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sc...@apache.org on 2009/02/14 01:44:27 UTC

svn commit: r744319 [1/2] - in /commons/sandbox/money/trunk/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/commons/ main/java/org/apache/commons/money/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/...

Author: scolebourne
Date: Sat Feb 14 00:44:26 2009
New Revision: 744319

URL: http://svn.apache.org/viewvc?rev=744319&view=rev
Log:
Create sandbox commons-money

Added:
    commons/sandbox/money/trunk/src/
    commons/sandbox/money/trunk/src/main/
    commons/sandbox/money/trunk/src/main/java/
    commons/sandbox/money/trunk/src/main/java/org/
    commons/sandbox/money/trunk/src/main/java/org/apache/
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java   (with props)
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java   (with props)
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java   (with props)
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java   (with props)
    commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java   (with props)
    commons/sandbox/money/trunk/src/test/
    commons/sandbox/money/trunk/src/test/java/
    commons/sandbox/money/trunk/src/test/java/org/
    commons/sandbox/money/trunk/src/test/java/org/apache/
    commons/sandbox/money/trunk/src/test/java/org/apache/commons/
    commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/
    commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java   (with props)
    commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestMoney.java   (with props)

Added: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java (added)
+++ commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,355 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Currency;
+
+/**
+ * An amount of money in a specific currency with unlimited size and precision.
+ * <p>
+ * TO DO: The idea for this class is to have a similar API to Money, but be of
+ * unlimited size, and hold an unlimited number of decimal places.
+ * This will mean replicating many of the method ideas from BigDecimal.
+ */
+public final class BigMoney {
+
+    /**
+     * The currency, not null.
+     */
+    private final Currency iCurrency;
+    /**
+     * The amount, not null.
+     */
+    private final BigDecimal iAmount;
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     *
+     * @param currency  the currency, not null
+     * @param amount  the amount of money, not null
+     * @return the new instance, never null
+     */
+    public static BigMoney of(Currency currency, BigDecimal amount) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        MoneyUtils.checkNotNull(amount, "Amount must not be null");
+        return new BigMoney(currency, amount);
+    }
+
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     *
+     * @param currency  the currency, not null
+     * @param amount  the amount of money, not null
+     * @return the new instance, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     */
+    public static BigMoney of(String currencyCode, BigDecimal amount) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return of(Currency.getInstance(currencyCode), amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     * The amount is a whole number only. Thus you can initialise the value
+     * 'USD 20', but not the value 'USD 20.32'.
+     *
+     * @param currency  the currency, not null
+     * @param amountMain  the amount of money in the main division of the currency
+     * @return the new instance, never null
+     */
+    public static BigMoney ofMajor(Currency currency, long amountMain) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        return new BigMoney(currency, new BigDecimal(amountMain));
+    }
+
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     * The amount is a whole number only. Thus you can initialise the value
+     * 'USD 20', but not the value 'USD 20.32'.
+     *
+     * @param currency  the currency, not null
+     * @param amountMain  the amount of money in the main division of the currency
+     * @return the new instance, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     */
+    public static BigMoney ofMajor(String currencyCode, long amountMain) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return ofMajor(Currency.getInstance(currencyCode), amountMain);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of the Money representing zero in the specified currency.
+     *
+     * @param currency  the currency, not null
+     * @return the instance representing zero, never null
+     */
+    public static BigMoney zero(Currency currency) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        return new BigMoney(currency, BigDecimal.ZERO);
+    }
+
+    /**
+     * Gets an instance of the Money representing zero in the specified currency.
+     *
+     * @param currencyCode  the currency code, not null
+     * @return the instance representing zero, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     */
+    public static BigMoney zero(String currencyCode) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return zero(Currency.getInstance(currencyCode));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of the Money from a string.
+     * <p>
+     * The string format is '<currencyCode> <amount>'.
+     * This matches the output from {@link #toString()}.
+     *
+     * @param moneyStr  the money string to parse, not null
+     * @return the parsed instance, never null
+     * @throws IllegalArgumentException if the string is malformed
+     */
+    public static BigMoney of(String moneyStr) {
+        MoneyUtils.checkNotNull(moneyStr, "Money must not be null");
+        if (moneyStr.length() < 5 || moneyStr.charAt(3) != ' ') {
+            throw new IllegalArgumentException("Money '" + moneyStr + "' cannot be parsed");
+        }
+        String currStr = moneyStr.substring(0, 3);
+        String amountStr = moneyStr.substring(4);
+        return of(Currency.getInstance(currStr), new BigDecimal(amountStr));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Constructor, creating a new monetary instance.
+     * 
+     * @param currency  the currency to use, not null
+     * @param amount  the amount of money, not null
+     */
+    private BigMoney(Currency currency, BigDecimal amount) {
+        iCurrency = currency;
+        iAmount = amount;
+    }
+
+    /**
+     * Resolves singletons.
+     * 
+     * @return the singleton instance
+     */
+    private Object readResolve() {
+        return BigMoney.of(iCurrency, iAmount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the currency of this instance.
+     * 
+     * @return the currency, never null
+     */
+    public Currency getCurrency() {
+        return iCurrency;
+    }
+
+    /**
+     * Gets the amount of this instance.
+     * 
+     * @return the amount, never null
+     */
+    public BigDecimal getAmount() {
+        return iAmount;
+    }
+
+    /**
+     * Gets the number of decimal places used by the currency and this monetary value.
+     * 
+     * @return the number of decimal places in use, typically 2 but could be 0, 1 and 3
+     */
+    public int getDecimalPlaces() {
+        return iCurrency.getDefaultFractionDigits();
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the amount of this instance in major units.
+     * <p>
+     * This extracts the whole number part of the monetary amount by dropping
+     * any amount beyond the decimal place, effectively rounding down.
+     * For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
+     * 
+     * @return the whole number part of the amount
+     * @throws ArithmeticException if the amount is too large for a long
+     */
+    public long getAmountMajor() {
+        return iAmount.setScale(0, RoundingMode.DOWN).longValueExact();
+    }
+
+    /**
+     * Gets the amount of this instance in minor units.
+     * <p>
+     * This return the whole of the monetary amount as a long in terms of the minor units.
+     * For example, 'EUR 2.35' will return 235, and 'BHD -1.345' will return -1345.
+     * 
+     * @return the whole number part of the amount
+     * @throws ArithmeticException if the amount is too large for a long
+     */
+    public long getAmountMinor() {
+        return iAmount.movePointRight(getDecimalPlaces()).longValueExact();
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Checks if this instance and the specified instance have the same currency.
+     * 
+     * @param money  the money to check, not null
+     * @return true if they have the same currency
+     */
+    public boolean isSameCurrency(BigMoney money) {
+        MoneyUtils.checkNotNull(money, "Money must not be null");
+        return (iCurrency.equals(money.getCurrency()));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a new instance with the specified number of decimal places.
+     * 
+     * @param currency  the currency to use, not null
+     * @param amount  the amount of money, not null
+     */
+    public BigMoney withDecimalPlaces(int decimalPlaces) {
+        return BigMoney.of(iCurrency, iAmount.negate());
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount added.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param moneyToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     * @throws MoneyException if the currencies differ
+     */
+    public BigMoney plus(BigMoney moneyToAdd) {
+        MoneyUtils.checkNotNull(moneyToAdd, "Money must not be null");
+        if (isSameCurrency(moneyToAdd) == false) {
+            throw new MoneyException("Cannot add " + moneyToAdd + " to " + this + " as the currencies differ");
+        }
+        return plus(moneyToAdd.getAmount());
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount added.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     */
+    public BigMoney plus(BigDecimal amountToAdd) {
+        MoneyUtils.checkNotNull(amountToAdd, "Amount must not be null");
+        return BigMoney.of(iCurrency, iAmount.add(amountToAdd));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in major units added.
+     * <p>
+     * This adds an amount in major units, leaving the minor units untouched.
+     * For example, USD 23.45 plus 138 gives USD 161.45.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     */
+    public BigMoney plusMajor(long amountToAdd) {
+        return plus(new BigDecimal(amountToAdd));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in minor units added.
+     * <p>
+     * This adds an amount in minor units.
+     * For example, USD 23.45 plus 138 gives USD 24.83.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     */
+    public BigMoney plusMinor(long amountToAdd) {
+        return plus(BigDecimal.valueOf(amountToAdd, getDecimalPlaces()));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount subtracted.
+     * <p>
+     * No precision is lost through this method as the result has the number
+     * of decimal places necessary to express the value.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param moneyToSubtract  the monetary value to subtract, not null
+     * @return the new instance with the input amount subtracted, never null
+     */
+    public BigMoney minus(BigMoney moneyToSubtract) {
+        MoneyUtils.checkNotNull(moneyToSubtract, "Money must not be null");
+        if (isSameCurrency(moneyToSubtract) == false) {
+            throw new MoneyException("Cannot subtract " + moneyToSubtract + " from " + this + " as the currencies differ");
+        }
+        return BigMoney.of(iCurrency, iAmount.subtract(moneyToSubtract.iAmount));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount negated.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param currency  the currency to use, not null
+     * @param amount  the amount of money, not null
+     */
+    public BigMoney negated() {
+        return BigMoney.of(iCurrency, iAmount.negate());
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the monetary value as a string.
+     * 
+     * @return the monetary value, never null
+     */
+    public String toString() {
+        return iCurrency.getCurrencyCode() + " " + iAmount.toPlainString();
+    }
+
+}

Propchange: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/BigMoney.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java (added)
+++ commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+/**
+ * A unit of currency.
+ * <p>
+ * This class represents a unit of currency such as the British Pound, Euro
+ * or US Dollar.
+ * 
+ * TO DO: Determine whether JDK Currency class is sufficient or not.
+ * Trouble is that sometimes the JDK data is wrong or incomplete
+ */
+public final class CurrencyUnit {
+
+//    /**
+//     * The currency, not null.
+//     */
+//    private final String iCode;
+//    /**
+//     * The number of decimal places.
+//     */
+//    private final int iDecimalPlaces;
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets an instance of Money in the specified currency.
+//     * <p>
+//     * This allows you to create an instance with a specific currency and amount.
+//     *
+//     * @param currency  the currency, not null
+//     * @param amount  the amount of money, not null
+//     * @return the new instance, never null
+//     */
+//    public static CurrencyUnit of(String currencyCode) {
+//        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+//        return new CurrencyUnit(currencyCode);
+//    }
+//
+//    /**
+//     * Gets an instance of Money in the specified currency.
+//     * <p>
+//     * This allows you to create an instance with a specific currency and amount.
+//     *
+//     * @param currency  the currency, not null
+//     * @param amount  the amount of money, not null
+//     * @return the new instance, never null
+//     * @throws IllegalArgumentException if the currency is unknown
+//     */
+//    public static CurrencyUnit of(String currencyCode, BigDecimal amount) {
+//        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+//        return of(Currency.getInstance(currencyCode), amount);
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets an instance of Money in the specified currency.
+//     * <p>
+//     * This allows you to create an instance with a specific currency and amount.
+//     * The amount is a whole number only. Thus you can initialise the value
+//     * 'USD 20', but not the value 'USD 20.32'.
+//     *
+//     * @param currency  the currency, not null
+//     * @param amountMain  the amount of money in the main division of the currency
+//     * @return the new instance, never null
+//     */
+//    public static CurrencyUnit of(Currency currency, long amountMain) {
+//        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+//        return new CurrencyUnit(currency, new BigDecimal(amountMain));
+//    }
+//
+//    /**
+//     * Gets an instance of Money in the specified currency.
+//     * <p>
+//     * This allows you to create an instance with a specific currency and amount.
+//     * The amount is a whole number only. Thus you can initialise the value
+//     * 'USD 20', but not the value 'USD 20.32'.
+//     *
+//     * @param currency  the currency, not null
+//     * @param amountMain  the amount of money in the main division of the currency
+//     * @return the new instance, never null
+//     * @throws IllegalArgumentException if the currency is unknown
+//     */
+//    public static CurrencyUnit of(String currencyCode, long amountMain) {
+//        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+//        return of(Currency.getInstance(currencyCode), amountMain);
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets an instance of the Money representing zero in the specified currency.
+//     *
+//     * @param currency  the currency, not null
+//     * @return the instance representing zero, never null
+//     */
+//    public static CurrencyUnit zero(Currency currency) {
+//        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+//        return new CurrencyUnit(currency, BigDecimal.ZERO);
+//    }
+//
+//    /**
+//     * Gets an instance of the Money representing zero in the specified currency.
+//     *
+//     * @param currencyCode  the currency code, not null
+//     * @return the instance representing zero, never null
+//     * @throws IllegalArgumentException if the currency is unknown
+//     */
+//    public static CurrencyUnit zero(String currencyCode) {
+//        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+//        return zero(Currency.getInstance(currencyCode));
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets an instance of the Money from a string.
+//     * <p>
+//     * The string format is '<currencyCode> <amount>'.
+//     * This matches the output from {@link #toString()}.
+//     *
+//     * @param moneyStr  the money string to parse, not null
+//     * @return the parsed instance, never null
+//     * @throws IllegalArgumentException if the string is malformed
+//     */
+//    public static CurrencyUnit of(String moneyStr) {
+//        MoneyUtils.checkNotNull(moneyStr, "Money must not be null");
+//        if (moneyStr.length() < 5 || moneyStr.charAt(3) != ' ') {
+//            throw new IllegalArgumentException("Money '" + moneyStr + "' cannot be parsed");
+//        }
+//        String currStr = moneyStr.substring(0, 3);
+//        String amountStr = moneyStr.substring(4);
+//        return of(Currency.getInstance(currStr), new BigDecimal(amountStr));
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Constructor, creating a new monetary instance.
+//     * 
+//     * @param currency  the currency to use, not null
+//     * @param amount  the amount of money, not null
+//     */
+//    private CurrencyUnit(Currency currency, BigDecimal amount) {
+//        iCurrency = currency;
+//        iAmount = amount;
+//    }
+//
+//    /**
+//     * Resolves singletons.
+//     * 
+//     * @return the singleton instance
+//     */
+//    private Object readResolve() {
+//        return CurrencyUnit.ofMajor(iCurrency, iAmount);
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets the currency of this instance.
+//     * 
+//     * @return the currency, never null
+//     */
+//    public Currency getCurrency() {
+//        return iCurrency;
+//    }
+//
+//    /**
+//     * Gets the amount of this instance.
+//     * 
+//     * @return the amount, never null
+//     */
+//    public BigDecimal getAmount() {
+//        return iAmount;
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets the whole amount of this instance, rounding down.
+//     * <p>
+//     * This extracts the whole number part of the monetary amount.
+//     * For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
+//     * 
+//     * @return the whole number part of the amount
+//     * @throws ArithmeticException if the amount is too large
+//     */
+//    public long getAmountWhole() {
+//        return iAmount.setScale(0, RoundingMode.DOWN).longValueExact();
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Checks if this instance and the specified instance have the same currency.
+//     * 
+//     * @param money  the money to check, not null
+//     * @return true if they have the same currency
+//     */
+//    private boolean isSameCurrency(CurrencyUnit money) {
+//        MoneyUtils.checkNotNull(money, "Money must not be null");
+//        return (iCurrency.equals(money.getCurrency()));
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Returns a new instance with the specified number of decimal places.
+//     * 
+//     * @param currency  the currency to use, not null
+//     * @param amount  the amount of money, not null
+//     */
+//    public CurrencyUnit withDecimalPlaces(int decimalPlaces) {
+//        return CurrencyUnit.ofMajor(iCurrency, iAmount.negate());
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Returns a copy of this monetary value with the amount added.
+//     * <p>
+//     * No precision is lost through this method as the result has the number
+//     * of decimal places necessary to express the value.
+//     * <p>
+//     * This instance is immutable and unaffected by this method.
+//     * 
+//     * @param moneyToAdd  the monetary value to add, not null
+//     * @return the new instance with the input amount added, never null
+//     */
+//    public CurrencyUnit plus(CurrencyUnit moneyToAdd) {
+//        MoneyUtils.checkNotNull(moneyToAdd, "Money must not be null");
+//        if (isSameCurrency(moneyToAdd) == false) {
+//            throw new MoneyException("Cannot add " + moneyToAdd + " to " + this + " as the currencies differ");
+//        }
+//        return CurrencyUnit.ofMajor(iCurrency, iAmount.add(moneyToAdd.iAmount));
+//    }
+//
+//    /**
+//     * Returns a copy of this monetary value with the amount subtracted.
+//     * <p>
+//     * No precision is lost through this method as the result has the number
+//     * of decimal places necessary to express the value.
+//     * <p>
+//     * This instance is immutable and unaffected by this method.
+//     * 
+//     * @param moneyToSubtract  the monetary value to subtract, not null
+//     * @return the new instance with the input amount subtracted, never null
+//     */
+//    public CurrencyUnit minus(CurrencyUnit moneyToSubtract) {
+//        MoneyUtils.checkNotNull(moneyToSubtract, "Money must not be null");
+//        if (isSameCurrency(moneyToSubtract) == false) {
+//            throw new MoneyException("Cannot subtract " + moneyToSubtract + " from " + this + " as the currencies differ");
+//        }
+//        return CurrencyUnit.ofMajor(iCurrency, iAmount.subtract(moneyToSubtract.iAmount));
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Returns a copy of this monetary value with the amount negated.
+//     * <p>
+//     * This instance is immutable and unaffected by this method.
+//     * 
+//     * @param currency  the currency to use, not null
+//     * @param amount  the amount of money, not null
+//     */
+//    public CurrencyUnit negated() {
+//        return CurrencyUnit.ofMajor(iCurrency, iAmount.negate());
+//    }
+//
+//    //-----------------------------------------------------------------------
+//    /**
+//     * Gets the monetary value as a string.
+//     * 
+//     * @return the monetary value, never null
+//     */
+//    public String toString() {
+//        return iCurrency.getCurrencyCode() + " " + iAmount.toPlainString();
+//    }
+
+}

Propchange: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/CurrencyUnit.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java (added)
+++ commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,776 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Currency;
+
+/**
+ * An amount of money in a specific currency.
+ * <p>
+ * An instance of money holds an amount in a currency.
+ * This is a relatively simple API, designed to build upon the JDK
+ * {@link Currency} class.
+ * <p>
+ * Every currency has a certain number of decimal places.
+ * This is typically 2 (Euro, British Pound, US Dollar) but might be
+ * 0 (Japanese Yen), 1 (Vietnamese Dong) or 3 (Bahrain Dinar).
+ * <p>
+ * This class is limited to storing and manipulating the amount at the
+ * precision of the number of decimal places.
+ * <p>
+ * Money is immutable and thread-safe.
+ */
+public final class Money implements Comparable < Money > {
+
+    /**
+     * The currency, not null.
+     */
+    private final Currency iCurrency;
+    /**
+     * The amount in minor units, not null.
+     */
+    private final long iAmount;
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     *
+     * @param currency  the currency, not null
+     * @param amount  the amount of money, not null
+     * @return the new instance, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money of(Currency currency, BigDecimal amount) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        MoneyUtils.checkNotNull(amount, "Amount must not be null");
+        return ofMinor(currency, decimalToMinor(currency, amount));
+    }
+
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     *
+     * @param currency  the currency, not null
+     * @param amount  the amount of money, not null
+     * @return the new instance, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money of(String currencyCode, BigDecimal amount) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return of(Currency.getInstance(currencyCode), amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     * The amount is a whole number only. Thus you can initialise the value
+     * 'USD 20', but not the value 'USD 20.32'.
+     *
+     * @param currency  the currency, not null
+     * @param amountMajor  the amount of money in the major division of the currency
+     * @return the new instance, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money ofMajor(Currency currency, long amountMajor) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        return ofMinor(currency, majorToMinor(currency, amountMajor));
+    }
+
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount.
+     * The amount is a whole number only. Thus you can initialise the value
+     * 'USD 20', but not the value 'USD 20.32'.
+     *
+     * @param currency  the currency, not null
+     * @param amountMajor  the amount of money in the major division of the currency
+     * @return the new instance, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money ofMajor(String currencyCode, long amountMajor) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return ofMajor(Currency.getInstance(currencyCode), amountMajor);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount
+     * expressed in terms of the minor unit.
+     * For example, if constructing US Dollars, the input to this method represents cents.
+     * Note that when a currency has zero decimal places, the major and minor units are the same.
+     *
+     * @param currency  the currency, not null
+     * @param amountMinor  the amount of money in the minor division of the currency
+     * @return the new instance, never null
+     */
+    public static Money ofMinor(Currency currency, long amountMinor) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        return new Money(currency, amountMinor);
+    }
+
+    /**
+     * Gets an instance of Money in the specified currency.
+     * <p>
+     * This allows you to create an instance with a specific currency and amount
+     * expressed in terms of the minor unit.
+     * For example, if constructing US Dollars, the input to this method represents cents.
+     * Note that when a currency has zero decimal places, the major and minor units are the same.
+     *
+     * @param currency  the currency, not null
+     * @param amountMinor  the amount of money in the minor division of the currency
+     * @return the new instance, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     */
+    public static Money ofMinor(String currencyCode, long amountMinor) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return ofMinor(Currency.getInstance(currencyCode), amountMinor);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of the Money representing zero in the specified currency.
+     *
+     * @param currency  the currency, not null
+     * @return the instance representing zero, never null
+     */
+    public static Money zero(Currency currency) {
+        MoneyUtils.checkNotNull(currency, "Currency must not be null");
+        return new Money(currency, 0);
+    }
+
+    /**
+     * Gets an instance of the Money representing zero in the specified currency.
+     *
+     * @param currencyCode  the currency code, not null
+     * @return the instance representing zero, never null
+     * @throws IllegalArgumentException if the currency is unknown
+     */
+    public static Money zero(String currencyCode) {
+        MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
+        return zero(Currency.getInstance(currencyCode));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of the Money from a string.
+     * <p>
+     * The string format is '<currencyCode> <amount>'.
+     * This matches the output from {@link #toString()}.
+     *
+     * @param moneyStr  the money string to parse, not null
+     * @return the parsed instance, never null
+     * @throws IllegalArgumentException if the string is malformed
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money of(String moneyStr) {
+        MoneyUtils.checkNotNull(moneyStr, "Money must not be null");
+        if (moneyStr.length() < 5 || moneyStr.charAt(3) != ' ') {
+            throw new IllegalArgumentException("Money '" + moneyStr + "' cannot be parsed");
+        }
+        String currStr = moneyStr.substring(0, 3);
+        String amountStr = moneyStr.substring(4);
+        return of(Currency.getInstance(currStr), new BigDecimal(amountStr));
+    }
+
+    /**
+     * Converts and amount in decimal to an amount in minor units safely.
+     * 
+     * @param currency  the currency, validated not null
+     * @param amount  the amount to convert, validated not null
+     * @return the converted amount
+     */
+    private static long decimalToMinor(Currency currency, BigDecimal amount) {
+        return amount.movePointRight(currency.getDefaultFractionDigits()).longValueExact();
+    }
+
+    /**
+     * Converts and amount in major units to an amount in minor units safely.
+     * 
+     * @param currency  the currency, validated not null
+     * @param amountMajor  the amount to convert
+     * @return the converted amount
+     */
+    private static long majorToMinor(Currency currency, long amountMajor) {
+        long mult = factor(currency.getDefaultFractionDigits());
+        long result = amountMajor * mult;
+        if (result / mult != amountMajor) {
+            throw new ArithmeticException("Monetary value is too large: " + currency.getCurrencyCode() + " " + amountMajor);
+        }
+        return result;
+    }
+
+    /**
+     * Gets the factors to divide or multiply by between major and minor units.
+     * 
+     * @param dp  the decimal places
+     * @return the factor
+     */
+    private static int factor(int dp) {
+        return (dp == 3 ? 1000 : (dp == 2 ? 100 : (dp == 1 ? 10 : 1)));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Constructor, creating a new monetary instance.
+     * 
+     * @param currency  the currency to use, not null
+     * @param amount  the amount of money, not null
+     */
+    private Money(Currency currency, long amount) {
+        iCurrency = currency;
+        iAmount = amount;
+    }
+
+    /**
+     * Resolves singletons.
+     * 
+     * @return the singleton instance
+     */
+    private Object readResolve() {
+        return Money.ofMinor(iCurrency, iAmount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the currency of this instance.
+     * 
+     * @return the currency, never null
+     */
+    public Currency getCurrency() {
+        return iCurrency;
+    }
+
+    /**
+     * Gets the amount of this instance.
+     * 
+     * @return the amount, never null
+     */
+    public BigDecimal getAmount() {
+        return BigDecimal.valueOf(iAmount, getDecimalPlaces());
+    }
+
+    /**
+     * Gets the amount of this instance in major units.
+     * <p>
+     * This extracts the whole number part of the monetary amount by dropping
+     * any amount beyond the decimal place, effectively rounding down.
+     * For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
+     * 
+     * @return the whole number part of the amount
+     */
+    public long getAmountMajor() {
+        long mult = factor(getDecimalPlaces());
+        return iAmount / mult;
+    }
+
+    /**
+     * Gets the amount of this instance in minor units.
+     * <p>
+     * This return the whole of the monetary amount as a long in terms of the minor units.
+     * For example, 'EUR 2.35' will return 235, and 'BHD -1.345' will return -1345.
+     * 
+     * @return the whole number part of the amount
+     */
+    public long getAmountMinor() {
+        return iAmount;
+    }
+
+    /**
+     * Gets the number of decimal places used by the currency and this monetary value.
+     * 
+     * @return the number of decimal places in use, typically 2 but could be 0, 1 and 3
+     */
+    public int getDecimalPlaces() {
+        return iCurrency.getDefaultFractionDigits();
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Checks if the amount is zero.
+     * 
+     * @return true if the amount is zero
+     */
+    public boolean isZero() {
+        return iAmount == 0;
+    }
+
+    /**
+     * Checks if the amount is greater than zero.
+     * 
+     * @return true if the amount is greater than zero
+     */
+    public boolean isPositive() {
+        return iAmount > 0;
+    }
+
+    /**
+     * Checks if the amount is zero or greater.
+     * 
+     * @return true if the amount is zero or greater
+     */
+    public boolean isPositiveOrZero() {
+        return iAmount >= 0;
+    }
+
+    /**
+     * Checks if the amount is less than zero.
+     * 
+     * @return true if the amount is less than zero
+     */
+    public boolean isNegative() {
+        return iAmount < 0;
+    }
+
+    /**
+     * Checks if the amount is zero or less.
+     * 
+     * @return true if the amount is zero or less
+     */
+    public boolean isNegativeOrZero() {
+        return iAmount <= 0;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Checks if this instance and the specified instance have the same currency.
+     * 
+     * @param money  the money to check, not null
+     * @return true if they have the same currency
+     */
+    public boolean isSameCurrency(Money money) {
+        MoneyUtils.checkNotNull(money, "Money must not be null");
+        return (iCurrency.equals(money.getCurrency()));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount added.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param moneyToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     * @throws MoneyException if the currencies differ
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money plus(Money moneyToAdd) {
+        MoneyUtils.checkNotNull(moneyToAdd, "Money must not be null");
+        if (isSameCurrency(moneyToAdd) == false) {
+            throw new MoneyException("Cannot add " + moneyToAdd + " to " + this + " as the currencies differ");
+        }
+        return plusMinor(moneyToAdd.getAmountMinor());
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount added.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money plus(BigDecimal amountToAdd) {
+        MoneyUtils.checkNotNull(amountToAdd, "Amount must not be null");
+        return plusMinor(decimalToMinor(iCurrency, amountToAdd));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in major units added.
+     * <p>
+     * This adds an amount in major units, leaving the minor units untouched.
+     * For example, USD 23.45 plus 138 gives USD 161.45.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money plusMajor(long amountToAdd) {
+        return plusMinor(majorToMinor(iCurrency, amountToAdd));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in minor units added.
+     * <p>
+     * This adds an amount in minor units.
+     * For example, USD 23.45 plus 138 gives USD 24.83.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToAdd  the monetary value to add, not null
+     * @return the new instance with the input amount added, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money plusMinor(long amountToAdd) {
+        if (amountToAdd == 0) {
+            return this;
+        }
+        long amountMinor = iAmount + amountToAdd;
+        if ((iAmount ^ amountMinor) < 0 && (iAmount ^ amountToAdd) >= 0) {
+            throw new ArithmeticException("Monetary value is too large");
+        }
+        return Money.ofMinor(iCurrency, amountMinor);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount subtracted.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param moneyToSubtract  the monetary value to subtract, not null
+     * @return the new instance with the input amount subtracted, never null
+     * @throws MoneyException if the currencies differ
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money minus(Money moneyToSubtract) {
+        MoneyUtils.checkNotNull(moneyToSubtract, "Money must not be null");
+        if (isSameCurrency(moneyToSubtract) == false) {
+            throw new MoneyException("Cannot subtract " + moneyToSubtract + " from " + this + " as the currencies differ");
+        }
+        return minusMinor(moneyToSubtract.getAmountMinor());
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount subtracted.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToSubtract  the monetary value to subtract, not null
+     * @return the new instance with the input amount subtracted, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money minus(BigDecimal amountToSubtract) {
+        MoneyUtils.checkNotNull(amountToSubtract, "Amount must not be null");
+        return minusMinor(decimalToMinor(iCurrency, amountToSubtract));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in major units subtracted.
+     * <p>
+     * This subtracts an amount in major units, leaving the minor units untouched.
+     * For example, USD 23.45 minus 138 gives USD -114.55.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToSubtract  the monetary value to subtract, not null
+     * @return the new instance with the input amount subtracted, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money minusMajor(long amountToSubtract) {
+        return minusMinor(majorToMinor(iCurrency, amountToSubtract));
+    }
+
+    /**
+     * Returns a copy of this monetary value with the amount in minor units subtracted.
+     * <p>
+     * This subtracts an amount in minor units.
+     * For example, USD 23.45 minus 138 gives USD 22.07.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param amountToSubtract  the monetary value to subtract, not null
+     * @return the new instance with the input amount subtracted, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money minusMinor(long amountToSubtract) {
+        if (amountToSubtract == 0) {
+            return this;
+        }
+        long amountMinor = iAmount - amountToSubtract;
+        if ((iAmount ^ amountMinor) < 0 && (iAmount ^ amountToSubtract) < 0) {
+            throw new ArithmeticException("Monetary value is too large");
+        }
+        return Money.ofMinor(iCurrency, amountMinor);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value multiplied by the specified value
+     * rounding down if necessary.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToMultiplyBy  the scalar value to multiply by, not null
+     * @return the new multiplied instance, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money multipliedBy(BigDecimal valueToMultiplyBy) {
+        return multipliedBy(valueToMultiplyBy, RoundingMode.DOWN);
+    }
+
+    /**
+     * Returns a copy of this monetary value multiplied by the specified value
+     * using the specified rounding mode to adjust the decimal places in the result.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToMultiplyBy  the scalar value to multiply by, not null
+     * @param roundingMode  the rounding mode to use to bring the decimal places back in line, not null
+     * @return the new multiplied instance, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money multipliedBy(BigDecimal valueToMultiplyBy, RoundingMode roundingMode) {
+        MoneyUtils.checkNotNull(valueToMultiplyBy, "Amount must not be null");
+        MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null");
+        BigDecimal amount = BigDecimal.valueOf(iAmount, getDecimalPlaces());
+        amount = amount.multiply(valueToMultiplyBy);
+        amount = amount.setScale(getDecimalPlaces(), roundingMode);
+        return Money.of(iCurrency, amount);
+    }
+
+    /**
+     * Returns a copy of this monetary value multiplied by the specified value.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToMultiplyBy  the scalar value to multiply by, not null
+     * @return the new multiplied instance, never null
+     * @throws ArithmeticException if the amount is too large
+     */
+    public Money multipliedBy(long valueToMultiplyBy) {
+        if (valueToMultiplyBy == 1) {
+            return this;
+        }
+        long total = iAmount * valueToMultiplyBy;
+        if (total / valueToMultiplyBy != iAmount) {
+            throw new ArithmeticException("Monetary value is too large");
+        }
+        return Money.ofMinor(iCurrency, total);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value divided by the specified value
+     * rounding down if necessary.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToDivideBy  the scalar value to multiply by, not null
+     * @return the new divided instance, never null
+     * @throws ArithmeticException if dividing by zero
+     */
+    public Money dividedBy(BigDecimal valueToDivideBy) {
+        return dividedBy(valueToDivideBy, RoundingMode.DOWN);
+    }
+
+    /**
+     * Returns a copy of this monetary value divided by the specified value
+     * using the specified rounding mode to adjust the decimal places in the result.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToDivideBy  the scalar value to divide by, not null
+     * @param roundingMode  the rounding mode to use, not null
+     * @return the new divided instance, never null
+     * @throws ArithmeticException if dividing by zero
+     * @throws ArithmeticException if the rounding fails
+     */
+    public Money dividedBy(BigDecimal valueToDivideBy, RoundingMode roundingMode) {
+        MoneyUtils.checkNotNull(valueToDivideBy, "Amount must not be null");
+        MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null");
+        BigDecimal amount = BigDecimal.valueOf(iAmount, getDecimalPlaces());
+        amount = amount.divide(valueToDivideBy, roundingMode);
+        return Money.of(iCurrency, amount);
+    }
+
+    /**
+     * Returns a copy of this monetary value divided by the specified value
+     * rounding down if necessary.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @param valueToDivideBy  the scalar value to divide by, not null
+     * @return the new divided instance, never null
+     * @throws ArithmeticException if dividing by zero
+     */
+    public Money dividedBy(long valueToDivideBy) {
+        if (valueToDivideBy == 1) {
+            return this;
+        }
+        return Money.ofMinor(iCurrency, iAmount / valueToDivideBy);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns a copy of this monetary value with the amount negated.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @return the new instance with the amount negated, never null
+     */
+    public Money negated() {
+        if (iAmount == Long.MIN_VALUE) {
+            throw new ArithmeticException("Monetary value is too large");
+        }
+        return Money.ofMinor(iCurrency, -iAmount);
+    }
+
+    /**
+     * Returns a copy of this monetary value with a positive amount.
+     * <p>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @return the new instance with the amount converted to be positive, never null
+     */
+    public Money abs() {
+        return (iAmount < 0 ? negated() : this);
+    }
+
+    /**
+     * Returns a copy of this monetary value rounded to the specified scale.
+     * <p>
+     * Scale is described in {@link BigDecimal} and represents the point below which
+     * the monetary value is zero.
+     * Negative rounding modes round increasingly large numbers.
+     * <ul>
+     * <li>Rounding EUR 45.23 to a scale of 2 has no effect (it already has that scale).
+     * <li>Rounding EUR 45.23 to a scale of 1 returns 45.20 or 45.30 depending on the rounding mode.
+     * <li>Rounding EUR 45.23 to a scale of 0 returns 45.00 or 46.00 depending on the rounding mode.
+     * <li>Rounding EUR 45.23 to a scale of -1 returns 40.00 or 50.00 depending on the rounding mode.
+     * </ul>
+     * This instance is immutable and unaffected by this method.
+     * 
+     * @return the new instance with the amount converted to be positive, never null
+     */
+    public Money rounded(int scale, RoundingMode roundingMode) {
+        MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null");
+        if (scale == getDecimalPlaces()) {
+            return this;
+        }
+        if (scale > getDecimalPlaces()) {
+            throw new MoneyException("Scale cannot be less than the number of allowed decimal places");
+        }
+        BigDecimal amount = BigDecimal.valueOf(iAmount, getDecimalPlaces());
+        amount = amount.setScale(scale, roundingMode);
+        return Money.of(iCurrency, amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Compares this monetary value to another.
+     * The compared values must be in the same currency.
+     * 
+     * @param other  the other monetary value, not null
+     * @return -1 if this is less than , 0 if equal, 1 if greater than
+     */
+    public int compareTo(Money other) {
+        if (isSameCurrency(other) == false) {
+            throw new MoneyException("Cannot compare " + other + " to " + this + " as the currencies differ");
+        }
+        if (iAmount < other.getAmountMinor()) {
+            return -1;
+        }
+        if (iAmount > other.getAmountMinor()) {
+            return 1;
+        }
+        return 0;
+    }
+
+    /**
+     * Checks if this monetary value is greater than another.
+     * The compared values must be in the same currency.
+     * 
+     * @param other  the other monetary value, not null
+     * @return true is this is greater than the specified monetary value
+     */
+    public boolean isGreaterThan(Money other) {
+        return compareTo(other) > 0;
+    }
+
+    /**
+     * Checks if this monetary value is less than another.
+     * The compared values must be in the same currency.
+     * 
+     * @param other  the other monetary value, not null
+     * @return true is this is less than the specified monetary value
+     */
+    public boolean isLessThan(Money other) {
+        return compareTo(other) < 0;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the monetary value as a string.
+     * 
+     * @return true if this instance equals the other instance
+     */
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (other instanceof Money) {
+            Money otherMoney = (Money) other;
+            return iCurrency.equals(otherMoney.getCurrency()) &&
+                    iAmount == otherMoney.getAmountMinor();
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code for this instance.
+     * 
+     * @return a suitable hash code
+     */
+    @Override
+    public int hashCode() {
+        return iCurrency.hashCode() ^ ((int) (iAmount ^ (iAmount >>> 32)));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the monetary value as a string.
+     * 
+     * @return the monetary value, never null
+     */
+    @Override
+    public String toString() {
+        StringBuilder buf = new StringBuilder();
+        long factor = factor(getDecimalPlaces());
+        buf.append(iCurrency.getCurrencyCode()).append(' ');
+        long amount = iAmount;
+        long minor;
+        if (amount < 0) {
+            buf.append('-').append(-(amount / factor));
+            minor = -(amount % factor);
+        } else {
+            buf.append(amount / factor);
+            minor = amount % factor;
+        }
+        if (getDecimalPlaces() > 0) {
+            int index = buf.length();
+            buf.append(minor + factor).setCharAt(index, '.');
+        }
+        return buf.toString();
+    }
+
+}

Propchange: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/Money.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java (added)
+++ commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+/**
+ * Exception thrown when a monetary calculation fails.
+ */
+public class MoneyException extends IllegalArgumentException {
+
+    /** Serialization lock. */
+    private static final long serialVersionUID = 735871535128L;
+
+    /**
+     * Constructor.
+     * 
+     * @param message  the message
+     */
+    public MoneyException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param message  the message
+     * @param cause  the exception cause
+     */
+    public MoneyException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+}

Propchange: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java (added)
+++ commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+/**
+ * Utilities for working with monetary values.
+ */
+public final class MoneyUtils {
+
+    /**
+     * Private constructor.
+     */
+    private MoneyUtils() {
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Gets an instance of the Money from a string.
+     * <p>
+     * The string format is 
+     *
+     * @param moneyStr  the money string to parse, not null
+     * @return the parsed instance, never null
+     * @throws NullPointerException if the input value is null
+     */
+    static void checkNotNull(Object object, String message) {
+        if (object == null) {
+            throw new NullPointerException(message);
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Checks if the money is zero, treating null as zero
+     * 
+     * @return true if the money is null or zero
+     */
+    public static boolean isZero(Money money) {
+        return (money == null || money.isZero());
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Finds the maximum monetary value.
+     * 
+     * @param money1  the first money instance, not null
+     * @param money2  the first money instance, not null
+     * @return the maximum value, never null
+     * @throws MoneyException if the currencies differ
+     */
+    public static Money max(Money money1, Money money2) {
+        checkNotNull(money1, "Money must not be null");
+        checkNotNull(money2, "Money must not be null");
+        if (money1.isSameCurrency(money2) == false) {
+            throw new MoneyException("Cannot find maximum of " + money1 + " and " + money2 + " as the currencies differ");
+        }
+        return (money1.getAmountMinor() >= money2.getAmountMinor() ? money1 : money2);
+    }
+
+    /**
+     * Finds the minimum monetary value.
+     * 
+     * @param money1  the first money instance, not null
+     * @param money2  the first money instance, not null
+     * @return the minimum value, never null
+     * @throws MoneyException if the currencies differ
+     */
+    public static Money min(Money money1, Money money2) {
+        checkNotNull(money1, "Money must not be null");
+        checkNotNull(money2, "Money must not be null");
+        if (money1.isSameCurrency(money2) == false) {
+            throw new MoneyException("Cannot find minimum of " + money1 + " and " + money2 + " as the currencies differ");
+        }
+        return (money1.getAmountMinor() <= money2.getAmountMinor() ? money1 : money2);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Adds two money objects, handling null.
+     * 
+     * @param money1  the first money instance, null returns money2
+     * @param money2  the first money instance, null returns money1
+     * @return the total, where null is ignored, null if both inputs are null
+     * @throws MoneyException if the currencies differ
+     * @throws ArithmeticException if the amount is too large
+     */
+    public static Money add(Money money1, Money money2) {
+        if (money1 != null) {
+            if (money2 != null) {
+                return money1.plus(money2);
+            } else {
+                return money1;
+            }
+        } else if (money2 != null) {
+            return money2;
+        } else {
+            return null;
+        }
+    }
+
+}

Propchange: commons/sandbox/money/trunk/src/main/java/org/apache/commons/money/MoneyUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java
URL: http://svn.apache.org/viewvc/commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java?rev=744319&view=auto
==============================================================================
--- commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java (added)
+++ commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java Sat Feb 14 00:44:26 2009
@@ -0,0 +1,256 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.money;
+
+import static org.testng.Assert.assertEquals;
+
+import java.math.BigDecimal;
+import java.util.Currency;
+
+import org.testng.annotations.Test;
+
+/**
+ * Test Money.
+ */
+@Test
+public class TestBigMoney {
+
+    private static final Currency GBP = Currency.getInstance("GBP");
+    private static final Currency EUR = Currency.getInstance("EUR");
+    private static final Currency XXX = Currency.getInstance("XXX");
+    private static final BigDecimal BIGDEC_2_34 = new BigDecimal("2.34");
+    private static final BigDecimal BIGDEC_M5_78 = new BigDecimal("-5.78");
+
+    //-----------------------------------------------------------------------
+    // money(Currency,BigDecimal)
+    //-----------------------------------------------------------------------
+    public void test_factory_money_Currency_BigDecimal() {
+        BigMoney test = BigMoney.of(GBP, BIGDEC_2_34);
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BIGDEC_2_34);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_money_Currency_BigDecimal_nullCurrency() {
+        BigMoney.of((Currency) null, BIGDEC_2_34);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_money_Currency_BigDecimal_nullBigDecimal() {
+        BigMoney.of(GBP, (BigDecimal) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // money(String,BigDecimal)
+    //-----------------------------------------------------------------------
+    public void test_factory_money_String_BigDecimal() {
+        BigMoney test = BigMoney.of("GBP", BIGDEC_2_34);
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BIGDEC_2_34);
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_money_String_BigDecimal_badCurrency() {
+        BigMoney.of("GBX", BIGDEC_2_34);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_money_String_BigDecimal_nullCurrency() {
+        BigMoney.of((String) null, BIGDEC_2_34);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_money_String_BigDecimal_nullBigDecimal() {
+        BigMoney.of("GBP", (BigDecimal) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // ofMajor(Currency,long)
+    //-----------------------------------------------------------------------
+    public void test_factory_ofMajor_Currency_long() {
+        BigMoney test = BigMoney.ofMajor(GBP, 234);
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), new BigDecimal("234"));
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_ofMajor_Currency_long_nullCurrency() {
+        BigMoney.ofMajor((Currency) null, 234);
+    }
+
+    //-----------------------------------------------------------------------
+    // ofMajor(String,long)
+    //-----------------------------------------------------------------------
+    public void test_factory_ofMajor_String_long() {
+        BigMoney test = BigMoney.ofMajor("GBP", 234);
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), new BigDecimal("234"));
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_ofMajor_String_long_badCurrency() {
+        BigMoney.ofMajor("GBX", 234);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_ofMajor_String_long_nullCurrency() {
+        BigMoney.ofMajor((String) null, 234);
+    }
+
+    //-----------------------------------------------------------------------
+    // zero(Currency)
+    //-----------------------------------------------------------------------
+    public void test_factory_zero_Currency() {
+        BigMoney test = BigMoney.zero(GBP);
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BigDecimal.ZERO);
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_zero_Currency_nullCurrency() {
+        BigMoney.zero((Currency) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // zero(String)
+    //-----------------------------------------------------------------------
+    public void test_factory_zero_String() {
+        BigMoney test = BigMoney.zero("GBP");
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BigDecimal.ZERO);
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_zero_String_badCurrency() {
+        BigMoney.zero("GBX");
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_zero_String_nullString() {
+        BigMoney.zero((String) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // valueOf(String)
+    //-----------------------------------------------------------------------
+    public void test_factory_valueOf_String_positive() {
+        BigMoney test = BigMoney.of("GBP 2.34");
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BIGDEC_2_34);
+    }
+
+    public void test_factory_valueOf_String_negative() {
+        BigMoney test = BigMoney.of("GBP -5.78");
+        assertEquals(test.getCurrency(), GBP);
+        assertEquals(test.getAmount(), BIGDEC_M5_78);
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_valueOf_String_tooShort() {
+        BigMoney.of("GBP ");
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_valueOf_String_noSpace() {
+        BigMoney.of("GBP2.34");
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void test_factory_valueOf_String_badCurrency() {
+        BigMoney.of("GBX 2.34");
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_factory_valueOf_String_nullString() {
+        BigMoney.of((String) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // getAmountMajor()
+    //-----------------------------------------------------------------------
+    public void test_getAmountMajor_positive() {
+        BigMoney test = BigMoney.of("GBP 2.34");
+        assertEquals(test.getAmountMajor(), 2);
+    }
+
+    public void test_getAmountMajor_negative() {
+        BigMoney test = BigMoney.of("GBP -5.78");
+        assertEquals(test.getAmountMajor(), -5);
+    }
+
+    @Test(expectedExceptions = ArithmeticException.class)
+    public void test_getAmountMajor_tooBig() {
+        BigMoney test = BigMoney.of("GBP 12345678912345678912");
+        test.getAmountMajor();
+    }
+
+    //-----------------------------------------------------------------------
+    // getAmountMinor()
+    //-----------------------------------------------------------------------
+    public void test_getAmountMinor_positive() {
+        BigMoney test = BigMoney.of("GBP 2.34");
+        assertEquals(test.getAmountMinor(), 234);
+    }
+
+    public void test_getAmountMinor_negative() {
+        BigMoney test = BigMoney.of("GBP -5.78");
+        assertEquals(test.getAmountMinor(), -578);
+    }
+
+    @Test(expectedExceptions = ArithmeticException.class)
+    public void test_getAmountMinor_tooBig() {
+        BigMoney test = BigMoney.of("GBP 123456789123456789");
+        test.getAmountMinor();
+    }
+
+    //-----------------------------------------------------------------------
+    // plus(BigMoney2)
+    //-----------------------------------------------------------------------
+    public void test_plus_BigMoney2_positive() {
+        BigMoney test = BigMoney.of("GBP 2.34").plus(BigMoney.of("GBP 1.23"));
+        assertEquals(test.toString(), "GBP 3.57");
+    }
+
+    public void test_plus_BigMoney2_negative() {
+        BigMoney test = BigMoney.of("GBP 2.34").plus(BigMoney.of("GBP 1.23"));
+        assertEquals(test.toString(), "GBP 3.57");
+    }
+
+    @Test(expectedExceptions = MoneyException.class)
+    public void test_plus_BigMoney2_currencyMismatch() {
+        BigMoney.of("GBP -5.78").plus(BigMoney.of("USD 1.23"));
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void test_plus_BigMoney2_nullBigMoney2() {
+        BigMoney.of("GBP -5.78").plus((BigMoney) null);
+    }
+
+    //-----------------------------------------------------------------------
+    // toString()
+    //-----------------------------------------------------------------------
+    public void test_toString_positive() {
+        BigMoney test = BigMoney.of(GBP, BIGDEC_2_34);
+        assertEquals(test.toString(), "GBP 2.34");
+    }
+
+    public void test_toString_negative() {
+        BigMoney test = BigMoney.of(EUR, BIGDEC_M5_78);
+        assertEquals(test.toString(), "EUR -5.78");
+    }
+
+}

Propchange: commons/sandbox/money/trunk/src/test/java/org/apache/commons/money/TestBigMoney.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain