package com.paritytrading.parity.net.pmd;

import com.paritytrading.parity.net.ProtocolMessage;
import java.nio.ByteBuffer;

/**
 * Common definitions.
 */
public class PMD {

    private PMD() {
    }

    public static final long VERSION = 2;

    public static final byte BUY  = 'B';
    public static final byte SELL = 'S';

    static final byte MESSAGE_TYPE_VERSION        = 'V';
    static final byte MESSAGE_TYPE_ORDER_ADDED    = 'A';
    static final byte MESSAGE_TYPE_ORDER_EXECUTED = 'E';
    static final byte MESSAGE_TYPE_ORDER_CANCELED = 'X';

    static final int MESSAGE_LENGTH_VERSION        =  5;
    static final int MESSAGE_LENGTH_ORDER_ADDED    = 42;
    static final int MESSAGE_LENGTH_ORDER_EXECUTED = 29;
    static final int MESSAGE_LENGTH_ORDER_CANCELED = 25;

    /**
     * A message.
     */
    public interface Message extends ProtocolMessage {
    }

    /**
     * A Version message.
     */
    public static class Version implements Message {
        public long version;

        @Override
        public void get(ByteBuffer buffer) {
            version = buffer.getInt() & 0xffffffffL;
        }

        @Override
        public void put(ByteBuffer buffer) {
            buffer.put(MESSAGE_TYPE_VERSION);
            buffer.putInt((int)version);
        }
    }

    /**
     * An Order Added message.
     */
    public static class OrderAdded implements Message {
        public long timestamp;
        public long orderNumber;
        public byte side;
        public long instrument;
        public long quantity;
        public long price;

        @Override
        public void get(ByteBuffer buffer) {
            timestamp   = buffer.getLong();
            orderNumber = buffer.getLong();
            side        = buffer.get();
            instrument  = buffer.getLong();
            quantity    = buffer.getLong();
            price       = buffer.getLong();
        }

        @Override
        public void put(ByteBuffer buffer) {
            buffer.put(MESSAGE_TYPE_ORDER_ADDED);
            buffer.putLong(timestamp);
            buffer.putLong(orderNumber);
            buffer.put(side);
            buffer.putLong(instrument);
            buffer.putLong(quantity);
            buffer.putLong(price);
        }
    }

    /**
     * An Order Executed message.
     */
    public static class OrderExecuted implements Message {
        public long timestamp;
        public long orderNumber;
        public long quantity;
        public long matchNumber;

        @Override
        public void get(ByteBuffer buffer) {
            timestamp   = buffer.getLong();
            orderNumber = buffer.getLong();
            quantity    = buffer.getLong();
            matchNumber = buffer.getInt() & 0xffffffffL;
        }

        @Override
        public void put(ByteBuffer buffer) {
            buffer.put(MESSAGE_TYPE_ORDER_EXECUTED);
            buffer.putLong(timestamp);
            buffer.putLong(orderNumber);
            buffer.putLong(quantity);
            buffer.putInt((int)matchNumber);
        }
    }

    /**
     * An Order Canceled message.
     */
    public static class OrderCanceled implements Message {
        public long timestamp;
        public long orderNumber;
        public long canceledQuantity;

        @Override
        public void get(ByteBuffer buffer) {
            timestamp        = buffer.getLong();
            orderNumber      = buffer.getLong();
            canceledQuantity = buffer.getLong();
        }

        @Override
        public void put(ByteBuffer buffer) {
            buffer.put(MESSAGE_TYPE_ORDER_CANCELED);
            buffer.putLong(timestamp);
            buffer.putLong(orderNumber);
            buffer.putLong(canceledQuantity);
        }
    }

}

@@ -1090,37 +1090,30 @@
 n = 
-buffer.getInt() & 0xffffffffL
+getUnsignedInt(buffer)
 ;%0A  
@@ -1242,35 +1242,39 @@
         
-buffer.putInt((int)
+putUnsignedInt(buffer, 
 version)
@@ -2724,37 +2724,30 @@
 r = 
-buffer.getInt() & 0xffffffffL
+getUnsignedInt(buffer)
 ;%0A  
@@ -3005,27 +3005,31 @@
     
-buffer.putInt((int)
+putUnsignedInt(buffer, 
 matc
@@ -3747,10 +3747,237 @@
 %0A    %7D%0A%0A
+    private static long getUnsignedInt(ByteBuffer buffer) %7B%0A        return buffer.getInt() & 0xffffffffL;%0A    %7D%0A%0A    private static void putUnsignedInt(ByteBuffer buffer, long value) %7B%0A        buffer.putInt((int)value);%0A    %7D%0A%0A
 %7D%0A

Clean up PMD message handling