Remove unused/unsafe hashmap, make namespace cache thread safe

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-23 19:20:49 +01:00
parent 02de469cad
commit ad75e942aa
2 changed files with 8 additions and 92 deletions

View File

@ -1,17 +1,18 @@
package net.minestom.server.utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* Represents a namespaced ID
* https://minecraft.gamepedia.com/Namespaced_ID
*/
public class NamespaceID implements CharSequence, Key {
private static final Int2ObjectOpenHashMap<NamespaceID> cache = new Int2ObjectOpenHashMap<>();
public final class NamespaceID implements CharSequence, Key {
private static final Map<Integer, NamespaceID> cache = new ConcurrentHashMap<>();
private static final String legalLetters = "[0123456789abcdefghijklmnopqrstuvwxyz_-]+";
private static final String legalPathLetters = "[0123456789abcdefghijklmnopqrstuvwxyz./_-]+";
@ -26,8 +27,7 @@ public class NamespaceID implements CharSequence, Key {
* @param namespaceID the namespace id to get the domain from
* @return the domain of the namespace ID
*/
@NotNull
public static String getDomain(@NotNull String namespaceID) {
public static @NotNull String getDomain(@NotNull String namespaceID) {
final int index = namespaceID.indexOf(':');
if (index < 0)
return "minecraft";
@ -42,7 +42,7 @@ public class NamespaceID implements CharSequence, Key {
* @param namespaceID the namespace id to get the path from
* @return the path of the namespace ID
*/
public static String getPath(@NotNull String namespaceID) {
public static @NotNull String getPath(@NotNull String namespaceID) {
final int index = namespaceID.indexOf(':');
if (index < 0)
return namespaceID;
@ -67,19 +67,6 @@ public class NamespaceID implements CharSequence, Key {
return from(key.asString());
}
private NamespaceID(@NotNull String path) {
final int index = path.indexOf(':');
if (index < 0) {
this.domain = "minecraft";
this.path = path;
} else {
this.domain = path.substring(0, index);
this.path = path.substring(index + 1);
}
this.full = toString();
validate();
}
private NamespaceID(String domain, String path) {
this.domain = domain;
this.path = path;
@ -124,13 +111,12 @@ public class NamespaceID implements CharSequence, Key {
}
@Override
public CharSequence subSequence(int start, int end) {
public @NotNull CharSequence subSequence(int start, int end) {
return full.subSequence(start, end);
}
@NotNull
@Override
public String toString() {
public @NotNull String toString() {
return domain + ":" + path;
}

View File

@ -1,70 +0,0 @@
package net.minestom.server.utils;
import org.jetbrains.annotations.NotNull;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
public class NamespaceIDHashMap<V> extends AbstractMap<NamespaceID, V> {
private final Map<NamespaceID, V> backing = new HashMap<>();
@NotNull
@Override
public Set<Entry<NamespaceID, V>> entrySet() {
return backing.entrySet();
}
@Override
public V get(Object key) {
return backing.get(key);
}
@Override
public V put(NamespaceID key, V value) {
return backing.put(key, value);
}
public boolean containsKey(String id) {
return containsKey(NamespaceID.getDomain(id), NamespaceID.getPath(id));
}
public boolean containsKey(String domain, String path) {
return backing.containsKey(NamespaceID.from(domain, path));
}
public V get(String id) {
return get(NamespaceID.getDomain(id), NamespaceID.getPath(id));
}
public V get(String domain, String path) {
return backing.get(NamespaceID.from(domain, path));
}
public V put(String domain, String path, V value) {
return put(NamespaceID.from(domain, path), value);
}
public V computeIfAbsent(String domain, String path, Function<? super NamespaceID, ? extends V> mappingFunction) {
return computeIfAbsent(NamespaceID.from(domain, path), mappingFunction);
}
public V put(String id, V value) {
return put(NamespaceID.from(id), value);
}
public V computeIfAbsent(String id, Function<? super NamespaceID, ? extends V> mappingFunction) {
return computeIfAbsent(NamespaceID.from(id), mappingFunction);
}
public V getOrDefault(String id, V defaultValue) {
return getOrDefault(NamespaceID.from(id), defaultValue);
}
public V getOrDefault(String domain, String path, V defaultValue) {
return getOrDefault(NamespaceID.from(domain, path), defaultValue);
}
}