mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-11 09:51:35 +01:00
record namespace (#2374)
This commit is contained in:
parent
5ec42c6ba9
commit
b1c34cc9d7
@ -1,7 +1,5 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.intellij.lang.annotations.Pattern;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -12,89 +10,59 @@ import java.util.Objects;
|
||||
* Represents a namespaced ID
|
||||
* https://minecraft.wiki/w/Namespaced_ID
|
||||
*/
|
||||
public final class NamespaceID implements CharSequence, Key {
|
||||
private static final String legalLetters = "[0123456789abcdefghijklmnopqrstuvwxyz_-]+";
|
||||
private static final String legalPathLetters = "[0123456789abcdefghijklmnopqrstuvwxyz./_-]+";
|
||||
private static final Cache<String, NamespaceID> CACHE = Caffeine.newBuilder().weakKeys().weakValues().build();
|
||||
|
||||
private final String domain;
|
||||
private final String path;
|
||||
private final String full;
|
||||
public record NamespaceID(@NotNull String domain, @NotNull String path) implements CharSequence, Key {
|
||||
private static final String LEGAL_LETTERS = "[0123456789abcdefghijklmnopqrstuvwxyz_-]+";
|
||||
private static final String LEGAL_PATH_LETTERS = "[0123456789abcdefghijklmnopqrstuvwxyz./_-]+";
|
||||
|
||||
public static @NotNull NamespaceID from(@NotNull String namespace) {
|
||||
return CACHE.get(namespace, id -> {
|
||||
final int index = id.indexOf(':');
|
||||
final String domain;
|
||||
final String path;
|
||||
if (index < 0) {
|
||||
domain = "minecraft";
|
||||
path = id;
|
||||
id = "minecraft:" + id;
|
||||
} else {
|
||||
domain = id.substring(0, index);
|
||||
path = id.substring(index + 1);
|
||||
}
|
||||
return new NamespaceID(id, domain, path);
|
||||
});
|
||||
final int index = namespace.indexOf(Key.DEFAULT_SEPARATOR);
|
||||
final String domain;
|
||||
final String path;
|
||||
if (index == -1) {
|
||||
domain = Key.MINECRAFT_NAMESPACE;
|
||||
path = namespace;
|
||||
} else {
|
||||
domain = namespace.substring(0, index);
|
||||
path = namespace.substring(index + 1);
|
||||
}
|
||||
return new NamespaceID(domain, path);
|
||||
}
|
||||
|
||||
public static @NotNull NamespaceID from(@NotNull String domain, @NotNull String path) {
|
||||
return from(domain + ":" + path);
|
||||
return new NamespaceID(domain, path);
|
||||
}
|
||||
|
||||
public static @NotNull NamespaceID from(@NotNull Key key) {
|
||||
return from(key.asString());
|
||||
return new NamespaceID(key.namespace(), key.value());
|
||||
}
|
||||
|
||||
private NamespaceID(String full, String domain, String path) {
|
||||
this.full = full;
|
||||
this.domain = domain;
|
||||
this.path = path;
|
||||
assert !domain.contains(".") && !domain.contains("/") : "Domain cannot contain a dot nor a slash character (" + full + ")";
|
||||
assert domain.matches(legalLetters) : "Illegal character in domain (" + full + "). Must match " + legalLetters;
|
||||
assert path.matches(legalPathLetters) : "Illegal character in path (" + full + "). Must match " + legalPathLetters;
|
||||
}
|
||||
|
||||
public @NotNull String domain() {
|
||||
return domain;
|
||||
}
|
||||
|
||||
public @NotNull String path() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof final Key that)) return false;
|
||||
return Objects.equals(this.domain, that.namespace()) && Objects.equals(this.path, that.value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = this.domain.hashCode();
|
||||
result = (31 * result) + this.path.hashCode();
|
||||
return result;
|
||||
public NamespaceID {
|
||||
domain = domain.intern();
|
||||
path = path.intern();
|
||||
assert !domain.contains(".") && !domain.contains("/") : "Domain cannot contain a dot nor a slash character (" + asString() + ")";
|
||||
assert domain.matches(LEGAL_LETTERS) : "Illegal character in domain (" + asString() + "). Must match " + LEGAL_LETTERS;
|
||||
assert path.matches(LEGAL_PATH_LETTERS) : "Illegal character in path (" + asString() + "). Must match " + LEGAL_PATH_LETTERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return full.length();
|
||||
return domain.length() + 1 + path.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charAt(int index) {
|
||||
return full.charAt(index);
|
||||
if (index < domain.length()) {
|
||||
return domain.charAt(index);
|
||||
} else if (index == domain.length()) {
|
||||
return ':';
|
||||
} else {
|
||||
return path.charAt(index - domain.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CharSequence subSequence(int start, int end) {
|
||||
return full.subSequence(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return full;
|
||||
return asString().subSequence(start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -110,16 +78,25 @@ public final class NamespaceID implements CharSequence, Key {
|
||||
|
||||
@Override
|
||||
public @NotNull String asString() {
|
||||
return this.full;
|
||||
return domain + ':' + path;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getDomain() {
|
||||
return domain();
|
||||
@Override
|
||||
public @NotNull String toString() {
|
||||
return asString();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getPath() {
|
||||
return path();
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof final Key that)) return false;
|
||||
return Objects.equals(this.domain, that.namespace()) && Objects.equals(this.path, that.value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = this.domain.hashCode();
|
||||
result = (31 * result) + this.path.hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user