public class Environment
extends java.lang.Object
implements java.lang.Cloneable
The environment class mimics the environment variables available in any shell using a hash map of keys/values, the key being the variables names, excepted here they are called parameters.
In addition, this class provides facilities to:
As in any shell, most of the time, the environment is global and accessible
from any part of the system. Here a singleton instance of this class is
created and accessible from anywhere in the JVM using the
getGlobalEnvironment()
method (indeed the singleton instance is
created at its first access). However, it is still possible to create a
private instance of this class for use in a specific part of a program.
To read a file of parameters, simply call the
readParameterFile(String)
method. In the same way, to write a set of
parameters to a file, call the writeParameterFile(String)
method.
The format of the parameter file is given in the description of these
methods.
To read parameters from he command line, call the
readCommandLine(String[])
or
readCommandLine(String[], Collection)
methods. These methods expect
a format for the command line that is described in there respective
documentations.
It is also possible to setup automatically the fields of an arbitrary object,
provided these fields have name that match parameters in this environment. To
do this call the initializeFieldsOf(Object)
method passing the
object to initialise as argument. The object to setup must provide methods of
the form "setThing(Type)" where "Thing" or "thing" is the name of the field
to set and "Type" is one of "int", "long", "float", "double", "String" and
"boolean". For the boolean type, the accepted values meaning true are "true",
"on", "1", and "yes", all other value are considered as false.
initializeFieldsOf(Object)
?Modifier and Type | Field and Description |
---|---|
static Environment |
GLOBAL_ENV
Global environment for the whole JVM.
|
Constructor and Description |
---|
Environment() |
Modifier and Type | Method and Description |
---|---|
Environment |
clone()
Generate a new Environment object with a deep copy of the elements this
object.
|
boolean |
getBooleanParameter(java.lang.String parameter)
Check a parameter expected to be of boolean type.
|
int |
getBooleanParameteri(java.lang.String parameter)
Check a parameter expected to be of boolean type.
|
static Environment |
getGlobalEnvironment()
Access to the global shared environment for the whole JVM.
|
double |
getNumberParameter(java.lang.String parameter)
Get the value of a parameter that is expected to be a number.
|
java.lang.String |
getParameter(java.lang.String parameter)
Access to a parameter in the environment.
|
int |
getParameterCount()
Returns the number of parameters found in the configuration file.
|
java.util.Set<java.lang.String> |
getParametersKeySet()
Set of all parameter names.
|
boolean |
hasParameter(java.lang.String parameter)
True if the given paramter exist.
|
void |
initializeFieldsOf(java.lang.Object object)
Initialize all the fields of the given object whose name correspond to
parameters of this environment.
|
void |
initializeFieldsOf(java.lang.Object object,
java.lang.String... fieldList)
Initialize all the fields of the given object that both appear in the given
field list and whose name correspond to parameters of this environment.
|
boolean |
isLocked()
Is the environment locked?.
|
void |
lockEnvironment(boolean on)
Disallow the addition of new parameters.
|
void |
printParameters()
Print all parameters the stdout.
|
void |
printParameters(java.io.PrintStream out)
Print all parameters to the given stream.
|
void |
readCommandLine(java.lang.String[] args)
Read the parameters from the given command line array.
|
void |
readCommandLine(java.lang.String[] args,
java.util.Collection<java.lang.String> trashcan)
Read the parameters from the given command line array.
|
void |
readParameterFile(java.lang.String fileName)
Read a parameter file.
|
void |
setParameter(java.lang.String parameter,
java.lang.String value)
Set the value of a parameter.
|
java.lang.String |
toString() |
void |
writeParameterFile(java.lang.String fileName)
Save the curent parameters to a file.
|
public static Environment GLOBAL_ENV
getGlobalEnvironment()
static method is called if this field was not
yet initialized by any other mean.getGlobalEnvironment()
public static Environment getGlobalEnvironment()
public boolean isLocked()
lockEnvironment(boolean)
public java.lang.String getParameter(java.lang.String parameter)
parameter
- The parameter name.public boolean hasParameter(java.lang.String parameter)
parameter
- The parameter name.public boolean getBooleanParameter(java.lang.String parameter)
parameter
- The parameter name.getBooleanParameteri(String)
public int getBooleanParameteri(java.lang.String parameter)
parameter
- The parameter name.getBooleanParameter(String)
public double getNumberParameter(java.lang.String parameter)
parameter
- The parameter name.public int getParameterCount()
public java.util.Set<java.lang.String> getParametersKeySet()
public Environment clone()
clone
in class java.lang.Object
public void setParameter(java.lang.String parameter, java.lang.String value)
parameter
- The parameter name.value
- The new parameter value.isLocked()
,
lockEnvironment(boolean)
public void lockEnvironment(boolean on)
on
- If true the environment is locked.public void initializeFieldsOf(java.lang.Object object)
object
- The object to initialize.initializeFieldsOf(Object, String[])
,
initializeFieldsOf(Object, Collection)
public void initializeFieldsOf(java.lang.Object object, java.lang.String... fieldList)
initializeFieldsOf(Object)
method description.object
- The object to initialize.fieldList
- The name of the fields to initialize in the object.initializeFieldsOf(Object)
,
initializeFieldsOf(Object, Collection)
public void printParameters(java.io.PrintStream out)
out
- The output stream to use.public void printParameters()
public java.lang.String toString()
toString
in class java.lang.Object
public void readCommandLine(java.lang.String[] args)
readCommandLine(String[], Collection)
method.args
- The command line.public void readCommandLine(java.lang.String[] args, java.util.Collection<java.lang.String> trashcan)
trashcan
.args
- The command line.trashcan
- Will be filled by the set of unparsed strings (can be null if
these strings can be ignored).public void writeParameterFile(java.lang.String fileName) throws java.io.IOException
fileName
- Name of the file to save the config in.java.io.IOException
- For any output error on the given file name.public void readParameterFile(java.lang.String fileName) throws java.io.IOException
fileName
- Name of the parameter file to read.java.io.IOException
- For any error with the given parameter file name.