1 /* 2 * Copyright 2009-2013 Stephen Colebourne 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 org.joda.money; 17 18 /** 19 * Exception thrown when a monetary operation fails due to mismatched currencies. 20 * <p> 21 * For example, this exception would be thrown when trying to add a monetary 22 * value in one currency to a monetary value in a different currency. 23 * <p> 24 * This exception makes no guarantees about immutability or thread-safety. 25 */ 26 public class CurrencyMismatchException extends IllegalArgumentException { 27 28 /** Serialization lock. */ 29 private static final long serialVersionUID = 1L; 30 31 /** First currency. */ 32 private final CurrencyUnit firstCurrency; 33 /** Second currency. */ 34 private final CurrencyUnit secondCurrency; 35 36 /** 37 * Constructor. 38 * 39 * @param firstCurrency the first currency, may be null 40 * @param secondCurrency the second currency, not null 41 */ 42 public CurrencyMismatchException(CurrencyUnit firstCurrency, CurrencyUnit secondCurrency) { 43 super("Currencies differ: " + 44 (firstCurrency != null ? firstCurrency.getCode() : "null") + '/' + 45 (secondCurrency != null ? secondCurrency.getCode() : "null")); 46 this.firstCurrency = firstCurrency; 47 this.secondCurrency = secondCurrency; 48 } 49 50 //----------------------------------------------------------------------- 51 /** 52 * Gets the first currency at fault. 53 * 54 * @return the currency at fault, may be null 55 */ 56 public CurrencyUnit getFirstCurrency() { 57 return firstCurrency; 58 } 59 60 /** 61 * Gets the second currency at fault. 62 * 63 * @return the currency at fault, may be null 64 */ 65 public CurrencyUnit getSecondCurrency() { 66 return secondCurrency; 67 } 68 69 }