class FungibleTradeBuilder extends AbstractTradeBuilder {

  UniqueId updateTrade(BeanDataSource tradeData) {
    ManageableTrade trade = buildTrade(tradeData);
    ManageableTrade previousTrade = getPositionMaster().getTrade(trade.getUniqueId());
    ManageablePosition position = getPositionMaster().get(previousTrade.getParentPositionId()).getPosition();
    if (!trade.getSecurityLink().equals(previousTrade.getSecurityLink())) {
      throw new IllegalArgumentException("Cannot update a trade's security. new version " + trade +
                                             ", previous version: " + previousTrade);
    }
    List<ManageableTrade> trades = Lists.newArrayList();
    for (ManageableTrade existingTrade : position.getTrades()) {
      if (existingTrade.getUniqueId().equals(trade.getUniqueId())) {
        trades.add(trade);
        position.setQuantity(position.getQuantity().subtract(existingTrade.getQuantity()).add(trade.getQuantity()));
      } else {
        trades.add(existingTrade);
      }
    }
    position.setTrades(trades);
    ManageablePosition savedPosition = getPositionMaster().update(new PositionDocument(position)).getPosition();
    ManageableTrade savedTrade = savedPosition.getTrade(trade.getUniqueId().getObjectId());
    if (savedTrade == null) {
      // shouldn't ever happen
      throw new DataNotFoundException("Failed to save trade " + trade + " to position " + savedPosition);
    } else {
      return savedTrade.getUniqueId();
    }
  }

    FungibleTradeBuilder(PositionMaster positionMaster,
                                     PortfolioMaster portfolioMaster,
                                     SecurityMaster securityMaster,
                                     Set<MetaBean> metaBeans,
                                     StringConvert stringConvert);

}