com.anji.util
Class Properties

java.lang.Object
  extended byjava.util.Dictionary
      extended byjava.util.Hashtable
          extended byjava.util.Properties
              extended bycom.anji.util.Properties
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class Properties
extends java.util.Properties

Beefed up version of java.util.Properties used to manage configuration parameters. Adds convenience functions for converting String properties to various data types and logs all default and configured property settings.

Author:
Philip Tucker
See Also:
Serialized Form

Field Summary
static java.lang.String CLASS_SUFFIX
          made public for unit tests
 
Fields inherited from class java.util.Properties
defaults
 
Constructor Summary
Properties()
          default constructor
Properties(java.util.Properties values)
          Initialize with values.
Properties(java.lang.String resource)
          initialize properties from file; also, initializes Logger
 
Method Summary
 boolean getBooleanProperty(java.lang.String key)
           
 boolean getBooleanProperty(java.lang.String key, boolean defaultVal)
           
 java.lang.Class getClassProperty(java.lang.String key)
           
 java.lang.Class getClassProperty(java.lang.String key, java.lang.Class defaultVal)
           
 java.io.File getDirProperty(java.lang.String key)
           
 double getDoubleProperty(java.lang.String key)
           
 double getDoubleProperty(java.lang.String key, double defaultVal)
           
 java.io.FileInputStream getFileInputProperty(java.lang.String key)
           
 java.io.FileOutputStream getFileOutputProperty(java.lang.String key)
           
 float getFloatProperty(java.lang.String key)
           
 float getFloatProperty(java.lang.String key, float defaultVal)
           
 int getIntProperty(java.lang.String key)
           
 int getIntProperty(java.lang.String key, int defaultVal)
           
 java.util.Set getKeysForPattern(java.lang.String pattern)
          Returns property keys matching regular expression pattern.
 long getLongProperty(java.lang.String key)
           
 long getLongProperty(java.lang.String key, long defaultVal)
           
 java.lang.String getName()
           
 java.util.Set getPropertiesForPattern(java.lang.String pattern)
          Returns property values for keys matching regular expression pattern.
 java.lang.String getProperty(java.lang.String key)
           
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultVal)
           
 java.io.InputStream getResourceProperty(java.lang.String key)
           
 short getShortProperty(java.lang.String key)
           
 short getShortProperty(java.lang.String key, short defaultVal)
           
 Properties getSubProperties(java.lang.String prefix)
          Return properties filtered for a particular sub-component, based on prefix.
static double[][] loadArrayFromFile(java.io.InputStream in)
           
static boolean[][] loadBooleanArrayFromFile(java.io.InputStream in)
          TODO - better class for this? combine w/ getProperty
static boolean[] loadBooleanRowFromString(java.lang.String line)
          TODO - better class for this? combine w/ getProperty
 void loadFromResource(java.lang.String resource)
          loads properties from file; also, initializes Logger
 void loadFromResourceWithoutLogging(java.lang.String resource)
          loads properties from file
protected static double[] loadRowFromString(java.lang.String line)
          TODO - better class for this? combine w/ getProperty
 java.util.List newObjectListProperty(java.lang.String key)
          Throws IllegalArgumentException exception if key not present.
 java.util.List newObjectListProperty(java.lang.String key, java.util.List defaultList)
          Return list of objects initialized from properties.
 java.lang.Object newObjectProperty(java.lang.Class aClass)
           
 java.lang.Object newObjectProperty(java.lang.String key)
           
protected  void setName(java.lang.String aName)
           
 java.lang.Object singletonObjectProperty(java.lang.Class aClass)
           
 java.lang.Object singletonObjectProperty(java.lang.String key)
           
 java.lang.String toString()
           
 
Methods inherited from class java.util.Properties
list, list, load, propertyNames, save, setProperty, store
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

CLASS_SUFFIX

public static final java.lang.String CLASS_SUFFIX
made public for unit tests

See Also:
Constant Field Values
Constructor Detail

Properties

public Properties()
default constructor


Properties

public Properties(java.util.Properties values)
Initialize with values.

Parameters:
values -

Properties

public Properties(java.lang.String resource)
           throws java.io.IOException
initialize properties from file; also, initializes Logger

