diff --git a/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java b/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java index 33eb9574..0c4f97ce 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java @@ -30,13 +30,15 @@ public class MessageDispatcher implements Telegraph { private static final String LOG_TAG = MessageDispatcher.class.getSimpleName(); - private static final Pool POOL = new Pool(16) { + private static final Pool POOL_GLOBAL = new Pool(16) { @Override protected Telegram newObject () { return new Telegram(); } }; + private final Pool pool; + private PriorityQueue queue; private IntMap> msgListeners; @@ -47,6 +49,13 @@ protected Telegram newObject () { /** Creates a {@code MessageDispatcher} */ public MessageDispatcher () { + this(POOL_GLOBAL); + } + + public MessageDispatcher (Pool pool) { + if (pool == null) + throw new IllegalArgumentException("pool cannot be null"); + this.pool = pool; this.queue = new PriorityQueue(); this.msgListeners = new IntMap>(); this.msgProviders = new IntMap>(); @@ -180,7 +189,7 @@ public void clearProviders () { /** Removes all the telegrams from the queue and releases them to the internal pool. */ public void clearQueue () { for (int i = 0; i < queue.size(); i++) { - POOL.free(queue.get(i)); + pool.free(queue.get(i)); } queue.clear(); } @@ -466,7 +475,7 @@ public void dispatchMessage (float delay, Telegraph sender, Telegraph receiver, throw new IllegalArgumentException("Sender cannot be null when a return receipt is needed"); // Get a telegram from the pool - Telegram telegram = POOL.obtain(); + Telegram telegram = pool.obtain(); telegram.sender = sender; telegram.receiver = receiver; telegram.message = msg; @@ -499,7 +508,7 @@ public void dispatchMessage (float delay, Telegraph sender, Telegraph receiver, boolean added = queue.add(telegram); // Return it to the pool if has been rejected - if (!added) POOL.free(telegram); + if (!added) pool.free(telegram); if (debugEnabled) { if (added) @@ -598,7 +607,7 @@ private void discharge (Telegram telegram) { discharge(telegram); } else { // Release the telegram to the pool - POOL.free(telegram); + pool.free(telegram); } }