/* (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 ask is sent by a provider. * * @param ask the ask sent by the provider */ public void onReceiveAsk(MessageAsk ask) { synchronized (syncObj) { Collections.sort(bids, compBids); if (bids.size() > 0) { MessageBid bid = (MessageBid) bids.getFirst(); double priceAsk = ask.getPrice(); double priceBid = bid.getPrice(); if (priceBid >= priceAsk) { double finalPrice = (priceAsk + priceBid) / 2; super.match(ask, bid, finalPrice); bids.remove(bid); } else { asks.add(ask); } } else { asks.add(ask); } } }