Пример #1
0
  private void initialiseDefaultMarket() {
    market = new Market();
    matchList = new LinkedList<RequestOfferMatch>();

    PriceOffer po780_1 = new PriceOffer(1000000);
    po780_1.setPrice(new BigDecimal(7.80));
    po780_1.setOfferTime(DateTimeUtil.createDateFromTime(1, 20));
    market.addOffer(po780_1);

    PriceOffer po780_2 = new PriceOffer(1000000);
    po780_2.setPrice(new BigDecimal(7.80));
    po780_2.setOfferTime(DateTimeUtil.createDateFromTime(1, 30));
    market.addOffer(po780_2);

    PriceOffer po775_1 = new PriceOffer(500000);
    po775_1.setPrice(new BigDecimal(7.75));
    po775_1.setOfferTime(DateTimeUtil.createDateFromTime(0, 10));
    market.addOffer(po775_1);

    PriceRequest pr765_1 = new PriceRequest(250000);
    pr765_1.setPrice(new BigDecimal(7.65));
    pr765_1.setRequestTime(DateTimeUtil.createDateFromTime(0, 40));
    pr765_1.setTier(1);
    market.addRequest(pr765_1);
  }
Пример #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();
    }
  }