From fe5f7dbf2b2da5eb5a258d630da5af946fe77ebf Mon Sep 17 00:00:00 2001 From: Son PC Date: Wed, 25 Sep 2024 15:24:36 +0700 Subject: [PATCH 1/3] allow the option to use an external pool --- .../src/com/badlogic/gdx/ai/msg/MessageDispatcher.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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..3bb0aab3 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,11 @@ protected Telegram newObject () { /** Creates a {@code MessageDispatcher} */ public MessageDispatcher () { + this(POOL_GLOBAL); + } + + public MessageDispatcher (Pool pool) { + POOL = pool; this.queue = new PriorityQueue(); this.msgListeners = new IntMap>(); this.msgProviders = new IntMap>(); From d2f8d3f4e67122a393ad29d5001b88daf9304cdd Mon Sep 17 00:00:00 2001 From: Son PC Date: Wed, 25 Sep 2024 15:41:13 +0700 Subject: [PATCH 2/3] add null check for pool parameter --- gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java | 2 ++ 1 file changed, 2 insertions(+) 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 3bb0aab3..a50228de 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java @@ -53,6 +53,8 @@ public MessageDispatcher () { } public MessageDispatcher (Pool pool) { + if (pool == null) + throw new IllegalArgumentException("pool cannot be null"); POOL = pool; this.queue = new PriorityQueue(); this.msgListeners = new IntMap>(); From 4210073dcacebf777ed70ca64cf055bb7cc2d002 Mon Sep 17 00:00:00 2001 From: Son PC Date: Wed, 25 Sep 2024 21:21:11 +0700 Subject: [PATCH 3/3] change POOL member name to lowercase --- .../com/badlogic/gdx/ai/msg/MessageDispatcher.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 a50228de..0c4f97ce 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/msg/MessageDispatcher.java @@ -37,7 +37,7 @@ protected Telegram newObject () { } }; - private final Pool POOL; + private final Pool pool; private PriorityQueue queue; @@ -55,7 +55,7 @@ public MessageDispatcher () { public MessageDispatcher (Pool pool) { if (pool == null) throw new IllegalArgumentException("pool cannot be null"); - POOL = pool; + this.pool = pool; this.queue = new PriorityQueue(); this.msgListeners = new IntMap>(); this.msgProviders = new IntMap>(); @@ -189,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(); } @@ -475,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; @@ -508,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) @@ -607,7 +607,7 @@ private void discharge (Telegram telegram) { discharge(telegram); } else { // Release the telegram to the pool - POOL.free(telegram); + pool.free(telegram); } }