2023-06-29 13:42:36 +02:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2022-05-28 21:55:41 +02:00
|
|
|
package de.bluecolored.bluemap.core.util;
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
import de.bluecolored.bluemap.api.debug.DebugDump;
|
2022-05-28 21:55:41 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
@DebugDump
|
2024-02-01 23:01:42 +01:00
|
|
|
public class Key implements Keyed {
|
|
|
|
|
|
|
|
private static final ConcurrentHashMap<String, String> STRING_INTERN_POOL = new ConcurrentHashMap<>();
|
2022-05-28 21:55:41 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
public static final String MINECRAFT_NAMESPACE = "minecraft";
|
|
|
|
public static final String BLUEMAP_NAMESPACE = "bluemap";
|
2022-05-28 21:55:41 +02:00
|
|
|
|
|
|
|
private final String namespace;
|
|
|
|
private final String value;
|
|
|
|
private final String formatted;
|
|
|
|
|
|
|
|
public Key(String formatted) {
|
|
|
|
String namespace = MINECRAFT_NAMESPACE;
|
2022-08-01 11:12:50 +02:00
|
|
|
String value = formatted;
|
2022-05-28 21:55:41 +02:00
|
|
|
int namespaceSeparator = formatted.indexOf(':');
|
|
|
|
if (namespaceSeparator > 0) {
|
|
|
|
namespace = formatted.substring(0, namespaceSeparator);
|
2022-08-01 11:12:50 +02:00
|
|
|
value = formatted.substring(namespaceSeparator + 1);
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
this.namespace = intern(namespace);
|
|
|
|
this.value = intern(value);
|
|
|
|
this.formatted = intern(this.namespace + ":" + this.value);
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Key(String namespace, String value) {
|
2024-02-01 23:01:42 +01:00
|
|
|
this.namespace = intern(namespace);
|
|
|
|
this.value = intern(value);
|
|
|
|
this.formatted = intern(this.namespace + ":" + this.value);
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFormatted() {
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
@Override
|
|
|
|
public Key getKey() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-05-29 02:34:26 +02:00
|
|
|
@SuppressWarnings("StringEquality")
|
2022-05-28 21:55:41 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
2022-08-01 11:12:50 +02:00
|
|
|
Key that = (Key) o;
|
2023-11-08 13:27:51 +01:00
|
|
|
return formatted == that.formatted;
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2024-02-01 23:01:42 +01:00
|
|
|
return formatted.hashCode();
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return formatted;
|
|
|
|
}
|
2024-02-01 23:01:42 +01:00
|
|
|
|
|
|
|
public static Key parse(String formatted) {
|
|
|
|
return new Key(formatted);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Key parse(String formatted, String defaultNamespace) {
|
|
|
|
String namespace = defaultNamespace;
|
|
|
|
String value = formatted;
|
|
|
|
int namespaceSeparator = formatted.indexOf(':');
|
|
|
|
if (namespaceSeparator > 0) {
|
|
|
|
namespace = formatted.substring(0, namespaceSeparator);
|
|
|
|
value = formatted.substring(namespaceSeparator + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Key(namespace, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Using our own function instead of {@link String#intern()} since the ConcurrentHashMap is much faster.
|
|
|
|
*/
|
|
|
|
private static String intern(String string) {
|
|
|
|
String interned = STRING_INTERN_POOL.putIfAbsent(string, string);
|
|
|
|
return interned != null ? interned : string;
|
|
|
|
}
|
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|