org.chwf.config
Interface Config

All Known Implementing Classes:
org.chwf.config.ConfigImpl

public interface Config

A class for retrieving configuration data from properties and XML files. See the package documentation for further information.

Author:
Paul Strack

Method Summary
 java.lang.String get(java.lang.String key)
          Gets the named property value.
 java.lang.String get(java.lang.String key, java.lang.String defaultValue)
          Gets the named property value.
 boolean getBoolean(java.lang.String key)
          Gets the named property value as a boolean value.
 boolean getBoolean(java.lang.String key, boolean defaultValue)
          Gets the named property value as a boolean value.
 double getDouble(java.lang.String key)
          Gets the named property value as a double value.
 double getDouble(java.lang.String key, double defaultValue)
          Gets the named property value as a double value.
 int getInt(java.lang.String key)
          Gets the named property value as an int value.
 int getInt(java.lang.String key, int defaultValue)
          Gets the named property value as an int value.
 java.lang.String[] getList(java.lang.String key)
          Gets an array of strings for a property key.
 java.lang.String[] getList(java.lang.String key, java.lang.String[] defaultList)
          As getList(java.lang.String), but with a default.
 java.util.SortedMap getMap(java.lang.String key)
          Gets a sorted map consisting of all entries beginning with the specified key.
 java.util.Map getOrderedMap(java.lang.String key)
          Gets an ordered map consisting of all entries beginning with the specified key.
 

Method Detail

get

public java.lang.String get(java.lang.String key)
                     throws ConfigurationException
Gets the named property value.

Parameters:
key - The search key.
Returns:
The property value.
Throws:
ConfigurationException - If the property is not found.

get

public java.lang.String get(java.lang.String key,
                            java.lang.String defaultValue)
Gets the named property value.

Parameters:
key - The search key.
defaultValue - A default value.
Returns:
The property value or the default value if the property is not found.

getInt

public int getInt(java.lang.String key)
           throws ConfigurationException
Gets the named property value as an int value. String values in the configuration file are parsed using the system locale.

Parameters:
key - The search key.
Returns:
The property value as an int.
Throws:
ConfigurationException - If the property is not found or is not a number.

getInt

public int getInt(java.lang.String key,
                  int defaultValue)
Gets the named property value as an int value. String values in the configuration file are parsed using the system locale.

Parameters:
key - The search key.
defaultValue - A default value.
Returns:
The property value as an int, or the default value if the property is not found or is not a number.

getDouble

public double getDouble(java.lang.String key)
                 throws ConfigurationException
Gets the named property value as a double value. String values in the configuration file are parsed using the system locale.

Parameters:
key - The search key.
Returns:
The property value as a double.
Throws:
ConfigurationException - If the property is not found or is not a number.

getDouble

public double getDouble(java.lang.String key,
                        double defaultValue)
Gets the named property value as a double value. String values in the configuration file are parsed using the system locale.

Parameters:
key - The search key.
defaultValue - A default value.
Returns:
The property value as a double, or the default value if the property is not found or is not a number.

getBoolean

public boolean getBoolean(java.lang.String key)
                   throws ConfigurationException
Gets the named property value as a boolean value.

Parameters:
key - The search key.
Returns:
The property value as a boolean.
Throws:
ConfigurationException - If the property is not found.

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean defaultValue)
Gets the named property value as a boolean value.

Parameters:
key - The search key.
defaultValue - A default value.
Returns:
The property value as a boolean, or the default value if the property is not found.

getList

public java.lang.String[] getList(java.lang.String key)
                           throws ConfigurationException

Gets an array of strings for a property key. This is done in one of two ways:

Note that this method never returns an empty list. If the property is not found, a ConfigurationException is thrown. If you need to allow for the possibility of empty lists, use the values of the getMap() method instead:

 Collection list = config.getMap().values();
Parameters:
key - The search key.
Returns:
The list of property values.
Throws:
ConfigurationException - If the property is not found.

getList

public java.lang.String[] getList(java.lang.String key,
                                  java.lang.String[] defaultList)

As getList(java.lang.String), but with a default.

Parameters:
key - The search key.
defaultList - The default list.
Returns:
The list of property values or the default if not found.

getMap

public java.util.SortedMap getMap(java.lang.String key)

Gets a sorted map consisting of all entries beginning with the specified key. The keys in the sorted map will be the property keys with the search key (and any leading ".") removed. For example, suppose the following values are in the property file:

 map.value.1=Value 1
 map.value.2=Value 2
 map.value.3=Value 3

The method call config.getMap("map.value") will result in a map with 3 values, whose keys are "1", "2" and "3".

Note that the sort order is based on string ordering, not numeric ordering. This means if you have more than 10 values, you will have to number them more carefully if the ordering is important:

 map.value.01=Value 1
 map.value.02=Value 2
 map.value.03=Value 3
 map.value.04=Value 4
 map.value.05=Value 5
 map.value.06=Value 6
 map.value.07=Value 7
 map.value.08=Value 8
 map.value.09=Value 9
 map.value.10=Value 10
 map.value.11=Value 11
Parameters:
key - The search key, which is used as the initial part of property keys.
Returns:
The sorted map, or an empty map if no property keys beginning with the search key.

getOrderedMap

public java.util.Map getOrderedMap(java.lang.String key)

Gets an ordered map consisting of all entries beginning with the specified key. The keys in the sorted map will be the property keys with the search key (and any leading ".") removed. If the configuration data is in an XML file, the data retains the sequential ordering of the elements in the XML file. For example, suppose the XML data were as follows:

 <map.values>
   <value name="C">Item 1</data>
   <value name="B">Item 2</data>
   <value name="A">Item 3</data>
 <map.values>

The method call config.getOrderedMap("map.values") will result in a map with 3 values, whose keys are "C", "B" and "A" in that order. Compare this to the getMap() method, which sorts keys in alphabetical order rather than in sequential order.

This method only functions correctly for configuration data in XML files, because property files are not ordered. Using this method for data in property files will result in keys sorted in pseudo-random order.

Parameters:
key - The search key, which is used as the initial part of property keys.
Returns:
The ordered map, or an empty map if no property keys beginning with the search key.


Copyright © 2002-2004, Paul Strack. All Rights Reserved.