javatools.administrative
Class Parameters

java.lang.Object
  extended by javatools.administrative.Parameters

public class Parameters
extends java.lang.Object

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). Provides an interface for an ini-File to load parameter settings. Note that this is a static version, i.e. all components share the same parameters. There is also an instantiable NonsharedParameters version. That version can deal with different parameter settings used in parallel. If your work will be shared with others, please consider using the NonsharedParameters, as they allow an easier integration of your component with other components that may want to work on a separate set of parameters (e.g. there might be different database settings etc.). The ini-File loaded may contain parameters of the form

 parameterName = value
 ...
 
It may also contain comments or section headers (i.e. anything that does not match the above pattern). Parameter names are not case sensitive. Initial and terminal spaces are trimmed for both parameter names and values. Boolean parameters accept multiple ways of expressing "true" (namely "on", "true", "yes" and "active").

To avoid passing around object handles, this class does not function as an object! There is only one "static object". Example:

 // Read data from my.ini
 Parameters.init("my.ini");
 // Abort with error message if the following parameters are not specified
 Parameters.ensureParameters(
                "firstPar - some help text for the first parameter",
                "secondPar - some help text for the secondparameter");
 // Retrieve the value of a parameter
 String p = Parameters.get("firstPar");
 
You can load parameters from multiple files. These will overlay.


Nested Class Summary
static class Parameters.UndefinedParameterException
          Thrown for an undefined Parameter
 
Field Summary
static java.io.File iniFile
          Holds the filename of the ini-file
static java.util.regex.Pattern INIPATTERN
          Holds the pattern used for ini-file-entries
static FinalSet<java.lang.String> no
          Holds words that count as "no" for boolean parameters
static java.util.Map<java.lang.String,java.lang.String> values
          Contains the values for the parameters
 
Constructor Summary
Parameters()
           
 
Method Summary
static void add(java.lang.String key, java.lang.String value)
          Adds a value to the map and to the ini file, if not yet there
static void ensureParameters(java.lang.String... p)
          Reports an error message and aborts if the parameters are undefined.
static java.lang.String get(java.lang.String s)
          Returns a value for a parameter
static java.lang.String get(java.lang.String s, java.lang.String defaultValue)
          Returns a value for a parameter, returning a default value by default
static boolean getBoolean(java.lang.String s)
          Returns a value for a boolean parameter
static boolean getBoolean(java.lang.String s, boolean defaultValue)
          Returns a value for a boolean parameter, returning a default value by default
static boolean getBooleanArgument(java.lang.String[] args, java.lang.String... argnames)
          Parses the arguments of the main method and tells whether a parameter is on or off
static Database getDatabase()
          Returns the database defined in this ini-file
static double getDouble(java.lang.String s)
          Returns a value for an integer parameter
static double getDouble(java.lang.String s, double defaultValue)
          Returns a value for an integer parameter returning the default value if undefined
static java.io.File getFile(java.lang.String s)
          Returns a value for a file or folder parameter
static java.io.File getFile(java.lang.String s, java.io.File defaultValue)
          Returns a value for a file or folder parameter, returning the default value if undefined
static int getInt(java.lang.String s)
          Returns a value for an integer parameter
static int getInt(java.lang.String s, int defaultValue)
          Returns a value for an integer parameter returning the default value if undefined
static java.util.List<java.lang.String> getList(java.lang.String s)
          Returns a value for a list parameter
