package name.abuchen.portfolio.snapshot;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Security;

/* package */final class PortfolioIndex
{
    private PortfolioIndex()
    {}

    /* package */static PerformanceIndex calculate(Client client, Portfolio portfolio, ReportingPeriod reportInterval,
                    List<Exception> warnings)
    {
        Client pseudoClient = new Client();

        Account pseudoAccount = new Account();
        pseudoAccount.setName(""); //$NON-NLS-1$
        pseudoClient.addAccount(pseudoAccount);

        Portfolio pseudoPortfolio = new Portfolio();
        pseudoPortfolio.setReferenceAccount(pseudoAccount);
        pseudoClient.addPortfolio(pseudoPortfolio);

        Set<Security> securities = new HashSet<Security>();

        for (PortfolioTransaction t : portfolio.getTransactions())
        {
            securities.add(t.getSecurity());

            switch (t.getType())
            {
                case BUY:
                case TRANSFER_IN:
                {
                    pseudoPortfolio.addTransaction(new PortfolioTransaction(t.getDate(), t.getSecurity(),
                                    PortfolioTransaction.Type.DELIVERY_INBOUND, t.getShares(), t.getAmount(), t
                                                    .getFees()));
                    break;
                }
                case SELL:
                case TRANSFER_OUT:
                    pseudoPortfolio.addTransaction(new PortfolioTransaction(t.getDate(), t.getSecurity(),
                                    PortfolioTransaction.Type.DELIVERY_OUTBOUND, t.getShares(), t.getAmount(), t
                                                    .getFees()));
                    break;
                case DELIVERY_INBOUND:
                case DELIVERY_OUTBOUND:
                    pseudoPortfolio.addTransaction(t);
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }

        for (Security security : securities)
            pseudoClient.addSecurity(security);

        return PerformanceIndex.forClient(pseudoClient, reportInterval, warnings);
    }

}

@@ -1006,24 +1006,385 @@
 ortfolio);%0A%0A
+        adaptPortfolioTransactions(portfolio, pseudoClient, pseudoPortfolio);%0A        collectDividends(portfolio, pseudoClient, pseudoAccount);%0A%0A        return PerformanceIndex.forClient(pseudoClient, reportInterval, warnings);%0A    %7D%0A%0A    private static void adaptPortfolioTransactions(Portfolio portfolio, Client pseudoClient, Portfolio pseudoPortfolio)%0A    %7B%0A
         Set%3C
@@ -2754,97 +2754,1410 @@
 y);%0A
-%0A        return PerformanceIndex.forClient(pseudoClient, reportInterval, warnings);%0A    %7D%0A
+    %7D%0A%0A    private static void collectDividends(Portfolio portfolio, Client pseudoClient, Account pseudoAccount)%0A    %7B%0A        if (portfolio.getReferenceAccount() != null)%0A        %7B%0A            for (AccountTransaction t : portfolio.getReferenceAccount().getTransactions())%0A            %7B%0A                if (t.getSecurity() != null && pseudoClient.getSecurities().contains(t.getSecurity()))%0A                %7B%0A                    switch (t.getType())%0A                    %7B%0A                        case DIVIDENDS:%0A                            pseudoAccount.addTransaction(t);%0A                            pseudoAccount.addTransaction(new AccountTransaction(t.getDate(), t.getSecurity(),%0A                                            AccountTransaction.Type.REMOVAL, t.getAmount()));%0A                            break;%0A                        case BUY:%0A                        case TRANSFER_IN:%0A                        case SELL:%0A                        case TRANSFER_OUT:%0A                        case DEPOSIT:%0A                        case REMOVAL:%0A                        case INTEREST:%0A                        case TAXES:%0A                        case FEES:%0A                            // do nothing%0A                            break;%0A                        default:%0A                            throw new UnsupportedOperationException();%0A%0A                    %7D%0A%0A                %7D%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Include dividends into performance calculation of one portfolio