From 1464fb7c2528364cc729c70a29ab3e0b522e5522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerd=20G=C3=BChne=20=28Marfir=29?= Date: Sun, 2 Apr 2023 10:43:37 +0200 Subject: [PATCH] #3237: add test case; refactoring --- .../security/DividendCalculationTest.java | 16 +++++++++++++ .../security/DividendCalculation.java | 23 +++++-------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/snapshot/security/DividendCalculationTest.java b/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/snapshot/security/DividendCalculationTest.java index 2251c13646..862ce25645 100644 --- a/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/snapshot/security/DividendCalculationTest.java +++ b/name.abuchen.portfolio.tests/src/name/abuchen/portfolio/snapshot/security/DividendCalculationTest.java @@ -200,6 +200,22 @@ public void rateOfReturnCalculationTest() assertEquals(0.1, dividends.getRateOfReturnPerYear(), 0.0); } + @Test + public void testCalculateYieldOnCost_emptyPortfolioShouldNotCauseExceptions() + { + Account myWealthyAccount = new Account("myWealthyAccount"); + myWealthyAccount.setCurrencyCode("USD"); + Security apple = new Security("Apple Corp", "USD"); + + List transactions = new ArrayList<>(); + + @SuppressWarnings("unused") + CostCalculation cost = Calculation.perform(CostCalculation.class, converter, apple, transactions); + DividendCalculation dividends = Calculation.perform(DividendCalculation.class, converter, apple, transactions); + + assertEquals(0.0, dividends.getYieldOnCost(), 0.0); + } + @Test public void testCalculateYieldOnCost_noDividendsShouldNotCauseExceptions() { diff --git a/name.abuchen.portfolio/src/name/abuchen/portfolio/snapshot/security/DividendCalculation.java b/name.abuchen.portfolio/src/name/abuchen/portfolio/snapshot/security/DividendCalculation.java index e9d089a248..88e99fa8ef 100644 --- a/name.abuchen.portfolio/src/name/abuchen/portfolio/snapshot/security/DividendCalculation.java +++ b/name.abuchen.portfolio/src/name/abuchen/portfolio/snapshot/security/DividendCalculation.java @@ -291,23 +291,12 @@ public void visit(CurrencyConverter converter, CalculationLineItem.DividendPayme { double fifoCosts = fifoCostAmount * Values.AmountFraction.factor(); - double diviSum = 0.0; - if (years > 1) - { - LocalDate startTimeframe = now.minusDays(365); - for (Payment p : payments) - { - if (p.date.isAfter(startTimeframe)) - { - diviSum = diviSum + (p.amount.getAmount() * Values.AmountFraction.factor()); - } - } - } - else - { - diviSum = sum.getAmount() * Values.AmountFraction.factor(); - diviSum = diviSum / years; - } + LocalDate firstDividendPaymentAccepted = now.minusYears(1); + double diviSum = years > 1 ? payments.stream() // + .filter(p -> p.date.isAfter(firstDividendPaymentAccepted)) // + .mapToDouble(p -> p.amount.getAmount() * Values.AmountFraction.factor()) // + .sum() // + : sum.getAmount() * Values.AmountFraction.factor() / years; return diviSum * 100.0 / fifoCosts; }