public synchronized void updateCurrentMarket() {
    Price buyTopPrice = buySide.topOfBookPrice();
    if (buyTopPrice == null) buyTopPrice = PriceFactory.makeLimitPrice((long) 0);

    Price sellTopPrice = sellSide.topOfBookPrice();
    if (sellTopPrice == null) sellTopPrice = PriceFactory.makeLimitPrice((long) 0);

    String str =
        buyTopPrice.toString()
            + buySide.topOfBookVolume()
            + sellTopPrice.toString()
            + sellSide.topOfBookVolume();

    if (!this.lastCurrentMarket.equals(str)) {
      MarketDataDTO mktDTO =
          new MarketDataDTO(
              this.productSymbol,
              buyTopPrice,
              buySide.topOfBookVolume(),
              sellTopPrice,
              sellSide.topOfBookVolume());

      CurrentMarketPublisher.getInstance().publishCurrentMarket(mktDTO);
      lastCurrentMarket = str;
    }
  }
  public synchronized void addToBook(Quote q) throws Exception {
    Price sellQuote = q.getQuoteSide("SELL").getPrice();
    Price buyQuote = q.getQuoteSide("BUY").getPrice();

    if (sellQuote.lessOrEqual(buyQuote))
      throw new DataValidationException("SELL Price is less than or Equal to BUY Price");
    if (q.getQuoteSide("SELL").getOriginalVolume() <= 0
        || q.getQuoteSide("BUY").getOriginalVolume() <= 0)
      throw new DataValidationException("BUY or SELL side is less than or equal to zero");

    if (this.userQuotes.contains(q.getUserName())) {
      buySide.removeQuote(q.getUserName());
      sellSide.removeQuote(q.getUserName());
      this.updateCurrentMarket();
    }
    this.addToBook(BookSide.BUY, q.getQuoteSide("BUY"));
    this.addToBook(BookSide.SELL, q.getQuoteSide("SELL"));

    userQuotes.add(q.getUserName());
    this.updateCurrentMarket();
  }
  public synchronized void openMarket() throws Exception {
    Price bestBuyPrice = buySide.topOfBookPrice();
    Price bestSellPrice = sellSide.topOfBookPrice();

    if (bestBuyPrice != null && bestSellPrice != null) {
      while (bestBuyPrice.greaterOrEqual(bestSellPrice)
          || bestBuyPrice.isMarket()
          || bestSellPrice.isMarket()) {
        ArrayList<Tradable> topOfBuySide = buySide.getEntriesAtPrice(bestBuyPrice);
        ArrayList<Tradable> toRemove = new ArrayList<Tradable>();
        HashMap<String, FillMessage> allFills = null;

        for (Tradable t : topOfBuySide) {
          allFills = sellSide.tryTrade(t);
          if (t.getRemainingVolume() == 0) {
            toRemove.add(t);
          }
        }

        for (Tradable t : toRemove) {
          buySide.removeTradable(t);
        }
        updateCurrentMarket();

        Price lastSalePrice = this.determineLastSalePrice(allFills);
        int lastSaleVolume = this.determineLastSaleQuantity(allFills);

        LastSalePublisher.getInstance()
            .publishLastSale(productSymbol, lastSalePrice, lastSaleVolume);

        bestBuyPrice = buySide.topOfBookPrice();
        bestSellPrice = sellSide.topOfBookPrice();

        if (bestBuyPrice == null || bestSellPrice == null) break;
      }
    }
  }