86 lines
2.3 KiB
Java
Raw Normal View History

package net.knarcraft.blacksmith.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for getting an object value as the correct type
*/
public final class ConfigHelper {
private ConfigHelper() {
}
/**
* Gets the given value as a string list
*
* @param value <p>The raw string list value</p>
* @return <p>The value as a string list, or null if not compatible</p>
*/
public static List<String> asStringList(Object value) {
if (value instanceof String) {
return List.of(((String) value).split(","));
} else if (value instanceof List<?> list) {
List<String> strings = new ArrayList<>();
for (Object object : list) {
strings.add(String.valueOf(object));
}
return strings;
} else {
return null;
}
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a double</p>
*/
public static double asDouble(Object value) {
if (value instanceof String) {
return Double.parseDouble((String) value);
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return (Double) value;
}
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a boolean</p>
*/
public static boolean asBoolean(Object value) {
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* Gets the given value as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as an integer</p>
*/
public static int asInt(Object value) {
if (value instanceof String) {
return Integer.parseInt((String) value);
} else {
return (Integer) value;
}
}
}