HADOOP-8415. Add getDouble() and setDouble() in org.apache.hadoop.conf.Configuration. Contributed by Jan van der Lugt. (harsh)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1342501 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Harsh J 2012-05-25 06:06:01 +00:00
parent 22cb0ec82a
commit 458be39423
3 changed files with 56 additions and 0 deletions

View File

@ -73,6 +73,9 @@ Trunk (unreleased changes)
HADOOP-8367 Improve documentation of declaringClassProtocolName in rpc headers HADOOP-8367 Improve documentation of declaringClassProtocolName in rpc headers
(Sanjay Radia) (Sanjay Radia)
HADOOP-8415. Add getDouble() and setDouble() in
org.apache.hadoop.conf.Configuration (Jan van der Lugt via harsh)
BUG FIXES BUG FIXES
HADOOP-8177. MBeans shouldn't try to register when it fails to create MBeanName. HADOOP-8177. MBeans shouldn't try to register when it fails to create MBeanName.

View File

@ -917,6 +917,7 @@ public float getFloat(String name, float defaultValue) {
return defaultValue; return defaultValue;
return Float.parseFloat(valueString); return Float.parseFloat(valueString);
} }
/** /**
* Set the value of the <code>name</code> property to a <code>float</code>. * Set the value of the <code>name</code> property to a <code>float</code>.
* *
@ -926,6 +927,35 @@ public float getFloat(String name, float defaultValue) {
public void setFloat(String name, float value) { public void setFloat(String name, float value) {
set(name,Float.toString(value)); set(name,Float.toString(value));
} }
/**
* Get the value of the <code>name</code> property as a <code>double</code>.
* If no such property exists, the provided default value is returned,
* or if the specified value is not a valid <code>double</code>,
* then an error is thrown.
*
* @param name property name.
* @param defaultValue default value.
* @throws NumberFormatException when the value is invalid
* @return property value as a <code>double</code>,
* or <code>defaultValue</code>.
*/
public double getDouble(String name, double defaultValue) {
String valueString = getTrimmed(name);
if (valueString == null)
return defaultValue;
return Double.parseDouble(valueString);
}
/**
* Set the value of the <code>name</code> property to a <code>double</code>.
*
* @param name property name.
* @param value property value.
*/
public void setDouble(String name, double value) {
set(name,Double.toString(value));
}
/** /**
* Get the value of the <code>name</code> property as a <code>boolean</code>. * Get the value of the <code>name</code> property as a <code>boolean</code>.

View File

@ -526,6 +526,29 @@ public void testFloatValues() throws IOException {
} }
} }
public void testDoubleValues() throws IOException {
out=new BufferedWriter(new FileWriter(CONFIG));
startConfig();
appendProperty("test.double1", "3.1415");
appendProperty("test.double2", "003.1415");
appendProperty("test.double3", "-3.1415");
appendProperty("test.double4", " -3.1415 ");
appendProperty("test.double5", "xyz-3.1415xyz");
endConfig();
Path fileResource = new Path(CONFIG);
conf.addResource(fileResource);
assertEquals(3.1415, conf.getDouble("test.double1", 0.0));
assertEquals(3.1415, conf.getDouble("test.double2", 0.0));
assertEquals(-3.1415, conf.getDouble("test.double3", 0.0));
assertEquals(-3.1415, conf.getDouble("test.double4", 0.0));
try {
conf.getDouble("test.double5", 0.0);
fail("Property had invalid double value, but was read successfully.");
} catch (NumberFormatException e) {
// pass
}
}
public void testGetClass() throws IOException { public void testGetClass() throws IOException {
out=new BufferedWriter(new FileWriter(CONFIG)); out=new BufferedWriter(new FileWriter(CONFIG));
startConfig(); startConfig();