public void processEvent()

in business-model/src/main/java/com/google/cloud/orderbook/OrderBookBuilder.java [39:87]


  public void processEvent(OrderBookEvent obe) {

    // Record the last orderbook event
    lastOrderBookEvent = obe;

    // Calculate the quantity delta
    long qty = 0;
    switch (obe.getType()) {
      case NEW: {
        qty = obe.getQuantityRemaining();
        break;
      }
      case EXECUTED: {
        qty = -1 * obe.getQuantityFilled();
        break;
      }
      case DELETED: {
        qty = -1 * obe.getQuantityRemaining();
        break;
      }
      default: {
        // Nothing
      }
    }

    // Skip if no delta!
    if (qty == 0) {
      return;
    }

    // Determine the price to adjust
    long price = obe.getPrice();
    if (obe.getSide() == OrderBookEvent.Side.BUY) {
      price *= -1;
    }

    // Adjust the prices at the level
    final long qtyChange = qty;
    prices.compute(price, (k, v) -> {

      // Calculate new quantity
      long newQty = ((v == null) ? 0 : (long)v) + qtyChange;

      // Return the new quantity -- null (remove) if zero
      return (newQty != 0) ? newQty : null;
    });

    receivedLastContractMessage = obe.getLastContractMessage();
  }