static java.lang.String getOrRequest(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static java.lang.String getOrRequestAndAdd(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static boolean getOrRequestAndAddBoolean(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static java.io.File getOrRequestAndAddFile(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static int getOrRequestAndAddInt(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static boolean getOrRequestBoolean(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static java.io.File getOrRequestFileParameter(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static int getOrRequestInteger(java.lang.String s, java.lang.String description)
          Returns a value for a parameter.
static void init(java.io.File f)
          Initializes the parameters from a file
static void init(java.lang.String file)
          Initializes the parameters from a file
static void init(java.lang.String filename, java.io.File... folders)
          Seeks the file in all given folders
static boolean isDefined(java.lang.String s)
          Tells whether a parameter is defined
static void main(java.lang.String[] args)
          Test routine
static java.util.Set<java.lang.String> parameters()
          Returns all defined parameters
static java.lang.String remove(java.lang.String parameter)
          Removes a value from the mapping (NOT: from the file)
static void reset()
          Deletes all current values
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iniFile

public static java.io.File iniFile
Holds the filename of the ini-file


values

public static java.util.Map<java.lang.String,java.lang.String> values
Contains the values for the parameters


INIPATTERN

public static java.util.regex.Pattern INIPATTERN
Holds the pattern used for ini-file-entries


no

public static FinalSet<java.lang.String> no
Holds words that count as "no" for boolean parameters

Constructor Detail

Parameters

public Parameters()
Method Detail

getFile

public static java.io.File getFile(java.lang.String s)
                            throws Parameters.UndefinedParameterException
Returns a value for a file or folder parameter

Throws:
Parameters.UndefinedParameterException

getFile

public static java.io.File getFile(java.lang.String s,
                                   java.io.File defaultValue)
                            throws Parameters.UndefinedParameterException
Returns a value for a file or folder parameter, returning the default value if undefined

Throws:
Parameters.UndefinedParameterException

getInt

public static int getInt(java.lang.String s)
                  throws Parameters.UndefinedParameterException
Returns a value for an integer parameter

Throws:
Parameters.UndefinedParameterException

getInt

public static int getInt(java.lang.String s,
                         int defaultValue)
                  throws Parameters.UndefinedParameterException
Returns a value for an integer parameter returning the default value if undefined

Throws:
Parameters.UndefinedParameterException

getDouble

public static double getDouble(java.lang.String s)
                        throws Parameters.UndefinedParameterException
Returns a value for an integer parameter

Throws:
Parameters.UndefinedParameterException

getDouble

public static double getDouble(java.lang.String s,
                               double defaultValue)
                        throws Parameters.UndefinedParameterException
Returns a value for an integer parameter returning the default value if undefined

Throws:
Parameters.UndefinedParameterException

getBoolean

public static boolean getBoolean(java.lang.String s)
                          throws Parameters.UndefinedParameterException
Returns a value for a boolean parameter

Throws:
Parameters.UndefinedParameterException

getBoolean

public static boolean getBoolean(java.lang.String s,
                                 boolean defaultValue)
Returns a value for a boolean parameter, returning a default value by default


getList

public static java.util.List<java.lang.String> getList(java.lang.String s)
                                                throws Parameters.UndefinedParameterException
Returns a value for a list parameter

Throws:
Parameters.UndefinedParameterException

get

public static java.lang.String get(java.lang.String s)
                            throws Parameters.UndefinedParameterException
Returns a value for a parameter

Throws:
Parameters.UndefinedParameterException

get

public static java.lang.String get(java.lang.String s,
                                   java.lang.String defaultValue)
Returns a value for a parameter, returning a default value by default


getOrRequest

public static java.lang.String getOrRequest(java.lang.String s,
                                            java.lang.String description)
Returns a value for a parameter. If not present, asks the user for it


getOrRequestAndAdd

public static java.lang.String getOrRequestAndAdd(java.lang.String s,
                                                  java.lang.String description)
                                           throws java.io.IOException
Returns a value for a parameter. If not present, asks the user for it and adds it

Throws:
java.io.IOException

getOrRequestAndAddInt

public static int getOrRequestAndAddInt(java.lang.String s,
                                        java.lang.String description)
                                 throws java.io.IOException
Returns a value for a parameter. If not present, asks the user for it and adds it

Throws:
java.io.IOException

getOrRequestAndAddFile

public static java.io.File getOrRequestAndAddFile(java.lang.String s,
                                                  java.lang.String description)
                                           throws java.io.IOException
Returns a value for a parameter. If not present, asks the user for it and adds it

Throws:
java.io.IOException

getOrRequestAndAddBoolean

public static boolean getOrRequestAndAddBoolean(java.lang.String s,
                                                java.lang.String description)
                                         throws java.io.IOException
Returns a value for a parameter. If not present, asks the user for it and adds it

Throws:
java.io.IOException

getOrRequestFileParameter

public static java.io.File getOrRequestFileParameter(java.lang.String s,
                                                     java.lang.String description)
Returns a value for a parameter. If not present, asks the user for it


remove

public static java.lang.String remove(java.lang.String parameter)
Removes a value from the mapping (NOT: from the file)


getOrRequestBoolean

public static boolean getOrRequestBoolean(java.lang.String s,
                                          java.lang.String description)
Returns a value for a parameter. If not present, asks the user for it


getOrRequestInteger

public static int getOrRequestInteger(java.lang.String s,
                                      java.lang.String description)
Returns a value for a parameter. If not present, asks the user for it


add

public static void add(java.lang.String key,
                       java.lang.String value)
                throws java.io.IOException
Adds a value to the map and to the ini file, if not yet there

Throws:
java.io.IOException

init

public static void init(java.io.File f)
                 throws java.io.IOException
Initializes the parameters from a file

Throws:
java.io.IOException

init

public static void init(java.lang.String filename,
                        java.io.File... folders)
                 throws java.io.IOException
Seeks the file in all given folders

Throws:
java.io.IOException

isDefined

public static boolean isDefined(java.lang.String s)
Tells whether a parameter is defined


init

public static void init(java.lang.String file)
                 throws java.io.IOException
Initializes the parameters from a file

Throws:
java.io.IOException

ensureParameters

public static void ensureParameters(java.lang.String... p)
Reports an error message and aborts if the parameters are undefined. p may contain strings of the form "parametername explanation"


getBooleanArgument

public static boolean getBooleanArgument(java.lang.String[] args,
                                         java.lang.String... argnames)
Parses the arguments of the main method and tells whether a parameter is on or off


reset

public static void reset()
Deletes all current values


getDatabase

public static Database getDatabase()
                            throws java.lang.Exception
Returns the database defined in this ini-file

Throws:
java.lang.Exception

parameters

public static java.util.Set<java.lang.String> parameters()
Returns all defined parameters


main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test routine

Throws:
java.lang.Exception