Parameters:
resource - a file in the application classpath
Throws:
java.io.IOException
Method Detail

loadFromResource

public void loadFromResource(java.lang.String resource)
                      throws java.io.IOException
loads properties from file; also, initializes Logger

Parameters:
resource - a file in the application classpath
Throws:
java.io.IOException

loadFromResourceWithoutLogging

public void loadFromResourceWithoutLogging(java.lang.String resource)
                                    throws java.io.IOException
loads properties from file

Parameters:
resource - a file in the application classpath
Throws:
java.io.IOException

getProperty

public java.lang.String getProperty(java.lang.String key)
Parameters:
key -
Returns:
String property value corresponding to key; throws runtime exception if key not found

getBooleanProperty

public boolean getBooleanProperty(java.lang.String key)
Parameters:
key -
Returns:
boolean property value corresponding to key; throws runtime exception if key not found

getLongProperty

public long getLongProperty(java.lang.String key)
Parameters:
key -
Returns:
long property value corresponding to key; throws runtime exception if key not found or invalid long

getIntProperty

public int getIntProperty(java.lang.String key)
Parameters:
key -
Returns:
int property value corresponding to key

getShortProperty

public short getShortProperty(java.lang.String key)
Parameters:
key -
Returns:
short property value corresponding to key

getFloatProperty

public float getFloatProperty(java.lang.String key)
Parameters:
key -
Returns:
float property value corresponding to key; throws runtime exception if key not found or invalid float

getDoubleProperty

public double getDoubleProperty(java.lang.String key)
Parameters:
key -
Returns:
double property value corresponding to key; throws runtime exception if key not found or invalid double

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultVal)
Parameters:
key -
defaultVal -
Returns:
String property value corresponding to key., or defaultVal if key not found

getBooleanProperty

public boolean getBooleanProperty(java.lang.String key,
                                  boolean defaultVal)
Parameters:
key -
defaultVal -
Returns:
boolean property value corresponding to key., or defaultVal if key not found

getLongProperty

public long getLongProperty(java.lang.String key,
                            long defaultVal)
Parameters:
key -
defaultVal -
Returns:
long property value corresponding to key., or defaultVal if key not found

getIntProperty

public int getIntProperty(java.lang.String key,
                          int defaultVal)
Parameters:
key -
defaultVal -
Returns:
int property value corresponding to key., or defaultVal if key not found

getShortProperty

public short getShortProperty(java.lang.String key,
                              short defaultVal)
Parameters:
key -
defaultVal -
Returns:
short property value corresponding to key., or defaultVal if key not found

getFloatProperty

public float getFloatProperty(java.lang.String key,
                              float defaultVal)
Parameters:
key -
defaultVal -
Returns:
float property value corresponding to key., or defaultVal if key not found

getDoubleProperty

public double getDoubleProperty(java.lang.String key,
                                double defaultVal)
Parameters:
key -
defaultVal -
Returns:
double property value corresponding to key., or defaultVal if key not found

getKeysForPattern

public java.util.Set getKeysForPattern(java.lang.String pattern)
Returns property keys matching regular expression pattern.

Parameters:
pattern - interpreted as regular expression
Returns:
Set contains String objects

getPropertiesForPattern

public java.util.Set getPropertiesForPattern(java.lang.String pattern)
Returns property values for keys matching regular expression pattern.

Parameters:
pattern - interpreted as regular expression
Returns:
Set contains String objects

getSubProperties

public Properties getSubProperties(java.lang.String prefix)
Return properties filtered for a particular sub-component, based on prefix.

Parameters:
prefix -
Returns:
Properties object properties contained in this object, but for those cases where the property starts with prefix, the property overrides another property of the same key; e.g., if prefix is "abc", and there is an "abc.foo=bar" property and a "foo=bat" property, this method returns a properties object containing "foo=bar"

getDirProperty

public java.io.File getDirProperty(java.lang.String key)
Parameters:
key -
Returns:
File directory property value corresponding to key; throws runtime exception if key not found

getFileInputProperty

public java.io.FileInputStream getFileInputProperty(java.lang.String key)
                                             throws java.io.FileNotFoundException
