/* (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 { MessageBid bida = (MessageBid) a; MessageBid bidb = (MessageBid) b; Double d_c1 = new Double(bida.getPrice()); Double d_c2 = new Double(bidb.getPrice()); return d_c2.compareTo(d_c1); } }
/** * 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); } } }