mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-14 11:45:52 -07:00
Add getObject methods to ConfigurationSection
By: stonar96 <minecraft.stonar96@gmail.com>
This commit is contained in:
@@ -633,6 +633,53 @@ public interface ConfigurationSection {
|
|||||||
public List<Map<?, ?>> getMapList(@NotNull String path);
|
public List<Map<?, ?>> getMapList(@NotNull String path);
|
||||||
|
|
||||||
// Bukkit
|
// Bukkit
|
||||||
|
/**
|
||||||
|
* Gets the requested object at the given path.
|
||||||
|
*
|
||||||
|
* If the Object does not exist but a default value has been specified, this
|
||||||
|
* will return the default value. If the Object does not exist and no
|
||||||
|
* default value was specified, this will return null.
|
||||||
|
*
|
||||||
|
* <b>Note:</b> For example #getObject(path, String.class) is <b>not</b>
|
||||||
|
* equivalent to {@link #getString(String) #getString(path)} because
|
||||||
|
* {@link #getString(String) #getString(path)} converts internally all
|
||||||
|
* Objects to Strings. However, #getObject(path, Boolean.class) is
|
||||||
|
* equivalent to {@link #getBoolean(String) #getBoolean(path)} for example.
|
||||||
|
*
|
||||||
|
* @param <T> the type of the requested object
|
||||||
|
* @param path the path to the object.
|
||||||
|
* @param clazz the type of the requested object
|
||||||
|
* @return Requested object
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public <T extends Object> T getObject(@NotNull String path, @NotNull Class<T> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the requested object at the given path, returning a default value if
|
||||||
|
* not found
|
||||||
|
*
|
||||||
|
* If the Object does not exist then the specified default value will
|
||||||
|
* returned regardless of if a default has been identified in the root
|
||||||
|
* {@link Configuration}.
|
||||||
|
*
|
||||||
|
* <b>Note:</b> For example #getObject(path, String.class, def) is
|
||||||
|
* <b>not</b> equivalent to
|
||||||
|
* {@link #getString(String, String) #getString(path, def)} because
|
||||||
|
* {@link #getString(String, String) #getString(path, def)} converts
|
||||||
|
* internally all Objects to Strings. However, #getObject(path,
|
||||||
|
* Boolean.class, def) is equivalent to {@link #getBoolean(String, boolean) #getBoolean(path,
|
||||||
|
* def)} for example.
|
||||||
|
*
|
||||||
|
* @param <T> the type of the requested object
|
||||||
|
* @param path the path to the object.
|
||||||
|
* @param clazz the type of the requested object
|
||||||
|
* @param def the default object to return if the object is not present at
|
||||||
|
* the path
|
||||||
|
* @return Requested object
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public <T extends Object> T getObject(@NotNull String path, @NotNull Class<T> clazz, @Nullable T def);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the requested {@link ConfigurationSerializable} object at the given
|
* Gets the requested {@link ConfigurationSerializable} object at the given
|
||||||
* path.
|
* path.
|
||||||
|
@@ -653,18 +653,30 @@ public class MemorySection implements ConfigurationSection {
|
|||||||
// Bukkit
|
// Bukkit
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
public <T extends Object> T getObject(@NotNull String path, @NotNull Class<T> clazz) {
|
||||||
Validate.notNull(clazz, "ConfigurationSerializable class cannot be null");
|
Validate.notNull(clazz, "Class cannot be null");
|
||||||
Object def = getDefault(path);
|
Object def = getDefault(path);
|
||||||
return getSerializable(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null);
|
return getObject(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public <T extends Object> T getObject(@NotNull String path, @NotNull Class<T> clazz, @Nullable T def) {
|
||||||
|
Validate.notNull(clazz, "Class cannot be null");
|
||||||
|
Object val = get(path, def);
|
||||||
|
return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
|
||||||
|
return getObject(path, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T def) {
|
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T def) {
|
||||||
Validate.notNull(clazz, "ConfigurationSerializable class cannot be null");
|
return getObject(path, clazz, def);
|
||||||
Object val = get(path);
|
|
||||||
return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@@ -460,6 +460,43 @@ public abstract class ConfigurationSectionTest {
|
|||||||
assertFalse(section.isList("doesntExist"));
|
assertFalse(section.isList("doesntExist"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetObject_String_Class() {
|
||||||
|
ConfigurationSection section = getConfigurationSection();
|
||||||
|
|
||||||
|
section.set("set", Integer.valueOf(1));
|
||||||
|
section.addDefault("default", Integer.valueOf(2));
|
||||||
|
section.addDefault("defaultAndSet", Boolean.TRUE);
|
||||||
|
section.set("defaultAndSet", Integer.valueOf(3));
|
||||||
|
|
||||||
|
assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class));
|
||||||
|
assertNull(section.getObject("set", Boolean.class));
|
||||||
|
assertEquals(Integer.valueOf(2), section.getObject("default", Number.class));
|
||||||
|
assertNull(section.getObject("default", Boolean.class));
|
||||||
|
assertEquals(Integer.valueOf(3), section.getObject("defaultAndSet", Integer.class));
|
||||||
|
assertEquals(Boolean.TRUE, section.getObject("defaultAndSet", Boolean.class));
|
||||||
|
assertEquals(Integer.valueOf(3), section.getObject("defaultAndSet", Object.class));
|
||||||
|
assertNull(section.getObject("defaultAndSet", String.class));
|
||||||
|
assertNull(section.getObject("doesntExist", Boolean.class));
|
||||||
|
assertNull(section.getString("doesntExist"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetObject_String_Class_T() {
|
||||||
|
ConfigurationSection section = getConfigurationSection();
|
||||||
|
|
||||||
|
section.set("set", Integer.valueOf(1));
|
||||||
|
section.addDefault("default", Integer.valueOf(2));
|
||||||
|
|
||||||
|
assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class, null));
|
||||||
|
assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class, Integer.valueOf(4)));
|
||||||
|
assertNull(section.getObject("set", Boolean.class, null));
|
||||||
|
assertNull(section.getObject("default", Integer.class, null));
|
||||||
|
assertNull(section.getObject("doesntExist", Boolean.class, null));
|
||||||
|
assertEquals(Boolean.TRUE, section.getObject("doesntExist", Boolean.class, Boolean.TRUE));
|
||||||
|
assertNull(section.getString("doesntExist"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetVector_String() {
|
public void testGetVector_String() {
|
||||||
ConfigurationSection section = getConfigurationSection();
|
ConfigurationSection section = getConfigurationSection();
|
||||||
|
Reference in New Issue
Block a user