package %%%PACKAGE%%%;

import java.util.ArrayList;
import java.util.Arrays;

final class AsyncMessageQueue {
    private static final Object[] NO_NEW_MESSAGES = new Object[] { 0 };

    private AsyncMessageQueue() {}

    private static int messageCount = 0;
    private static final ArrayList<Object> messages = new ArrayList<Object>();

    public static synchronized void pushMessage(int messageType, Object... params) {
        messages.add(messageType);
        messages.addAll(Arrays.asList(params));
        messageCount++;
    }

    public static synchronized Object[] pumpMessages() {
        if (messageCount == 0) {
            return NO_NEW_MESSAGES;
        } else {
            messages.add(0, (Object) messageCount);
            Object[] output = messages.toArray(new Object[messages.size()]);
            messages.clear();
            messageCount = 0;
            return output;
        }
    }
}
