PortfolioAggregator { public Portfolio aggregate(Portfolio inputPortfolio) { UniqueId portfolioId = inputPortfolio.getUniqueId(); UniqueId aggId; if (portfolioId != null) { String aggPortfolioId = buildPortfolioName(portfolioId.getValue()); aggId = UniqueId.of(portfolioId.getScheme(), aggPortfolioId); } else { aggId = createSyntheticIdentifier(); } String aggPortfolioName = buildPortfolioName(inputPortfolio.getName()); List<Position> flattenedPortfolio = flatten(inputPortfolio); final SimplePortfolioNode root = new SimplePortfolioNode(createSyntheticIdentifier(), buildPortfolioName("Portfolio")); SimplePortfolio aggPortfolio = new SimplePortfolio(aggId, aggPortfolioName, root); aggregate(root, flattenedPortfolio, new ArrayDeque<AggregationFunction<?>>(_aggregationFunctions)); aggPortfolio.setAttributes(inputPortfolio.getAttributes()); return aggPortfolio; } PortfolioAggregator(AggregationFunction<?>... aggregationFunctions);  PortfolioAggregator(Collection<AggregationFunction<?>> aggregationFunctions); Portfolio aggregate(Portfolio inputPortfolio); static List<Position> flatten(Portfolio inputPortfolio); static void aggregate(String portfolioName, String aggregationName,
                               PortfolioMaster portfolioMaster, PositionMaster positionMaster,
                               PositionSource positionSource, SecuritySource secSource,
                               AggregationFunction<?>[] aggregationFunctions, boolean split);  }
@Test(enabled = false) public void multipleInstancesOfSamePosition() { SimplePortfolio portfolio = new SimplePortfolio(id("portfolio"), "portfolio"); SimplePortfolioNode root = new SimplePortfolioNode(id("root"), "root"); SimplePortfolioNode node1 = new SimplePortfolioNode(id("node1"), "node1"); SimplePortfolioNode node2 = new SimplePortfolioNode(id("node2"), "node2"); ExternalId securityId = ExternalId.of("sec", "123"); SimplePosition position = new SimplePosition(id("position"), BigDecimal.ONE, securityId); SimpleCounterparty counterparty = new SimpleCounterparty(ExternalId.of("cpty", "123")); SimpleSecurityLink securityLink = new SimpleSecurityLink(securityId); Trade trade = new SimpleTrade(securityLink, BigDecimal.ONE, counterparty, LocalDate.now(), OffsetTime.now()); position.addTrade(trade); portfolio.setRootNode(root); node1.addPosition(position); node2.addPosition(position); root.addChildNode(node1); root.addChildNode(node2); CounterpartyAggregationFunction fn = new CounterpartyAggregationFunction(); Portfolio aggregate = new PortfolioAggregator(fn).aggregate(portfolio); PortfolioNode aggregateRoot = aggregate.getRootNode(); assertEquals(1, aggregateRoot.getChildNodes().size()); PortfolioNode node = aggregateRoot.getChildNodes().get(0); assertEquals(1, node.getPositions().size()); }