package fundamental.games.metropolis.connection.global;

import java.util.LinkedList;

public class MessagesQueue<T>
{
    private LinkedList<T> messages = new LinkedList<>();
    private Object queueLock = new Object();

    //********************************

    public MessagesQueue() { }

    //*********************************

    public void addMessage(T message)
    {
        synchronized (queueLock)
        {
            messages.add(message);
        }
    }

    //*********************************

    public T popMessage()
    {
        T message;
        synchronized (queueLock)
        {
            if(messages.isEmpty())
                message = null;
            else
                message = messages.remove();
        }
        return message;
    }
}
