Class OrderBook

java.lang.Object
eds.model.OrderBook
All Implemented Interfaces:
IOrderBook

public class OrderBook extends Object implements IOrderBook
Maintains the resting limit orders on both sides of the order book.

The order book stores orders separately for bids and asks using price-sorted maps.

  • Bids are sorted in descending price order (best bid = highest).
  • Asks are sorted in ascending price order (best ask = lowest).
  • Orders at the same price level follow FIFO ordering.

The internal data structures are never exposed directly. Consumers interact with the order book through an immutable OrderBook.OrderBookSnapshot.

Mutation operations are intended to be used only by the matching engine, while read-only data is provided for the UI and statistics layer.

  • Constructor Details

    • OrderBook

      public OrderBook()
  • Method Details

    • hasBids

      public boolean hasBids()
      Checks whether the order book contains any bid orders.
      Specified by:
      hasBids in interface IOrderBook
      Returns:
      true if at least one bid order exists, otherwise false
    • hasAsks

      public boolean hasAsks()
      Checks whether the order book contains any ask orders.
      Specified by:
      hasAsks in interface IOrderBook
      Returns:
      true if at least one ask order exists, otherwise false
    • getBestBidPrice

      public OptionalDouble getBestBidPrice()
      Returns the current best bid price.
      Returns:
      an OptionalDouble containing the highest bid price, or an empty optional if no bids exist
    • getBestAskPrice

      public OptionalDouble getBestAskPrice()
      Returns the current best ask price.
      Returns:
      an OptionalDouble containing the lowest ask price, or an empty optional if no asks exist
    • getBestBidOrder

      public Optional<Order> getBestBidOrder()
      Returns the best bid order currently resting in the book.
      Returns:
      an Optional containing the best bid order, or empty if no bids exist
    • getBestAskOrder

      public Optional<Order> getBestAskOrder()
      Returns the best ask order currently resting in the book.
      Returns:
      an Optional containing the best ask order, or empty if no asks exist
    • getMidPrice

      public OptionalDouble getMidPrice()
      Computes the mid price of the order book.

      The mid price is defined as:

      (bestBid + bestAsk) / 2
      
      Returns:
      an OptionalDouble containing the mid price, or empty if either side of the book is empty
    • getSpread

      public OptionalDouble getSpread()
      Computes the bid-ask spread.

      The spread is defined as:

      bestAsk − bestBid
      
      Returns:
      an OptionalDouble containing the spread, or empty if either side of the book is empty
    • addOrder

      public void addOrder(Order order)
      Adds a LIMIT order to the appropriate side of the order book.

      If the price level does not yet exist, a new FIFO queue is created automatically.

      Parameters:
      order - the limit order to add
      Throws:
      IllegalArgumentException - if the order type is not LIMIT
    • removeOrder

      public void removeOrder(Order order)
      Removes a specific order from the book.

      If the removal leaves a price level empty, that level is removed from the map.

      Parameters:
      order - the order to remove
    • removeBestBidOrder

      public void removeBestBidOrder()
      Removes the best bid order from the order book.

      If the price level becomes empty after removal, the entire level is deleted.

    • removeBestAskOrder

      public void removeBestAskOrder()
      Removes the best ask order from the order book.

      If the price level becomes empty after removal, the entire level is deleted.

    • getSnapshot

      public OrderBook.OrderBookSnapshot getSnapshot()
      Builds an immutable snapshot of the current order book state.

      The returned lists are unmodifiable using List.copyOf(Collection).

      Specified by:
      getSnapshot in interface IOrderBook
      Returns:
      a snapshot containing aggregated bid/ask levels and key metrics
    • remainingOrderCount

      public int remainingOrderCount()
      Returns the total number of resting orders in the book.

      This counts orders across both bid and ask sides.

      Specified by:
      remainingOrderCount in interface IOrderBook
      Returns:
      total number of remaining orders