mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-23 10:35:18 +01:00
Adds getter to ConfigEntry for List<String>
This commit is contained in:
parent
da3c89450e
commit
3a09c19dbb
@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
@ -195,6 +196,22 @@ public class ConfigEntry {
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
public @Nullable List<String> getStringList() {
|
||||
return getStringList(null);
|
||||
}
|
||||
|
||||
@Contract("!null -> !null")
|
||||
public @Nullable List<String> getStringList(List<String> fallbackValue) {
|
||||
Object value = get();
|
||||
|
||||
if (value instanceof List) {
|
||||
//noinspection unchecked
|
||||
return (List<String>) value;
|
||||
}
|
||||
|
||||
return fallbackValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #getMaterial(CompatibleMaterial)
|
||||
*/
|
||||
|
@ -4,11 +4,15 @@ import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@ -133,6 +137,26 @@ class ConfigEntryTest {
|
||||
assertTrue(entry.getBoolean(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStringList() {
|
||||
SongodaYamlConfig cfg = new SongodaYamlConfig(new File("ConfigEntryTest.yml"));
|
||||
ConfigEntry entry = new ConfigEntry(cfg, "key");
|
||||
|
||||
final List<String> fallbackValue = Collections.unmodifiableList(new LinkedList<>());
|
||||
|
||||
entry.set(null);
|
||||
assertNull(entry.getStringList());
|
||||
assertSame(fallbackValue, entry.getStringList(fallbackValue));
|
||||
|
||||
entry.set(Collections.singletonList("value"));
|
||||
assertEquals(Collections.singletonList("value"), entry.getStringList());
|
||||
|
||||
entry.set(new String[] {"value2"});
|
||||
assertEquals(Collections.singletonList("value2"), entry.getStringList());
|
||||
|
||||
entry.set("string-value");
|
||||
assertNull(entry.getStringList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMaterial() {
|
||||
|
Loading…
Reference in New Issue
Block a user