Adds getter to ConfigEntry for List<String>

This commit is contained in:
Christian Koop 2022-08-18 23:06:16 +02:00
parent da3c89450e
commit 3a09c19dbb
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 41 additions and 0 deletions

View File

@ -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)
*/

View File

@ -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() {