package gw.lang.enhancements uses gw.api.financials.CurrencyAmount /** * The overloaded versions of the sum() method had to be moved to separate enhancements due to the way block type erasure * works (all blocks with the same arity have the same erasure). Splitting the methods up into different enhancements * prevents them from conflicting, since they become part of different Java classes. * * Copyright 2010 Guidewire Software, Inc. */ @ReadOnly enhancement GWCoreArrayCurrencyAmountSumEnhancement : T[] { /** * Sums up the values of the target of the mapper argument. * Returns null if the array is empty, since the currency cannot be determined. * Note: this behavior is different from that of other sum methods, where zero is returned. * @throws IllegalStateException if all mapped amounts are not in the same currency */ function sum( mapper(elt:T):CurrencyAmount ) : CurrencyAmount { if (not this.HasElements) { return null } var sum : CurrencyAmount = null for (elem in this) { var val = mapper(elem) sum = sum == null ? val : sum.add(val) } return sum } }