|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.string.Properties
This class extends and simplified the application propertie file interface provided by java.util.Properties. It will also search the data path for a properties file to load. All the setters return the string value being set. This can be useful for additional recording (as in logs). It is possible to flag a property so that code can be informed if the property changes.
Properties myCodes = new Properties( "myCodes"); // find and load myCodes.properties.txt
String p1 = myCodes.get( "p1key");
String p2 = myCodes.get( "p2key", "defaultP2"); // return default if key not found
int p3 = myCodes.check( "p3key", 3); // throw an exception if p3 is not an integer
int p4 = myCodes.get( "p4key", 4); // will return default if p4 is not an integer
int p5 = myCodes.check( "p5key", true); // throw an exception if p3 is not a known boolean
int p6 = myCodes.get( "p6key", false); // will return default if p4 is not a known boolean
myCodes.set( "p7key = p7value");
myCodes.set( "p8key", "p8value");
myCodes.set( "p8key", 8);
myCodes.set( "p9key", true);
if (! myCodes.load( "myCodes")) Application.error( "Could not find 'myCodes.properties.txt'");
// Iterate through the properties
for (String key = properties.first(); key != null; key = properties.next())
Log.message( key + "=" + properties.get( key));
// Use properties to store messages with variable parameters
catch (Exception exception)
{
String message = "Can't scrape field "+baseEntry.className+"."+key;
String stamp = Log.error( message, exception);
problems.set( key, "{0} (see log reference {1})", new String[] { message, stamp});
}
// Properties can be dumped in human readable form in key: value format, 1 per line):
System.out( myCodes.toString);
| Nested Class Summary | |
static interface |
Properties.Change
Give this interface to the rare class that needs to be informed of when a property or properties change. |
| Field Summary | |
Map |
map
map stores properties - and exposed for direct access. |
| Constructor Summary | |
Properties()
Initialiser Properties. |
|
Properties(Properties preload)
Initialiser Properties. |
|
Properties(String fileName)
Initialiser - Properties. |
|
| Method Summary | |
boolean |
check(String key,
boolean defaultValue)
Retrieve a boolean property, with default. |
int |
check(String key,
int defaultValue)
test numeric and retrieve an integer property, with default. |
void |
deregister(String key,
Properties.Change with)
Remove registration of a class with a variable change. |
String |
first()
Used to iterate through all the properties. |
String |
get(String key)
Method get - retrieve a string property. |
boolean |
get(String key,
boolean defaultValue)
retrieve a boolean property, with default. |
int |
get(String key,
int defaultValue)
retrieve an integer property, with default. |
int[] |
get(String key,
int[] defaultValues)
Retrieve a comma separated array of integers. |
long |
get(String key,
long defaultValue)
retrieve an integer property, with default. |
Set |
get(String key,
Set set)
Retrieve a comma separated array of strings. |
String |
get(String key,
String defaultValue)
retrieve a string property, with default. |
String[] |
get(String key,
String[] defaultValues)
Retrieve a comma separated array of strings. |
String[] |
getList(String key)
Properties with the same key are maintained in a list. |
boolean |
load(InputStream stream,
String name)
Load properties from a stream. |
void |
load(Properties other)
Load properties from another properties object. |
boolean |
load(String file)
Load properties from a file. |
String |
next()
Used to iterate through all the properties. |
void |
preload(Map preLoadedMap)
If you have a name/value pair string map from another source (say an MVC), it can be laoded here. |
void |
register(String key,
Properties.Change with)
Call to register an instance to be told if a property changes. |
void |
save(String userConfigurationFileName)
Save a changed configuration to disk. |
String |
set(String property)
Set a string property from a name/value pair. |
String |
set(String key,
boolean value)
Set a boolean property. |
String |
set(String key,
long value)
Set an integer property. |
String |
set(String key,
String value)
Set a string property. |
int |
size()
Return the number of property entries that currently exist. |
String |
toString()
Override toString to provide a better representation. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
public Map map
map stores properties - and exposed for direct access.
| Constructor Detail |
public Properties()
Properties config = new Properties();
Objectpublic Properties(String fileName)
Properties properties = new Properties( propertiesFileName);
fileName - look for and load properties from file fileName.properties.txt on data pathpublic Properties(Properties preload)
Properties properties = new Properties( System.getProperties());
preload - properties to preload with.| Method Detail |
public String get(String key)
String fileSep = properties.get( "file.separator");
key - property key to look for
public String[] getList(String key)
key - property name
public String get(String key,
String defaultValue)
String path = properties.get( "mypath", "/temp");
key - property key to look fordefaultValue - default to use if property does not exist
public String[] get(String key,
String[] defaultValues)
String defaults[] = {"one","two","three"};
defaults = properties.get( "list", defaults);
key - property key to look fordefaultValues - default to use if property does not exist
public int[] get(String key,
int[] defaultValues)
int defaults[] = {1,10,100};
defaults = properties.get( "list", defaults);
key - property key to look fordefaultValues - default to use if property does not exist
NumberFormatException
public Set get(String key,
Set set)
Set defaults = new HashSet(); defaults = properties.get( "list", defaults);
key - property key to look forset - default to use if property does not exist
public int check(String key,
int defaultValue)
throws Convert.Exception
try { int days = properties.check( "backup.removal", 28); }
key - property key to look fordefaultValue - default to use if property does not exist
Convert.Exception
public int get(String key,
int defaultValue)
int days = properties.get( "backup.removal", 28);
key - property key to look fordefaultValue - default to use if property does not exist
public long get(String key,
long defaultValue)
int days = properties.get( "backup.removal", 28);
key - property key to look fordefaultValue - default to use if property does not exist
public boolean check(String key,
boolean defaultValue)
throws Convert.Exception
try { boolean allowed = properties.check( "edit_permission", false); }
key - property key to look fordefaultValue - default to use if property does not exist
Convert.ExceptionConvert.toBoolean(String)
public boolean get(String key,
boolean defaultValue)
boolean allowed = properties.check( "edit_permission", false);
key - property key to look fordefaultValue - default to use if property does not exist
Convert.toBoolean(String)public String set(String property)
property - as in key=value
public String set(String key,
String value)
key - of property to setvalue - of property to set
public String set(String key,
long value)
key - of property to setvalue - of property to set
public String set(String key,
boolean value)
key - of property to setvalue - of property to set
public boolean load(String file)
load( "myfile") || load( "default");
file - path and name of file to open
public boolean load(InputStream stream,
String name)
load( reader.getInputStream());
stream - input stream to read properties fromname - path and name of open stream - used for display only
public void load(Properties other)
other - properties file to copy in.public void preload(Map preLoadedMap)
preLoadedMap - Map of String name/value pairs.public int size()
public void save(String userConfigurationFileName)
throws FileNotFoundException
userConfigurationFileName - being the name of the user configuration file.
FileNotFoundExceptionpublic String first()
next()public String next()
first()public String toString()
public void register(String key,
Properties.Change with)
key - Key to register to be informed if it is changed.with - Class with Change interface to inform.
public void deregister(String key,
Properties.Change with)
key - Key registered for Change that we want to remove.with - Class with Change interface to deregister.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||