Exemplo n.º 1
0
 private List<RequestOfferMatch> cloneMatchList(List<RequestOfferMatch> origList) {
   List<RequestOfferMatch> copy = new ArrayList<RequestOfferMatch>();
   for (RequestOfferMatch match : origList) {
     copy.add(match.clone());
   }
   return copy;
 }
Exemplo n.º 2
0
  private RequestOfferMatch checkForMatchingOffers(PriceRequest req) {
    RequestOfferMatch match = null;
    List<PriceOffer> matchingOffers = market.getOffersFromPrice(req.getPrice());

    if (matchingOffers.isEmpty()) {
      return null;
    } else {
      int amountToFill = req.getAmount();
      int totalMatchedAmount = 0;
      match = new RequestOfferMatch();
      // The first match will be the best price
      match.setPrice(matchingOffers.get(0).getPrice());
      match.setRequestList(new ArrayList<PriceRequest>());
      match.setOfferList(new ArrayList<PriceOffer>());

      ListIterator<PriceOffer> iter = matchingOffers.listIterator();
      while (amountToFill > 0 && iter.hasNext()) {
        PriceOffer offer = iter.next();

        if (offer.getAmount() >= amountToFill) {
          PriceOffer matchedOffer = offer.clone();
          matchedOffer.setAmount(amountToFill);
          offer.setAmount(offer.getAmount() - amountToFill);
          if (offer.getAmount() == 0) {
            market.removeOffer(offer);
          }

          match.getOfferList().add(matchedOffer);
          if (match.getRequestList().isEmpty()) {
            match.getRequestList().add(req.clone());
          } else {
            PriceRequest matchedRequest = match.getRequestList().get(0);
            matchedRequest.setAmount(matchedRequest.getAmount() + amountToFill);
          }
          market.removeRequest(req);
          totalMatchedAmount += amountToFill;
          amountToFill = 0;
        } else {
          PriceOffer matchedOffer = offer.clone();
          match.getOfferList().add(matchedOffer);

          PriceRequest matchedRequest;
          if (match.getRequestList().isEmpty()) {
            matchedRequest = req.clone();
            matchedRequest.setAmount(0);
            match.getRequestList().add(matchedRequest);
          } else {
            matchedRequest = match.getRequestList().get(0);
          }
          int matchedAmount = offer.getAmount();
          matchedRequest.setAmount(matchedRequest.getAmount() + matchedAmount);

          amountToFill -= matchedAmount;
          totalMatchedAmount += matchedAmount;
          req.setAmount(amountToFill);

          iter.remove();
          market.removeOffer(offer);
        }
      }

      match.setMatchedAmount(totalMatchedAmount);
      match.setMatchTime(new Date());
      matchList.push(match);
      return match.clone();
    }
  }