hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject svn commit: r1147971 - in /hadoop/common/trunk/common: CHANGES.txt src/java/org/apache/hadoop/conf/Configuration.java src/test/core/org/apache/hadoop/conf/TestConfiguration.java
Date Mon, 18 Jul 2011 17:03:21 GMT
Author: eli
Date: Mon Jul 18 17:03:20 2011
New Revision: 1147971

URL: http://svn.apache.org/viewvc?rev=1147971&view=rev
Log:
HADOOP-2081. Configuration getInt, getLong, and getFloat replace invalid numbers with the
default value. Contributed by Harsh J

Modified:
    hadoop/common/trunk/common/CHANGES.txt
    hadoop/common/trunk/common/src/java/org/apache/hadoop/conf/Configuration.java
    hadoop/common/trunk/common/src/test/core/org/apache/hadoop/conf/TestConfiguration.java

Modified: hadoop/common/trunk/common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/CHANGES.txt?rev=1147971&r1=1147970&r2=1147971&view=diff
==============================================================================
--- hadoop/common/trunk/common/CHANGES.txt (original)
+++ hadoop/common/trunk/common/CHANGES.txt Mon Jul 18 17:03:20 2011
@@ -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

Modified: hadoop/common/trunk/common/src/java/org/apache/hadoop/conf/Configuration.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/java/org/apache/hadoop/conf/Configuration.java?rev=1147971&r1=1147970&r2=1147971&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/java/org/apache/hadoop/conf/Configuration.java (original)
+++ hadoop/common/trunk/common/src/java/org/apache/hadoop/conf/Configuration.java Mon Jul
18 17:03:20 2011
@@ -682,11 +682,13 @@ public class Configuration implements It
   /** 
    * 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 It
     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 It
 
   /** 
    * 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 It
     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 It
 
   /** 
    * 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 It
     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>.

Modified: hadoop/common/trunk/common/src/test/core/org/apache/hadoop/conf/TestConfiguration.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/test/core/org/apache/hadoop/conf/TestConfiguration.java?rev=1147971&r1=1147970&r2=1147971&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/test/core/org/apache/hadoop/conf/TestConfiguration.java
(original)
+++ hadoop/common/trunk/common/src/test/core/org/apache/hadoop/conf/TestConfiguration.java
Mon Jul 18 17:03:20 2011
@@ -367,6 +367,8 @@ public class TestConfiguration extends T
     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 T
     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 T
     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 T
     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 T
     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 T
     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 {



Mime
View raw message