Add getObject methods to ConfigurationSection

By: stonar96 <minecraft.stonar96@gmail.com>
This commit is contained in:
Bukkit/Spigot
2019-03-19 23:19:09 +01:00
parent 5f8c960c7c
commit 4526a9f21e
3 changed files with 102 additions and 6 deletions

View File

@@ -633,6 +633,53 @@ public interface ConfigurationSection {
public List<Map<?, ?>> getMapList(@NotNull String path);
// 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
* path.

View File

@@ -653,18 +653,30 @@ public class MemorySection implements ConfigurationSection {
// Bukkit
@Nullable
@Override
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz) {
Validate.notNull(clazz, "ConfigurationSerializable class cannot be null");
public <T extends Object> T getObject(@NotNull String path, @NotNull Class<T> clazz) {
Validate.notNull(clazz, "Class cannot be null");
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
@Override
public <T extends ConfigurationSerializable> T getSerializable(@NotNull String path, @NotNull Class<T> clazz, @Nullable T def) {
Validate.notNull(clazz, "ConfigurationSerializable class cannot be null");
Object val = get(path);
return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def;
return getObject(path, clazz, def);
}
@Nullable