HADOOP-2081. Configuration getInt, getLong, and getFloat replace invalid numbers with the default value. Contributed by Harsh J
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1147971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
845889384b
commit
d3b2f68050
|
@ -402,6 +402,9 @@ Trunk (unreleased changes)
|
|||
HADOOP-7471. The saveVersion.sh script sometimes fails to extract SVN URL.
|
||||
(Alejandro Abdelnur via eli)
|
||||
|
||||
HADOOP-2081. Configuration getInt, getLong, and getFloat replace
|
||||
invalid numbers with the default value. (Harsh J via eli)
|
||||
|
||||
Release 0.22.0 - Unreleased
|
||||
|
||||
INCOMPATIBLE CHANGES
|
||||
|
|
|
@ -682,11 +682,13 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
/**
|
||||
* Get the value of the <code>name</code> property as an <code>int</code>.
|
||||
*
|
||||
* If no such property exists, or if the specified value is not a valid
|
||||
* <code>int</code>, then <code>defaultValue</code> is returned.
|
||||
* If no such property exists, the provided default value is returned,
|
||||
* or if the specified value is not a valid <code>int</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 an <code>int</code>,
|
||||
* or <code>defaultValue</code>.
|
||||
*/
|
||||
|
@ -694,15 +696,11 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
String valueString = getTrimmed(name);
|
||||
if (valueString == null)
|
||||
return defaultValue;
|
||||
try {
|
||||
String hexString = getHexDigits(valueString);
|
||||
if (hexString != null) {
|
||||
return Integer.parseInt(hexString, 16);
|
||||
}
|
||||
return Integer.parseInt(valueString);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
String hexString = getHexDigits(valueString);
|
||||
if (hexString != null) {
|
||||
return Integer.parseInt(hexString, 16);
|
||||
}
|
||||
return Integer.parseInt(valueString);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -718,11 +716,13 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
|
||||
/**
|
||||
* Get the value of the <code>name</code> property as a <code>long</code>.
|
||||
* If no such property is specified, or if the specified value is not a valid
|
||||
* <code>long</code>, then <code>defaultValue</code> is returned.
|
||||
* If no such property exists, the provided default value is returned,
|
||||
* or if the specified value is not a valid <code>long</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>long</code>,
|
||||
* or <code>defaultValue</code>.
|
||||
*/
|
||||
|
@ -730,15 +730,11 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
String valueString = getTrimmed(name);
|
||||
if (valueString == null)
|
||||
return defaultValue;
|
||||
try {
|
||||
String hexString = getHexDigits(valueString);
|
||||
if (hexString != null) {
|
||||
return Long.parseLong(hexString, 16);
|
||||
}
|
||||
return Long.parseLong(valueString);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
String hexString = getHexDigits(valueString);
|
||||
if (hexString != null) {
|
||||
return Long.parseLong(hexString, 16);
|
||||
}
|
||||
return Long.parseLong(valueString);
|
||||
}
|
||||
|
||||
private String getHexDigits(String value) {
|
||||
|
@ -771,11 +767,13 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
|
||||
/**
|
||||
* Get the value of the <code>name</code> property as a <code>float</code>.
|
||||
* If no such property is specified, or if the specified value is not a valid
|
||||
* <code>float</code>, then <code>defaultValue</code> is returned.
|
||||
*
|
||||
* If no such property exists, the provided default value is returned,
|
||||
* or if the specified value is not a valid <code>float</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>float</code>,
|
||||
* or <code>defaultValue</code>.
|
||||
*/
|
||||
|
@ -783,11 +781,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
String valueString = getTrimmed(name);
|
||||
if (valueString == null)
|
||||
return defaultValue;
|
||||
try {
|
||||
return Float.parseFloat(valueString);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Float.parseFloat(valueString);
|
||||
}
|
||||
/**
|
||||
* Set the value of the <code>name</code> property to a <code>float</code>.
|
||||
|
|
|
@ -367,6 +367,8 @@ public class TestConfiguration extends TestCase {
|
|||
appendProperty("test.hex1", "0x10");
|
||||
appendProperty("test.hex2", "0xF");
|
||||
appendProperty("test.hex3", "-0x10");
|
||||
// Invalid?
|
||||
appendProperty("test.hex4", "-0x10xyz");
|
||||
endConfig();
|
||||
Path fileResource = new Path(CONFIG);
|
||||
conf.addResource(fileResource);
|
||||
|
@ -376,7 +378,18 @@ public class TestConfiguration extends TestCase {
|
|||
assertEquals(15, conf.getLong("test.hex2", 0));
|
||||
assertEquals(-16, conf.getInt("test.hex3", 0));
|
||||
assertEquals(-16, conf.getLong("test.hex3", 0));
|
||||
|
||||
try {
|
||||
conf.getLong("test.hex4", 0);
|
||||
fail("Property had invalid long value, but was read successfully.");
|
||||
} catch (NumberFormatException e) {
|
||||
// pass
|
||||
}
|
||||
try {
|
||||
conf.getInt("test.hex4", 0);
|
||||
fail("Property had invalid int value, but was read successfully.");
|
||||
} catch (NumberFormatException e) {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
|
||||
public void testIntegerValues() throws IOException{
|
||||
|
@ -386,6 +399,7 @@ public class TestConfiguration extends TestCase {
|
|||
appendProperty("test.int2", "020");
|
||||
appendProperty("test.int3", "-20");
|
||||
appendProperty("test.int4", " -20 ");
|
||||
appendProperty("test.int5", " -20xyz ");
|
||||
endConfig();
|
||||
Path fileResource = new Path(CONFIG);
|
||||
conf.addResource(fileResource);
|
||||
|
@ -397,6 +411,12 @@ public class TestConfiguration extends TestCase {
|
|||
assertEquals(-20, conf.getLong("test.int3", 0));
|
||||
assertEquals(-20, conf.getInt("test.int4", 0));
|
||||
assertEquals(-20, conf.getLong("test.int4", 0));
|
||||
try {
|
||||
conf.getInt("test.int5", 0);
|
||||
fail("Property had invalid int value, but was read successfully.");
|
||||
} catch (NumberFormatException e) {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
|
||||
public void testBooleanValues() throws IOException {
|
||||
|
@ -424,6 +444,7 @@ public class TestConfiguration extends TestCase {
|
|||
appendProperty("test.float2", "003.1415");
|
||||
appendProperty("test.float3", "-3.1415");
|
||||
appendProperty("test.float4", " -3.1415 ");
|
||||
appendProperty("test.float5", "xyz-3.1415xyz");
|
||||
endConfig();
|
||||
Path fileResource = new Path(CONFIG);
|
||||
conf.addResource(fileResource);
|
||||
|
@ -431,6 +452,12 @@ public class TestConfiguration extends TestCase {
|
|||
assertEquals(3.1415f, conf.getFloat("test.float2", 0.0f));
|
||||
assertEquals(-3.1415f, conf.getFloat("test.float3", 0.0f));
|
||||
assertEquals(-3.1415f, conf.getFloat("test.float4", 0.0f));
|
||||
try {
|
||||
conf.getFloat("test.float5", 0.0f);
|
||||
fail("Property had invalid float value, but was read successfully.");
|
||||
} catch (NumberFormatException e) {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetClass() throws IOException {
|
||||
|
|
Loading…
Reference in New Issue