Parameters:
key -
Returns:
FileInputStream property value corresponding to key; throws runtime exception if key not found
Throws:
java.io.FileNotFoundException

getFileOutputProperty

public java.io.FileOutputStream getFileOutputProperty(java.lang.String key)
                                               throws java.io.FileNotFoundException
Parameters:
key -
Returns:
FileOutputStream property value corresponding to key; throws runtime exception if key not found
Throws:
java.io.FileNotFoundException

getResourceProperty

public java.io.InputStream getResourceProperty(java.lang.String key)
Parameters:
key -
Returns:
InputStream resource (i.e., searches classpath for it) property value corresponding to key; throws runtime exception if key not found

singletonObjectProperty

public java.lang.Object singletonObjectProperty(java.lang.String key)
Parameters:
key -
Returns:
Object value corresponding to key; throws runtime exception if key not found

toString

public java.lang.String toString()
See Also:
Object.toString()

singletonObjectProperty

public java.lang.Object singletonObjectProperty(java.lang.Class aClass)
Parameters:
aClass -
Returns:
Object singleton instance of cl initialized with properties if it is Configurable

newObjectProperty

public java.lang.Object newObjectProperty(java.lang.String key)
Parameters:
key - key+CLASS_SUFFIX references property with fully qualified class name
Returns:
Object value corresponding to key; throws runtime exception if key not found

newObjectProperty

public java.lang.Object newObjectProperty(java.lang.Class aClass)
Parameters:
aClass -
Returns:
Object new instance of class cl, initialized with properties if Configurable not found

getClassProperty

public java.lang.Class getClassProperty(java.lang.String key)
Parameters:
key -
Returns:
Class corresponding to full package specification in property value associated with key
Throws:
java.lang.IllegalArgumentException - if key not found

getClassProperty

public java.lang.Class getClassProperty(java.lang.String key,
                                        java.lang.Class defaultVal)
Parameters:
key -
defaultVal -
Returns:
Class corresponding to full package specification in property value associated with key; returns defaultVal if key not found

loadArrayFromFile

public static double[][] loadArrayFromFile(java.io.InputStream in)
                                    throws java.io.FileNotFoundException,
                                           java.io.IOException
Parameters:
in -
Returns:
double[][] matrix generated by interpreting contents of in as a 2-dimensional array of values; columns separated by semi-colons, rows separated by "\n"
Throws:
java.io.FileNotFoundException
java.io.IOException

loadBooleanArrayFromFile

public static boolean[][] loadBooleanArrayFromFile(java.io.InputStream in)
                                            throws java.io.FileNotFoundException,
                                                   java.io.IOException
TODO - better class for this? combine w/ getProperty

Parameters:
in -
Returns:
boolean[][] matrix generated by interpreting contents of in as a 2-dimensional array of values; columns separated by semi-colons, rows separated by "\n"
Throws:
java.io.FileNotFoundException
java.io.IOException

loadRowFromString

protected static double[] loadRowFromString(java.lang.String line)
TODO - better class for this? combine w/ getProperty

Parameters:
line -
Returns:
double[] generated by interpreting contents of line as a list of values separated by semi-colons

loadBooleanRowFromString

public static boolean[] loadBooleanRowFromString(java.lang.String line)
TODO - better class for this? combine w/ getProperty

Parameters:
line -
Returns:
boolean[] generated by interpreting contents of line as a list of values separated by semi-colons

newObjectListProperty

public java.util.List newObjectListProperty(java.lang.String key,
                                            java.util.List defaultList)
Return list of objects initialized from properties. Each object has a name given in a comma-separated list in property value for key. Each object, if it is Configurable, is initialized with properties prefixed with its name, plus remainder of properties. The class of each object is determined by a property named object name +CLASS_SUFFIX

Parameters:
key -
defaultList - returns this if no property key exists
Returns:
List contains initialized objects

newObjectListProperty

public java.util.List newObjectListProperty(java.lang.String key)
Throws IllegalArgumentException exception if key not present.

Parameters:
key -
Returns:
List contains initialized objects
See Also:
newObjectListProperty(String, List)

getName

public java.lang.String getName()
Returns:
name of this property set

setName

protected void setName(java.lang.String aName)
Parameters:
aName - name of this property set