Source:
public ImmutableNetPriceImpl(@NonNull PriceCurrency baseCurrency, @NonNull List<Price> prices) { this.currency = baseCurrency; this.currencyToPriceMap = new HashMap<>(); this.notExchangedPriceMap = new HashMap<>(); BigDecimal possiblyIncorrectTotalPrice = BigDecimal.ZERO; BigDecimal totalPrice = BigDecimal.ZERO; boolean areAllExchangeRatesValid = true; for (final Price price : prices) { notExchangedPriceMap.put(price.getCurrency(), notExchangedPriceMap.containsKey(price.getCurrency()) ? notExchangedPriceMap.get(price.getCurrency()).add(price.getPrice()) : price.getPrice()); final BigDecimal priceToAdd; final PriceCurrency currencyForPriceToAdd; if (price.getExchangeRate().supportsExchangeRateFor(baseCurrency)) { priceToAdd = price.getPrice().multiply(price.getExchangeRate().getExchangeRate(baseCurrency)) .setScale(Distance.RATE_PRECISION, RoundingMode.HALF_UP); totalPrice = totalPrice.add(priceToAdd); currencyForPriceToAdd = baseCurrency; } else { priceToAdd = price.getPrice().setScale(Distance.RATE_PRECISION, RoundingMode.HALF_UP); currencyForPriceToAdd = price.getCurrency(); areAllExchangeRatesValid = false; } possiblyIncorrectTotalPrice = possiblyIncorrectTotalPrice.add(priceToAdd); final BigDecimal priceForCurrency = currencyToPriceMap.containsKey(currencyForPriceToAdd) ? currencyToPriceMap.get(currencyForPriceToAdd).add(priceToAdd) : priceToAdd; currencyToPriceMap.put(currencyForPriceToAdd, priceForCurrency); } this.totalPrice = totalPrice.setScale(DEFAULT_DECIMAL_PRECISION, RoundingMode.HALF_DOWN); this.possiblyIncorrectTotalPrice = possiblyIncorrectTotalPrice.setScale(DEFAULT_DECIMAL_PRECISION, RoundingMode.HALF_DOWN); this.areAllExchangeRatesValid = areAllExchangeRatesValid; this.exchangeRate = new ExchangeRateBuilderFactory().setBaseCurrency(baseCurrency).build(); this.decimalFormattedPrice = calculateDecimalFormattedPrice(); this.currencyFormattedPrice = calculateCurrencyFormattedPrice(); this.currencyCodeFormattedPrice = calculateCurrencyCodeFormattedPrice(); }

Target:
public ImmutableNetPriceImpl(@NonNull PriceCurrency baseCurrency, @NonNull List<Price> prices) { this.currency = baseCurrency; this.currencyToPriceMap = new HashMap<>(); this.notExchangedPriceMap = new HashMap<>(); BigDecimal possiblyIncorrectTotalPrice = BigDecimal.ZERO; BigDecimal totalPrice = BigDecimal.ZERO; boolean areAllExchangeRatesValid = true; for (final Price price : prices) { notExchangedPriceMap.put(price.getCurrency(), notExchangedPriceMap.containsKey(price.getCurrency()) ? notExchangedPriceMap.get(price.getCurrency()).add(price.getPrice()) : price.getPrice()); final BigDecimal priceToAdd; final PriceCurrency currencyForPriceToAdd; if (price.getExchangeRate().supportsExchangeRateFor(baseCurrency)) { priceToAdd = price.getPrice().multiply(price.getExchangeRate().getExchangeRate(baseCurrency)) .setScale(DEFAULT_DECIMAL_PRECISION, RoundingMode.HALF_UP); totalPrice = totalPrice.add(priceToAdd); currencyForPriceToAdd = baseCurrency; } else { priceToAdd = price.getPrice().setScale(DEFAULT_DECIMAL_PRECISION, RoundingMode.HALF_UP); currencyForPriceToAdd = price.getCurrency(); areAllExchangeRatesValid = false; } possiblyIncorrectTotalPrice = possiblyIncorrectTotalPrice.add(priceToAdd); final BigDecimal priceForCurrency = currencyToPriceMap.containsKey(currencyForPriceToAdd) ? currencyToPriceMap.get(currencyForPriceToAdd).add(priceToAdd) : priceToAdd; currencyToPriceMap.put(currencyForPriceToAdd, priceForCurrency); } this.totalPrice = totalPrice.setScale(TOTAL_DECIMAL_PRECISION, RoundingMode.HALF_DOWN); this.possiblyIncorrectTotalPrice = possiblyIncorrectTotalPrice.setScale(TOTAL_DECIMAL_PRECISION, RoundingMode.HALF_DOWN); this.areAllExchangeRatesValid = areAllExchangeRatesValid; this.exchangeRate = new ExchangeRateBuilderFactory().setBaseCurrency(baseCurrency).build(); this.decimalFormattedPrice = calculateDecimalFormattedPrice(); this.currencyFormattedPrice = calculateCurrencyFormattedPrice(); this.currencyCodeFormattedPrice = calculateCurrencyCodeFormattedPrice(); }