Added more kotlin utilities

This commit is contained in:
Auxilor
2022-02-03 11:23:07 +00:00
parent 7628c0bbfd
commit 9b5cc1fd9c
3 changed files with 47 additions and 0 deletions

View File

@@ -3,6 +3,9 @@ package com.willfp.eco.util;
import com.willfp.eco.core.Eco;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* Utilities / API methods for {@link NamespacedKey}s.
@@ -45,8 +48,25 @@ public final class NamespacedKeyUtils {
*/
@NotNull
public static NamespacedKey fromString(@NotNull final String string) {
return Objects.requireNonNull(NamespacedKeyUtils.fromStringOrNull(string));
}
/**
* Create a NamespacedKey from a string.
* <p>
* Preferred over {@link NamespacedKey#fromString(String)} for performance reasons.
*
* @param string The string.
* @return The key, or null if not a key.
*/
@Nullable
public static NamespacedKey fromStringOrNull(@NotNull final String string) {
int index = string.indexOf(":");
if (index < 0) {
return null;
}
return NamespacedKeyUtils.create(
string.substring(0, index),
string.substring(index + 1)

View File

@@ -13,3 +13,9 @@ fun <T> List<T>.toFrequencyMap(): Map<T, Int> =
*/
fun Iterable<String>.containsIgnoreCase(element: String): Boolean =
ListUtils.containsIgnoreCase(this, element)
/**
* @see ListUtils.create2DList
*/
fun <T> create2DList(rows: Int, columns: Int): List<List<T>> =
ListUtils.create2DList(rows, columns)

View File

@@ -0,0 +1,21 @@
@file:JvmName("NamespacedKeyUtilsExtensions")
package com.willfp.eco.util
/**
* @see NamespacedKeyUtils.fromString
*/
fun namespacedKeyOf(string: String) =
NamespacedKeyUtils.fromString(string)
/**
* @see NamespacedKeyUtils.fromString
*/
fun safeNamespacedKeyOf(string: String) =
NamespacedKeyUtils.fromStringOrNull(string)
/**
* @see NamespacedKeyUtils.create
*/
fun namespacedKeyOf(namespace: String, key: String) =
NamespacedKeyUtils.create(namespace, key)