001/* 002 * Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * 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, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 * 016 * Contributors: Anatole Tresch - initial implementation Werner Keil - 017 * extensions and adaptions. 018 */ 019package org.javamoney.moneta; 020 021import javax.money.CurrencyUnit; 022import javax.money.MonetaryAmount; 023import javax.money.MonetaryException; 024 025/** 026 * Exception thrown when the currency of a {@link MonetaryAmount} passed to 027 * arithmetic operations on another {@link MonetaryAmount} is not compatible. 028 * <p> 029 * For example, this exception would be thrown when trying to multiply a 030 * {@link MonetaryAmount} in (ISO-4217) CHF with a a {@link MonetaryAmount} in 031 * (ISO-4217) USD. 032 * 033 * @author Werner Keil 034 * @author Anatole Tresch 035 */ 036public class CurrencyMismatchException extends MonetaryException { 037 /** 038 * serialVersionUID. 039 */ 040 private static final long serialVersionUID = 3277879391197687869L; 041 042 /** The source currrency */ 043 private CurrencyUnit source; 044 045 /** The target currrency */ 046 private CurrencyUnit target; 047 048 /** 049 * Constructor. 050 * 051 * @param source 052 * the source currency, not {@code null}. 053 * @param target 054 * the mismatching target currency, not {@code null}. 055 */ 056 public CurrencyMismatchException(CurrencyUnit source, CurrencyUnit target) { 057 super("Currency mismatch: " + source + " != " + target); 058 if (source == null || target == null) { 059 throw new IllegalArgumentException( 060 "Source or target currency may not be null."); 061 } 062 this.source = source; 063 this.target = target; 064 } 065 066 /** 067 * Access the source {@link CurrencyUnit} instance. 068 * 069 * @return the source currency, not {@code null} 070 */ 071 public CurrencyUnit getSource() { 072 return source; 073 } 074 075 /** 076 * Access the target {@link CurrencyUnit} instance. 077 * 078 * @return the target currency, not {@code null} 079 */ 080 public CurrencyUnit getTarget() { 081 return target; 082 } 083 084 /* 085 * (non-Javadoc) 086 * 087 * @see java.lang.Object#toString() 088 */ 089 @Override 090 public String toString() { 091 return "CurrencyMismatchException [source=" + source + ", target=" 092 + target + "]"; 093 } 094 095}