Added default values to ExternalDataStore.get

This commit is contained in:
Auxilor
2023-03-19 22:03:21 +00:00
parent 1e64815e47
commit feb8898a87
2 changed files with 50 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.function.Supplier;
/**
* A simple store key-value store for data to be stored outside of plugins.
@@ -45,6 +46,39 @@ public final class ExternalDataStore {
}
}
/**
* Get data from the store.
*
* @param key The key.
* @param clazz The class.
* @param defaultValue The default value.
* @param <T> The type.
* @return The value.
*/
@NotNull
public static <T> T get(@NotNull final String key,
@NotNull final Class<T> clazz,
@NotNull final T defaultValue) {
T value = get(key, clazz);
return value == null ? defaultValue : value;
}
/**
* Get data from the store.
*
* @param key The key.
* @param clazz The class.
* @param defaultValue The default value.
* @param <T> The type.
* @return The value.
*/
@NotNull
public static <T> T get(@NotNull final String key,
@NotNull final Class<T> clazz,
@NotNull final Supplier<T> defaultValue) {
return get(key, clazz, defaultValue.get());
}
private ExternalDataStore() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -16,3 +16,19 @@ fun writeExternalData(
inline fun <reified T> readExternalData(
key: String
): T? = ExternalDataStore.get(key, T::class.java)
/**
* @see ExternalDataStore.get
*/
inline fun <reified T> readExternalData(
key: String,
default: T
): T = ExternalDataStore.get(key, T::class.java) ?: default
/**
* @see ExternalDataStore.get
*/
inline fun <reified T> readExternalData(
key: String,
default: () -> T
): T = readExternalData(key, default())