001    /*
002     *  Copyright 2009-2013 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.money;
017    
018    /**
019     * Exception thrown when a monetary operation fails due to mismatched currencies.
020     * <p>
021     * For example, this exception would be thrown when trying to add a monetary
022     * value in one currency to a monetary value in a different currency.
023     * <p>
024     * This exception makes no guarantees about immutability or thread-safety.
025     */
026    public class CurrencyMismatchException extends IllegalArgumentException {
027    
028        /** Serialization lock. */
029        private static final long serialVersionUID = 1L;
030    
031        /** First currency. */
032        private final CurrencyUnit firstCurrency;
033        /** Second currency. */
034        private final CurrencyUnit secondCurrency;
035    
036        /**
037         * Constructor.
038         * 
039         * @param firstCurrency  the first currency, may be null
040         * @param secondCurrency  the second currency, not null
041         */
042        public CurrencyMismatchException(CurrencyUnit firstCurrency, CurrencyUnit secondCurrency) {
043            super("Currencies differ: " +
044                    (firstCurrency != null ? firstCurrency.getCode() : "null") + '/' +
045                    (secondCurrency != null ? secondCurrency.getCode() : "null"));
046            this.firstCurrency = firstCurrency;
047            this.secondCurrency = secondCurrency;
048        }
049    
050        //-----------------------------------------------------------------------
051        /**
052         * Gets the first currency at fault.
053         * 
054         * @return the currency at fault, may be null
055         */
056        public CurrencyUnit getFirstCurrency() {
057            return firstCurrency;
058        }
059    
060        /**
061         * Gets the second currency at fault.
062         * 
063         * @return the currency at fault, may be null
064         */
065        public CurrencyUnit getSecondCurrency() {
066            return secondCurrency;
067        }
068    
069    }