diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 1d050cf09..40da7db4e 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -2,7 +2,7 @@ package net.minestom.server.tag; import net.kyori.adventure.text.Component; import net.minestom.server.item.ItemStack; -import net.minestom.server.utils.collection.IndexMap; +import net.minestom.server.utils.collection.AutoIncrementMap; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -30,7 +30,7 @@ import java.util.function.UnaryOperator; */ @ApiStatus.NonExtendable public class Tag { - private static final IndexMap INDEX_MAP = new IndexMap<>(); + private static final AutoIncrementMap INDEX_MAP = new AutoIncrementMap<>(); record PathEntry(String name, int index) { } diff --git a/src/main/java/net/minestom/server/utils/collection/IndexMap.java b/src/main/java/net/minestom/server/utils/collection/AutoIncrementMap.java similarity index 69% rename from src/main/java/net/minestom/server/utils/collection/IndexMap.java rename to src/main/java/net/minestom/server/utils/collection/AutoIncrementMap.java index 8c1480fd5..89a31fe7d 100644 --- a/src/main/java/net/minestom/server/utils/collection/IndexMap.java +++ b/src/main/java/net/minestom/server/utils/collection/AutoIncrementMap.java @@ -6,13 +6,14 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @ApiStatus.Internal -public final class IndexMap { +public final class AutoIncrementMap { private final Object2IntOpenHashMap write = new Object2IntOpenHashMap<>(); - private Object2IntOpenHashMap read = copy(); + private Object2IntOpenHashMap read; private int lastIndex; - public IndexMap() { - write.defaultReturnValue(-1); + public AutoIncrementMap() { + this.write.defaultReturnValue(-1); + this.read = write.clone(); } @Contract(pure = true) @@ -24,16 +25,10 @@ public final class IndexMap { index = write.getInt(key); if (index == -1) { write.put(key, (index = lastIndex++)); - read = copy(); + read = write.clone(); } } } return index; } - - private Object2IntOpenHashMap copy() { - var map = new Object2IntOpenHashMap<>(write); - map.defaultReturnValue(-1); - return map; - } } diff --git a/src/test/java/net/minestom/server/utils/collection/IndexMapTest.java b/src/test/java/net/minestom/server/utils/collection/AutoIncrementMapTest.java similarity index 79% rename from src/test/java/net/minestom/server/utils/collection/IndexMapTest.java rename to src/test/java/net/minestom/server/utils/collection/AutoIncrementMapTest.java index 09c4fcd23..b19454170 100644 --- a/src/test/java/net/minestom/server/utils/collection/IndexMapTest.java +++ b/src/test/java/net/minestom/server/utils/collection/AutoIncrementMapTest.java @@ -4,10 +4,10 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -public class IndexMapTest { +public class AutoIncrementMapTest { @Test public void test() { - IndexMap map = new IndexMap<>(); + AutoIncrementMap map = new AutoIncrementMap<>(); for (int i = 0; i < 1000; i++) { assertEquals(i, map.get("test" + i)); for (int j = 0; j < i; j++) {