csli.util
Class Config

java.lang.Object
  extended by csli.util.Config

public class Config
extends Object

Author:
Danilo Mirkovic

Field Summary
static Config main
          Main config file for the application.
 
Method Summary
 String get(String key)
          Returns the config property, null if it doesn't exist.
 File getAbsoluteFile(String value)
          Returns a file assuming that it is specified as an absolute path.
 List<File> getAbsoluteFileList(String key)
          Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files.
 File getAbsoluteFileProperty(String key)
          This returns a File object as described by the specified key, which must be an absolute path.
 boolean getBoolean(String key)
          If the value of the key is true, return true, otherwise return false;
 List<Boolean> getBooleanList(String key)
          Returns a list of Booleans corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets.
static Config getConfig(String filename)
          Returns the config object for the specified filename.
static String getConfigDir()
           
 Properties getConfigProperties()
           
 double getDouble(String key)
          Returns a double corresponding to the key, NaN if none
 List<Double> getDoubleList(String key)
          Returns a list of Doubles corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets.
 List<File> getExistingFileList(String key)
          Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files.
 File getExistingFileProperty(String key)
          This returns a File object as described by the specified key.
 List<File> getFileList(String key)
          Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files.
 String getFilename()
           
 File getFileProperty(String key)
          This returns a File object as described by the specified key, which must be a path relative to the config directory.
 int getInteger(String key)
          Returns an integer corresponding to the key, -1 if none
 List<Integer> getIntegerList(String key)
          Returns a list of Integers corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets.
 Collection getKeys()
           
 List<String> getList(String key)
          Gets a list, which is specified either as a filename, or comma delimited, in square brackets.
static Config getMainConfig()
           
 File getRelativeFile(String value)
          Returns a file relative to the main config dir.
 List<File> getRelativeFileList(String key)
          Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files.
 Set<String> getSet(String key)
          Same as getList, only returning a set (each element is unique) See comments for getList.
 void save()
          Save the config properties
 void saveList(String configKey, List list)
          Save the list in the file specified by the config setting.
 void set(String key, String value)
          Sets the config property ( and auto-saves the file).
 void set(String key, String value, boolean save)
           
 void setBoolean(String key, boolean b)
           
 void setBoolean(String key, boolean b, boolean save)
           
static void setConfigDir(String path)
           
 void setInteger(String key, int i)
           
static void setMainConfigFile(String filename)
          Sets the main properties file.
static void setMainEmptyConfig()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

main

public static Config main
Main config file for the application. Should be set when the application starts.

Method Detail

getConfig

public static Config getConfig(String filename)
Returns the config object for the specified filename. Efficient -- if it's not available in memory, reads it from a file in the config directory. Returns null if the file does not exist.

Parameters:
filename -
Returns:

getConfigDir

public static String getConfigDir()

getMainConfig

public static Config getMainConfig()

setConfigDir

public static void setConfigDir(String path)

setMainConfigFile

public static void setMainConfigFile(String filename)
Sets the main properties file. The path to this file will be considered a path where all other config files reside.


setMainEmptyConfig

public static void setMainEmptyConfig()

get

public String get(String key)
Returns the config property, null if it doesn't exist.

Returns:

getBoolean

public boolean getBoolean(String key)
If the value of the key is true, return true, otherwise return false;


getBooleanList

public List<Boolean> getBooleanList(String key)
Returns a list of Booleans corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets. e.g. key=[true, false, false, true] key=file:file.foo

Parameters:
key -
Returns:
the List, or null if the key or specified file do not exist. Non-boolean entries in the list (or file) will get the value false

getFileList

public List<File> getFileList(String key)
Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files. Just calls getList(key) and appropriately converts to file using same helper as getRelativeFile(String)

Parameters:
key -
Returns:
a list of File objects

getRelativeFileList

public List<File> getRelativeFileList(String key)
Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files. Just calls getList(key) and appropriately converts to file using same helper as getRelativeFile(String)

Parameters:
key -
Returns:
a list of File objects, or null on error

getAbsoluteFileList

public List<File> getAbsoluteFileList(String key)
Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files. Just calls getList(key) and appropriately converts to file using same helper as getAbsoluteFile(String)

