epugh 2003/03/11 09:38:54
Modified: configuration/src/java/org/apache/commons/configuration
CompositeConfiguration.java
Log:
Fixed getting a config value when you have a defaultValue provided.
Revision Changes Path
1.2 +200 -72
jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java
Index: CompositeConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CompositeConfiguration.java 28 Feb 2003 20:23:41 -0000 1.1
+++ CompositeConfiguration.java 11 Mar 2003 17:38:54 -0000 1.2
@@ -67,32 +67,39 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
* @version $Id$
*/
-public class CompositeConfiguration implements Configuration {
+public class CompositeConfiguration implements Configuration
+{
private ArrayList configList = new ArrayList();
/**
* Creates an empty CompositeConfiguration object which can then
* be added some other Configuration files
*/
- public CompositeConfiguration() {
+ public CompositeConfiguration()
+ {
}
- public void addConfiguration(Configuration config) {
- if (!configList.contains(config)) {
+ public void addConfiguration(Configuration config)
+ {
+ if (!configList.contains(config))
+ {
configList.add(config);
}
}
- public void removeConfiguration(Configuration config) {
+ public void removeConfiguration(Configuration config)
+ {
configList.remove(config);
}
- public int getNumberOfConfigurations() {
+ public int getNumberOfConfigurations()
+ {
return configList.size();
}
-
- public void clear(){
+
+ public void clear()
+ {
configList.clear();
}
@@ -102,7 +109,8 @@
* @param key The Key to add the property to.
* @param token The Value to add.
*/
- public void addProperty(String key, Object token) {
+ public void addProperty(String key, Object token)
+ {
throw new Error("This operation is not supported");
}
@@ -112,11 +120,14 @@
*
* @return An Iterator.
*/
- public Iterator getKeys() {
+ public Iterator getKeys()
+ {
HashSet keys = new HashSet();
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
- for (Iterator j = config.getKeys(); j.hasNext();) {
+ for (Iterator j = config.getKeys(); j.hasNext();)
+ {
String key = (String) j.next();
keys.add(key);
}
@@ -130,11 +141,14 @@
*
* @return An Iterator.
*/
- public Iterator getKeys(String key) {
+ public Iterator getKeys(String key)
+ {
HashSet keys = new HashSet();
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
- for (Iterator j = config.getKeys(key); j.hasNext();) {
+ for (Iterator j = config.getKeys(key); j.hasNext();)
+ {
String newKey = (String) j.next();
keys.add(newKey);
}
@@ -153,15 +167,19 @@
* malformed (does not contain an equals sign).
* @see #getProperties(String, Properties)
*/
- public Properties getProperties(String key) {
+ public Properties getProperties(String key)
+ {
return getFirstMatchingConfig(key).getProperties(key);
}
- public boolean isEmpty() {
+ public boolean isEmpty()
+ {
boolean isEmpty = true;
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
- if (!config.isEmpty()) {
+ if (!config.isEmpty())
+ {
return false;
}
}
@@ -176,7 +194,8 @@
* @return value as object. Will return user value if exists,
* if not then default value if exists, otherwise null
*/
- public Object getProperty(String key) {
+ public Object getProperty(String key)
+ {
return getFirstMatchingConfig(key).getProperty(key);
}
@@ -188,7 +207,8 @@
* @param key
* @param value
*/
- public void setProperty(String key, Object value) {
+ public void setProperty(String key, Object value)
+ {
getFirstMatchingConfig(key).setProperty(key, value);
}
@@ -197,17 +217,21 @@
*
* @param key the key to remove along with corresponding value.
*/
- public void clearProperty(String key) {
+ public void clearProperty(String key)
+ {
throw new Error("This operation is not supported");
}
/**
* check if the configuration contains the key
*/
- public boolean containsKey(String key) {
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ public boolean containsKey(String key)
+ {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
- if (config.containsKey(key)) {
+ if (config.containsKey(key))
+ {
return true;
}
}
@@ -222,10 +246,12 @@
*
* @param prefix
*/
- public Configuration subset(String prefix) {
+ public Configuration subset(String prefix)
+ {
CompositeConfiguration subsetConfig = new CompositeConfiguration();
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
subsetConfig.addConfiguration(config.subset(prefix));
}
@@ -245,7 +271,8 @@
* @ exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public float getFloat(String key) {
+ public float getFloat(String key)
+ {
return getFirstMatchingConfig(key).getFloat(key);
}
@@ -259,7 +286,8 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Boolean.
*/
- public boolean getBoolean(String key) {
+ public boolean getBoolean(String key)
+ {
return getFirstMatchingConfig(key).getBoolean(key);
}
@@ -272,8 +300,9 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Boolean.
*/
- public boolean getBoolean(String key, boolean defaultValue) {
- return getFirstMatchingConfig(key).getBoolean(key, defaultValue);
+ public boolean getBoolean(String key, boolean defaultValue)
+ {
+ return getBoolean(key, new Boolean(defaultValue).booleanValue());
}
@@ -287,8 +316,17 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Boolean.
*/
- public Boolean getBoolean(String key, Boolean defaultValue) {
- return getFirstMatchingConfig(key).getBoolean(key, defaultValue);
+ public Boolean getBoolean(String key, Boolean defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getBoolean(key,
defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -303,7 +341,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public byte getByte(String key) {
+ public byte getByte(String key)
+ {
return getFirstMatchingConfig(key).getByte(key);
}
@@ -318,8 +357,9 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public byte getByte(String key, byte defaultValue) {
- return getFirstMatchingConfig(key).getByte(key, defaultValue);
+ public byte getByte(String key, byte defaultValue)
+ {
+ return getByte(key, new Byte(defaultValue).byteValue());
}
/**
@@ -334,8 +374,17 @@
* @exception NumberFormatException is thrown if the value mapped by the key
* has not a valid number format.
*/
- public Byte getByte(String key, Byte defaultValue) {
- return getFirstMatchingConfig(key).getByte(key, defaultValue);
+ public Byte getByte(String key, Byte defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getByte(key, defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -350,7 +399,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public double getDouble(String key) {
+ public double getDouble(String key)
+ {
return getFirstMatchingConfig(key).getDouble(key);
}
@@ -365,7 +415,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public double getDouble(String key, double defaultValue) {
+ public double getDouble(String key, double defaultValue)
+ {
return getDouble(key, new Double(defaultValue)).doubleValue();
}
@@ -381,8 +432,17 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public Double getDouble(String key, Double defaultValue) {
- return getFirstMatchingConfig(key).getDouble(key, defaultValue);
+ public Double getDouble(String key, Double defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getDouble(key,
defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -396,8 +456,9 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public float getFloat(String key, float defaultValue) {
- return getFirstMatchingConfig(key).getFloat(key, defaultValue);
+ public float getFloat(String key, float defaultValue)
+ {
+ return getFloat(key, new Float(defaultValue).floatValue());
}
/**
@@ -412,8 +473,17 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public Float getFloat(String key, Float defaultValue) {
- return getFirstMatchingConfig(key).getFloat(key, defaultValue);
+ public Float getFloat(String key, Float defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getFloat(key, defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -428,7 +498,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public int getInt(String key) {
+ public int getInt(String key)
+ {
return getFirstMatchingConfig(key).getInt(key);
}
@@ -443,8 +514,9 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public int getInt(String key, int defaultValue) {
- return getFirstMatchingConfig(key).getInt(key, defaultValue);
+ public int getInt(String key, int defaultValue)
+ {
+ return getInt(key, new Integer(defaultValue).intValue());
}
/**
@@ -459,8 +531,17 @@
* @exception NumberFormatException is thrown if the value mapped by the key
* has not a valid number format.
*/
- public Integer getInteger(String key, Integer defaultValue) {
- return getFirstMatchingConfig(key).getInteger(key, defaultValue);
+ public Integer getInteger(String key, Integer defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getInteger(key,
defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -475,7 +556,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public long getLong(String key) {
+ public long getLong(String key)
+ {
return getFirstMatchingConfig(key).getLong(key);
}
@@ -490,8 +572,9 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public long getLong(String key, long defaultValue) {
- return getFirstMatchingConfig(key).getLong(key, defaultValue);
+ public long getLong(String key, long defaultValue)
+ {
+ return getLong(key, new Long(defaultValue).longValue());
}
/**
@@ -506,8 +589,17 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public Long getLong(String key, Long defaultValue) {
- return getFirstMatchingConfig(key).getLong(key, defaultValue);
+ public Long getLong(String key, Long defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getLong(key, defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -522,7 +614,8 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public short getShort(String key) {
+ public short getShort(String key)
+ {
return getFirstMatchingConfig(key).getShort(key);
}
@@ -537,8 +630,10 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public short getShort(String key, short defaultValue) {
- return getFirstMatchingConfig(key).getShort(key, defaultValue);
+ public short getShort(String key, short defaultValue)
+ {
+ return getShort(key, new Short(defaultValue).shortValue());
+
}
/**
@@ -553,8 +648,16 @@
* @exception NumberFormatException is thrown if the value mapped
* by the key has not a valid number format.
*/
- public Short getShort(String key, Short defaultValue) {
- return getFirstMatchingConfig(key).getShort(key, defaultValue);
+ public Short getShort(String key, Short defaultValue)
+ {
+ try
+ {
+ return getFirstMatchingConfig(key).getShort(key, defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
/**
@@ -565,7 +668,8 @@
* @exception ClassCastException is thrown if the key maps to an object that
* is not a String.
*/
- public String getString(String key) {
+ public String getString(String key)
+ {
return getFirstMatchingConfig(key).getString(key);
}
@@ -578,8 +682,18 @@
* @exception ClassCastException is thrown if the key maps to an object that
* is not a String.
*/
- public String getString(String key, String defaultValue) {
- return getFirstMatchingConfig(key).getString(key, defaultValue);
+ public String getString(String key, String defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getString(key,
defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+
+ }
}
/**
@@ -591,7 +705,8 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a String/Vector of Strings.
*/
- public String[] getStringArray(String key) {
+ public String[] getStringArray(String key)
+ {
return getFirstMatchingConfig(key).getStringArray(key);
}
@@ -603,7 +718,8 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
- public Vector getVector(String key) {
+ public Vector getVector(String key)
+ {
return getFirstMatchingConfig(key).getVector(key);
}
@@ -616,14 +732,26 @@
* @exception ClassCastException is thrown if the key maps to an
* object that is not a Vector.
*/
- public Vector getVector(String key, Vector defaultValue) {
- return getFirstMatchingConfig(key).getVector(key, defaultValue);
+ public Vector getVector(String key, Vector defaultValue)
+ {
+ try
+ {
+
+ return getFirstMatchingConfig(key).getVector(key,
defaultValue);
+ }
+ catch (NoSuchElementException nsee)
+ {
+ return defaultValue;
+ }
}
- private Configuration getFirstMatchingConfig(String key) {
- for (ListIterator i = configList.listIterator(); i.hasNext();) {
+ private Configuration getFirstMatchingConfig(String key)
+ {
+ for (ListIterator i = configList.listIterator(); i.hasNext();)
+ {
Configuration config = (Configuration) i.next();
- if (config.containsKey(key)) {
+ if (config.containsKey(key))
+ {
return config;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]