Configuration Class
parent
047c7ab322
commit
1f08fa0d9d
@ -0,0 +1,88 @@
|
||||
package net.pingex.discordbot;
|
||||
|
||||
import org.ini4j.Ini;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Contains configuration keys for the whole app. Also parses a .ini configuration file.
|
||||
* @version 0.1-dev
|
||||
* @author Raphael "Pingex" NAAS
|
||||
*/
|
||||
public final class Configuration
|
||||
{
|
||||
private static Logger LOGGER = Logger.getLogger(Configuration.class.getName());
|
||||
|
||||
/**
|
||||
* Main Configuration Datastore
|
||||
*/
|
||||
private static Ini datastore = new Ini();
|
||||
|
||||
/**
|
||||
* Load keys from a file to the datastore
|
||||
* @param path Relative or absolute path to the config file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void loadConfiguration(String path) throws IOException
|
||||
{
|
||||
LOGGER.info("Loading configuration file: " + path);
|
||||
datastore.load(new File(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a value from the datastore
|
||||
* @param section Section of the datastore
|
||||
* @param key Key wanted
|
||||
* @return A String representation of the value, `null` if the key doesn't exist
|
||||
*/
|
||||
public static String getValue(String section, String key)
|
||||
{
|
||||
return getValue(section, key, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a value from the datastore
|
||||
* @param section Section of the datastore
|
||||
* @param key Key wanted
|
||||
* @param returnType Method will return a variable of this type
|
||||
* @param <T> Method will return a variable of this type
|
||||
* @return A <T> representation of the value, `null` if the key doesn't exist
|
||||
*/
|
||||
public static<T> T getValue(String section, String key, Class<T> returnType)
|
||||
{
|
||||
return datastore.get(section, key, returnType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the values associated with this key
|
||||
* @param section Section of the datastore
|
||||
* @param key Key wanted
|
||||
* @return A List of String containing all the values
|
||||
*/
|
||||
public static List<String> getMultiValue(String section, String key)
|
||||
{
|
||||
return datastore.get(section).getAll(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a key value. If value is null or an empty String, then the key will be removed from the datastore.
|
||||
* Please note this method doesn't support multivalue keys.
|
||||
* @param section Section of the datastore
|
||||
* @param key Key to change
|
||||
* @param value New key value
|
||||
*/
|
||||
public static void setValue(String section, String key, String value)
|
||||
{
|
||||
if(value == null || value.isEmpty())
|
||||
datastore.remove(section, key);
|
||||
else
|
||||
{
|
||||
if(datastore.get(section, key) == null)
|
||||
datastore.add(section, key, value);
|
||||
else
|
||||
datastore.get(section).replace(key, value);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue