<commit_before>package name.abuchen.portfolio.snapshot;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import name.abuchen.portfolio.Messages;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.SecurityPrice;
import name.abuchen.portfolio.model.Taxonomy;

public class PortfolioSnapshot
{
    // //////////////////////////////////////////////////////////////
    // factory methods
    // //////////////////////////////////////////////////////////////

    public static PortfolioSnapshot create(Portfolio portfolio, Date time)
    {
        Map<Security, SecurityPosition> positions = new HashMap<Security, SecurityPosition>();

        for (PortfolioTransaction t : portfolio.getTransactions())
        {
            if (t.getDate().getTime() <= time.getTime())
            {
                switch (t.getType())
                {
                    case TRANSFER_IN:
                    case BUY:
                    case DELIVERY_INBOUND:
                    {
                        SecurityPosition p = positions.get(t.getSecurity());
                        if (p == null)
                            positions.put(t.getSecurity(), p = new SecurityPosition(t.getSecurity()));
                        p.addTransaction(t);
                        break;
                    }
                    case TRANSFER_OUT:
                    case SELL:
                    case DELIVERY_OUTBOUND:
                    {
                        SecurityPosition p = positions.get(t.getSecurity());
                        if (p == null)
                            positions.put(t.getSecurity(), p = new SecurityPosition(t.getSecurity()));
                        p.addTransaction(t);
                        break;
                    }
                    default:
                        throw new UnsupportedOperationException("Unsupported operation: " + t.getType()); //$NON-NLS-1$
                }
            }
        }

        ArrayList<SecurityPosition> collection = new ArrayList<SecurityPosition>(positions.values());
        for (Iterator<SecurityPosition> iter = collection.iterator(); iter.hasNext();)
        {
            SecurityPosition p = iter.next();

            if (p.getShares() == 0)
            {
                iter.remove();
            }
            else
            {
                SecurityPrice price = p.getSecurity().getSecurityPrice(time);
                p.setPrice(price);
            }
        }

        return new PortfolioSnapshot(portfolio, time, collection);
    }

    public static PortfolioSnapshot merge(List<PortfolioSnapshot> snapshots)
    {
        if (snapshots.isEmpty())
            throw new RuntimeException("Error: PortfolioSnapshots to be merged must not be empty"); //$NON-NLS-1$<commit_msg>Remove obsolete switch statement when creating portfolio snapshot<commit_after>package name.abuchen.portfolio.snapshot;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import name.abuchen.portfolio.Messages;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.SecurityPrice;
import name.abuchen.portfolio.model.Taxonomy;

public class PortfolioSnapshot
{
    // //////////////////////////////////////////////////////////////
    // factory methods
    // //////////////////////////////////////////////////////////////

    public static PortfolioSnapshot create(Portfolio portfolio, Date time)
    {
        Map<Security, SecurityPosition> positions = new HashMap<Security, SecurityPosition>();

        for (PortfolioTransaction t : portfolio.getTransactions())
        {
            if (t.getDate().getTime() <= time.getTime())
            {
                SecurityPosition position = positions.get(t.getSecurity());
                if (position == null)
                {
                    position = new SecurityPosition(t.getSecurity());
                    positions.put(t.getSecurity(), position);
                }
                position.addTransaction(t);
            }
        }

        ArrayList<SecurityPosition> collection = new ArrayList<SecurityPosition>(positions.values());
        for (Iterator<SecurityPosition> iter = collection.iterator(); iter.hasNext();)
        {
            SecurityPosition p = iter.next();

            if (p.getShares() == 0)
            {
                iter.remove();
            }
            else
            {
                SecurityPrice price = p.getSecurity().getSecurityPrice(time);
                p.setPrice(price);
            }
        }

        return new PortfolioSnapshot(portfolio, time, collection);
    }

    public static PortfolioSnapshot merge(List<PortfolioSnapshot> snapshots)
    {
        if (snapshots.isEmpty())
            throw new RuntimeException("Error: PortfolioSnapshots to be merged must not be empty"); //$NON-NLS-1$
{'fraction_non_alphanumeric': 0.14220271044092384, 'fraction_numerical': 0.000954380606986066, 'mean_word_length': 1.6384692849949647, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773240', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1232, 'n_words': 255}