StockOp { @CoGroup public void cutoff( @Key(group = "ITEM_CODE", order = "PURCHASED_DATE ASC") List<Stock> stocks, @Key(group = "ITEM_CODE", order = "SHIPPED_DATE ASC") List<Shipment> shipments, Result<Stock> newStocks, Result<Shipment> newShipments) { Iterator<Stock> eachStock = stocks.iterator(); Iterator<Shipment> eachShipment = shipments.iterator(); while (eachStock.hasNext()) { Stock stock = eachStock.next(); while (eachShipment.hasNext()) { if (stock.getQuantity() == 0) { break; } Shipment shipment = eachShipment.next(); shipment.setCost(stock.getCost()); newShipments.add(shipment); stock.getQuantityOption().add(-1); } newStocks.add(stock); } while (eachShipment.hasNext()) { Shipment shipment = eachShipment.next(); newShipments.add(shipment); } }  @Branch ShipmentStatus checkShipment(Shipment shipment); @CoGroup void cutoff(
            // 在庫は商品ごとにグルーピングして、さらに入荷順に並べる
            @Key(group = "ITEM_CODE", order = "PURCHASED_DATE ASC") List<Stock> stocks,
            // 注文も商品ごとにグルーピングして、さらに出荷順に並べる
            @Key(group = "ITEM_CODE", order = "SHIPPED_DATE ASC") List<Shipment> shipments,
            Result<Stock> newStocks,
            Result<Shipment> newShipments);  }
@Test public void testCutoff() { StockOpImpl operator = new StockOpImpl(); List<Stock> stocks = new ArrayList<Stock>(); stocks.add(StockFactory.create(new DateTime(), 0, 100, 10)); List<Shipment> shipments = new ArrayList<Shipment>(); shipments.add(ShipmentFactory.create(new DateTime(), 10, 100)); MockResult<Stock> newStocks = new MockResult<Stock>(); MockResult<Shipment> newShipments = new MockResult<Shipment>(); operator.cutoff(stocks, shipments, newStocks, newShipments); assertThat(newStocks.getResults().size(), is(1)); assertThat(newShipments.getResults().size(), is(1)); }
@Test public void testCutoff_shortage() { StockOpImpl operator = new StockOpImpl(); List<Stock> stocks = Arrays.asList(StockFactory.create(new DateTime(), 0, 100, 10)); List<Shipment> shipments = Arrays.asList(); MockResult<Stock> newStocks = new MockResult<Stock>(); MockResult<Shipment> newShipments = new MockResult<Shipment>(); operator.cutoff(stocks, shipments, newStocks, newShipments); assertThat(newStocks.getResults().size(), is(1)); assertThat(newShipments.getResults().size(), is(0)); }
@Test public void testCutoff_race() { StockOpImpl operator = new StockOpImpl(); List<Stock> stocks = new ArrayList<Stock>(); List<Shipment> shipments = new ArrayList<Shipment>(); MockResult<Stock> newStocks = new MockResult<Stock>(); MockResult<Shipment> newShipments = new MockResult<Shipment>(); operator.cutoff(stocks, shipments, newStocks, newShipments); assertThat(newStocks.getResults().size(), is(0)); assertThat(newShipments.getResults().size(), is(0)); }