コード例 #1
0
    /* (non-Javadoc)
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    public int compare(Object a, Object b) throws ClassCastException {
      if (a == null) {
        return -1;
      } else if (b == null) {
        return 1;
      } else if (a == null && b == null) {
        return 0;
      } else {
        MessageAsk aska = (MessageAsk) a;
        MessageAsk askb = (MessageAsk) b;

        Double d_c1 = new Double(aska.getPrice());
        Double d_c2 = new Double(askb.getPrice());

        return d_c1.compareTo(d_c2);
      }
    }
コード例 #2
0
  /**
   * Called when a bid is received.
   *
   * @param bid the bid received by the auctioneer
   */
  public void onReceiveBid(MessageBid bid) {
    synchronized (syncObj) {
      Collections.sort(asks, compAsks);
      if (asks.size() > 0) {
        MessageAsk ask = (MessageAsk) asks.getFirst();
        double priceAsk = ask.getPrice();
        double priceBid = bid.getPrice();

        if (priceBid >= priceAsk) {
          double finalPrice = (priceAsk + priceBid) / 2;
          super.match(ask, bid, finalPrice);
          asks.remove(ask);
        } else {
          bids.add(bid);
        }
      } else {
        bids.add(bid);
      }
    }
  }