org.chwf.config
Class PolymorphicConfig

java.lang.Object
  |
  +--org.chwf.config.ConfigImpl
        |
        +--org.chwf.config.PolymorphicConfig
All Implemented Interfaces:
Config, RawConfig

public class PolymorphicConfig
extends org.chwf.config.ConfigImpl

A subclass of Config that adds the superclass configuration to the configuration search path. Other than the factory method, it may be used in every respect like the Config class.

Author:
Paul Strack

Constructor Summary
PolymorphicConfig()
           
 
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.
static Config getConfig(java.lang.Class contextClass)
          Factory method for PolymorphicConfig class.
static Config getConfig(java.lang.Class contextClass, java.util.Locale locale)
          Factory method for PolymorphicConfig class.
 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 ConfigImpl.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.
 java.util.Map getRawData()
          Get the raw configuration data.
 java.lang.String getRawValue(java.lang.String key)
          Get raw data value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PolymorphicConfig

public PolymorphicConfig()
Method Detail

getConfig

public static Config getConfig(java.lang.Class contextClass)
                        throws ConfigurationException
Factory method for PolymorphicConfig class.

Parameters:
contextClass - The context class for the configuration.
Returns:
The Config object for the given class.
Throws:
ConfigurationException - For errors in loading configuration data.

getConfig

public static Config getConfig(java.lang.Class contextClass,
                               java.util.Locale locale)
                        throws ConfigurationException
Factory method for PolymorphicConfig class.

Parameters:
contextClass - The context class for the configuration.
locale - The locale.
Returns:
The Config object for the given class.
Throws:
ConfigurationException - For errors in loading configuration data.

get

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

Specified by:
get in interface Config
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.

Specified by:
get in interface Config
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.

Specified by:
getInt in interface Config
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.

Specified by:
getInt in interface Config
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.

Specified by:
getDouble in interface Config
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.

Specified by:
getDouble in interface Config
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.

Specified by:
getBoolean in interface Config
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.

Specified by:
getBoolean in interface Config
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();
Specified by:
getList in interface Config
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 ConfigImpl.getList(java.lang.String), but with a default.

Specified by:
getList in interface Config
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
Specified by:
getMap in interface Config
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.

Specified by:
getOrderedMap in interface Config
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.

getRawData

public java.util.Map getRawData()
Get the raw configuration data. Parent configuration data is not included.
Specified by:
getRawData in interface RawConfig
Returns:
The list of keys for the current Config object.

getRawValue

public java.lang.String getRawValue(java.lang.String key)
Get raw data value.
Specified by:
getRawValue in interface RawConfig
Parameters:
key - The search key.
Returns:
The value or null if it does not exist.


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