Parameters:
key -
Returns:
a list of File objects, or null on error

getExistingFileList

public List<File> getExistingFileList(String key)
Assumes the key's value is a list of file paths, or a file containing such a list (see getList(String)) and returns them as a list of Files. Just calls getList(key) and converts to file using same method as getExistingFileProperty(String) - each value is first treated as a path relative to the config directory; if the file doesn't exist, it is treated as an absolute path; if it still doesn't exist, we return null.

Parameters:
key -
Returns:
a list of File objects, or null on error

getFilename

public String getFilename()
Returns:
the filename from which this config was generated

getFileProperty

public File getFileProperty(String key)
This returns a File object as described by the specified key, which must be a path relative to the config directory. For example, given "whatever.dir=../stuff/" in the config file, and given the current value of Config.getConfigDir() is the file e:/lib/config, specifying "whatever.dir" as the argument to this method would return the file e:/lib/stuff/.


getAbsoluteFileProperty

public File getAbsoluteFileProperty(String key)
This returns a File object as described by the specified key, which must be an absolute path. For example, given "whatever.dir=e:/lib/stuff/" in the config file, specifying "whatever.dir" as the argument to this method would return the file e:/lib/stuff/.


getExistingFileProperty

public File getExistingFileProperty(String key)
This returns a File object as described by the specified key. The description is first treated as a path relative to the config directory; if the file doesn't exist, it is treated as an absolute path; if it still doesn't exist, we return null.

Parameters:
key - the config key
Returns:
a File object if such a file exists, null if not

getInteger

public int getInteger(String key)
Returns an integer corresponding to the key, -1 if none

Parameters:
key -
Returns:

getIntegerList

public List<Integer> getIntegerList(String key)
Returns a list of Integers corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets. e.g. key=[1, 2, 3, 4] key=file:file.foo

Parameters:
key -
Returns:
the List, or null if the key or specified file do not exist. Non-integer entries in the list (or file) will get the value -1

getDouble

public double getDouble(String key)
Returns a double corresponding to the key, NaN if none

Parameters:
key -
Returns:

getDoubleList

public List<Double> getDoubleList(String key)
Returns a list of Doubles corresponding to the key, which is specified either as a filename, or comma delimited, in square brackets. e.g. key=[1.1, 2.0, -3.3, 4] key=file:file.foo

Parameters:
key -
Returns:
the List, or null if the key or specified file do not exist. Non-double entries in the list (or file) will get the value Double.NaN

getKeys

public Collection getKeys()

getList

public List<String> getList(String key)
Gets a list, which is specified either as a filename, or comma delimited, in square brackets. e.g. key=[a, b, c, d] key=file:file.foo In the file, list consists of lines which are not empty and don't begin with // or # Non-list-like values will be converted to singleton lists.

Returns:
the List, or null if the key or specified file do not exist

getRelativeFile

public File getRelativeFile(String value)
Returns a file relative to the main config dir. For example, given "../stuff/file" as the argument, and given the current value of Config.getConfigDir() is the file e:/lib/config, this method would return the file e:/lib/stuff/file.

Parameters:
relativePath -
Returns:

getAbsoluteFile

public File getAbsoluteFile(String value)
Returns a file assuming that it is specified as an absolute path. For example, given "e:/stuff/file" as the argument, this will simply return a File object created directly from that string. If you want to specify relative paths, e.g. "../stuff/file", use getRelativeFile(String) instead

Parameters:
absolutePath -
Returns:

getSet

public Set<String> getSet(String key)
Same as getList, only returning a set (each element is unique) See comments for getList.


save

public void save()
Save the config properties


saveList

public void saveList(String configKey,
                     List list)
Save the list in the file specified by the config setting. the value of the setting must be of from file:filename

Parameters:
configKey -
el -

set

public void set(String key,
                String value)
Sets the config property ( and auto-saves the file). Don't use often.


set

public void set(String key,
                String value,
                boolean save)

setBoolean

public void setBoolean(String key,
                       boolean b)

setBoolean

public void setBoolean(String key,
                       boolean b,
                       boolean save)

setInteger

public void setInteger(String key,
                       int i)

getConfigProperties

public Properties getConfigProperties()