package com.paritytrading.parity.match;

import java.util.ArrayList;

class PriceLevel {

    private Side side;

    private long price;

    private ArrayList<Order> orders;

    public PriceLevel(Side side, long price) {
        this.side   = side;
        this.price  = price;
        this.orders = new ArrayList<>();
    }

    public Side getSide() {
        return side;
    }

    public long getPrice() {
        return price;
    }

    public boolean isEmpty() {
        return orders.isEmpty();
    }

    public Order add(long orderId, long size) {
        Order order = new Order(this, orderId, size);

        orders.add(order);

        return order;
    }

    public long match(long orderId, Side side, long quantity, OrderBookListener listener) {
        while (quantity > 0 && !orders.isEmpty()) {
            Order order = orders.get(0);

            long orderQuantity = order.getRemainingQuantity();

            if (orderQuantity > quantity) {
                order.reduce(quantity);

                listener.match(order.getId(), orderId, side, price, quantity, order.getRemainingQuantity());

                quantity = 0;
            } else {
                orders.remove(0);

                listener.match(order.getId(), orderId, side, price, orderQuantity, 0);

                quantity -= orderQuantity;
            }
        }

        return quantity;
    }

    public void delete(Order order) {
        orders.remove(order);
    }

}

@@ -829,21 +829,23 @@
   Order 
-order
+resting
  = order
@@ -872,21 +872,23 @@
    long 
-order
+resting
 Quantity
@@ -890,21 +890,23 @@
 ntity = 
-order
+resting
 .getRema
@@ -939,21 +939,23 @@
     if (
-order
+resting
 Quantity
@@ -985,21 +985,23 @@
         
-order
+resting
 .reduce(
@@ -1035,37 +1035,39 @@
  listener.match(
-order
+resting
 .getId(), orderI
@@ -1092,21 +1092,23 @@
 antity, 
-order
+resting
 .getRema
@@ -1243,21 +1243,23 @@
 r.match(
-order
+resting
 .getId()
@@ -1282,21 +1282,23 @@
  price, 
-order
+resting
 Quantity
@@ -1332,21 +1332,23 @@
 tity -= 
-order
+resting
 Quantity

Clean up matching logic