public Map<InvestmentVehicle, AssetPosition> getPositionsByVehicle() { Map<InvestmentVehicle, AssetPosition> answer = new HashMap<InvestmentVehicle, AssetPosition>(); long assets = getAssets(); for (SecurityPosition p : jointPortfolio.getPositions()) answer.put(p.getSecurity(), new AssetPosition(p.getSecurity(), p, assets)); for (AccountSnapshot a : accounts) { SecurityPosition sp = new SecurityPosition(null); sp.setShares(Values.Share.factor()); sp.setPrice(new SecurityPrice(getTime(), a.getFunds())); answer.put(a.getAccount(), new AssetPosition(a.getAccount(), sp, assets)); } return answer; }
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); }