package org.jvirtanen.parity.net.pmr;

import static org.jvirtanen.parity.net.pmr.PMR.*;

import com.paritytrading.nassau.MessageListener;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * A parser for inbound messages.
 */
public class PMRParser implements MessageListener {

    private Order  order;
    private Cancel cancel;
    private Trade  trade;

    private PMRListener listener;

    /**
     * Create a parser for inbound messages.
     *
     * @param listener the message listener
     */
    public PMRParser(PMRListener listener) {
        this.order  = new Order();
        this.cancel = new Cancel();
        this.trade  = new Trade();

        this.listener = listener;
    }

    @Override
    public void message(ByteBuffer buffer) throws IOException {
        byte messageType = buffer.get();

        switch (messageType) {
        case MESSAGE_TYPE_ORDER:
            order(buffer);
            break;
        case MESSAGE_TYPE_CANCEL:
            cancel(buffer);
            break;
        case MESSAGE_TYPE_TRADE:
            trade(buffer);
            break;
        default:
            throw new PMRException("Unknown message type: " + (char)messageType);
        }
    }

    private void order(ByteBuffer buffer) throws IOException {
        order.get(buffer);

        listener.order(order);
    }

    private void cancel(ByteBuffer buffer) throws IOException {
        cancel.get(buffer);

        listener.cancel(cancel);
    }

    private void trade(ByteBuffer buffer) throws IOException {
        trade.get(buffer);

        listener.trade(trade);
    }

}

@@ -905,16 +905,20 @@
    order
+.get
 (buffer)
@@ -911,32 +911,67 @@
 er.get(buffer);%0A
+            listener.order(order);%0A
             brea
@@ -1025,16 +1025,20 @@
   cancel
+.get
 (buffer)
@@ -1031,32 +1031,69 @@
 el.get(buffer);%0A
+            listener.cancel(cancel);%0A
             brea
@@ -1145,16 +1145,20 @@
    trade
+.get
 (buffer)
@@ -1151,32 +1151,67 @@
 de.get(buffer);%0A
+            listener.trade(trade);%0A
             brea
@@ -1333,397 +1333,6 @@
  %7D%0A%0A
-    private void order(ByteBuffer buffer) throws IOException %7B%0A        order.get(buffer);%0A%0A        listener.order(order);%0A    %7D%0A%0A    private void cancel(ByteBuffer buffer) throws IOException %7B%0A        cancel.get(buffer);%0A%0A        listener.cancel(cancel);%0A    %7D%0A%0A    private void trade(ByteBuffer buffer) throws IOException %7B%0A        trade.get(buffer);%0A%0A        listener.trade(trade);%0A    %7D%0A%0A
 %7D%0A

Simplify PMR parser in network protocols