/*
 * Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil.
 * 
 * Licensed 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.javamoney.calc.function;

import java.math.BigDecimal;
import java.util.Objects;

import javax.money.CurrencyUnit;
import javax.money.MonetaryAmount;

import org.javamoney.moneta.Money;

/**
 * This class allows to calculate the total of some {@link MonetaryAmount}
 * instances.
 * 
 * @author Anatole Tresch
 */
final class Total implements
		MonetaryCalculation<Iterable<? extends MonetaryAmount>> {

	/**
	 * Private constructor, there is only one instance of this class, accessible
	 * calling {@link org.javamoney.calc.function.MonetaryCalculations#total()} .
	 */
	Total() {
	}

    /**
	 * Evaluates the total sum of the given amounts.
	 * 
	 * @param amounts
	 *            The amounts, at least one instance, not null, all of the same
	 *            currency.
	 * @return the total sum.
	 */
	public MonetaryAmount calculate(Iterable<? extends MonetaryAmount> amounts) {
		if (amounts == null) {
			throw new IllegalArgumentException("amounts required.");
		}
		CurrencyUnit unit = null;
		BigDecimal result = null;
		for (MonetaryAmount amount : amounts) {
			if (result == null) {
				result = amount.getNumber().numberValue(BigDecimal.class);
			} else {
				result = result.add(amount.getNumber().numberValue(BigDecimal.class));
			}
		}
		if (result == null) {
			throw new IllegalArgumentException("amounts is empty.");
		}
		return Money.of(result, unit);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Total [Iterable<MonetaryAmount> -> MonetaryAmount]";
	}

}

@@ -1736,16 +1736,49 @@
 null) %7B%0A
+%09%09%09%09unit = amount.getCurrency();%0A
 %09%09%09%09resu
@@ -1844,16 +1844,146 @@
  else %7B%0A
+%09%09%09%09if (unit != amount.getCurrency()) %7B%0A%09%09%09%09%09throw new IllegalArgumentException(%22amounts should be of same currency%22);%0A%09%09%09%09%7D%0A%09%09%09%09%0A
 %09%09%09%09resu

Initialize currency unit when calulating totals