From e2d6310591f4547fb1602d8d1511db505cd7fccc Mon Sep 17 00:00:00 2001 From: Nicolas Peugnet Date: Thu, 11 Jan 2024 14:44:03 +0100 Subject: Finish cleanup of WhipserTargetList - throw IndexOutOfBoundsException instead of IllegalArgumentException as it seems more appropriate. - also throw in get when trying to access out of bounds elements to be more consistent with free. - simply set the bitset to 0 in clear, setting the first and last bits to one was useless as the rest of the logic already took care of these cases --- src/main/java/se/lublin/humla/model/WhisperTargetList.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/se/lublin/humla/model/WhisperTargetList.java b/src/main/java/se/lublin/humla/model/WhisperTargetList.java index 172999a..ae76454 100644 --- a/src/main/java/se/lublin/humla/model/WhisperTargetList.java +++ b/src/main/java/se/lublin/humla/model/WhisperTargetList.java @@ -57,21 +57,25 @@ public class WhisperTargetList { } public WhisperTarget get(byte id) { + if (id < TARGET_MIN || id > TARGET_MAX) + throw new IndexOutOfBoundsException(); + if ((mTakenIds & (1 << id)) == 0) return null; + return mActiveTargets[id - TARGET_MIN]; } public void free(byte slot) { if (slot < TARGET_MIN || slot > TARGET_MAX) - throw new IllegalArgumentException(); + throw new IndexOutOfBoundsException(); mTakenIds &= ~(1 << slot); } public int spaceRemaining() { int counter = 0; - for (byte i = TARGET_MIN; i < TARGET_MAX; i++) { + for (byte i = TARGET_MIN; i <= TARGET_MAX; i++) { if ((mTakenIds & (1 << i)) == 0) { counter++; } @@ -80,7 +84,6 @@ public class WhisperTargetList { } public void clear() { - // Slots 0 and 31 are non-whisper targets. - mTakenIds = 1 | (1 << 31); + mTakenIds = 0; } } -- cgit v1.2.3