Forráskód Böngészése

HADOOP-3355. Enhances Configuration class to accept hex numbers for getInt and getLong. Contributed by Amareshwari Sriramadasu.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@656270 13f79535-47bb-0310-9956-ffa450edef68
Devaraj Das 17 éve
szülő
commit
1056dac2aa

+ 3 - 0
CHANGES.txt

@@ -123,6 +123,9 @@ Trunk (unreleased changes)
     HADOOP-3332. Reduces the amount of logging in Reducer's shuffle phase.
     (Devaraj Das)
 
+    HADOOP-3355. Enhances Configuration class to accept hex numbers for getInt
+    and getLong. (Amareshwari Sriramadasu via ddas)
+
   OPTIMIZATIONS
 
     HADOOP-3274. The default constructor of BytesWritable creates empty 

+ 26 - 0
src/java/org/apache/hadoop/conf/Configuration.java

@@ -332,6 +332,10 @@ public class Configuration implements Iterable<Map.Entry<String,String>> {
     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;
@@ -364,12 +368,34 @@ public class Configuration implements Iterable<Map.Entry<String,String>> {
     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;
     }
   }
 
+  private String getHexDigits(String value) {
+    boolean negative = false;
+    String str = value;
+    String hexString = null;
+    if (value.startsWith("-")) {
+      negative = true;
+      str = value.substring(1);
+    }
+    if (str.startsWith("0x") || str.startsWith("0X")) {
+      hexString = str.substring(2);
+      if (negative) {
+        hexString = "-" + hexString;
+      }
+      return hexString;
+    }
+    return null;
+  }
+  
   /** 
    * Set the value of the <code>name</code> property to a <code>long</code>.
    * 

+ 35 - 0
src/test/org/apache/hadoop/conf/TestConfiguration.java

@@ -258,6 +258,41 @@ public class TestConfiguration extends TestCase {
     assertEquals(true, range.isIncluded(100000000));
   }
 
+  public void testHexValues() throws IOException{
+    out=new BufferedWriter(new FileWriter(CONFIG));
+    startConfig();
+    appendProperty("test.hex1", "0x10");
+    appendProperty("test.hex2", "0xF");
+    appendProperty("test.hex3", "-0x10");
+    endConfig();
+    Path fileResource = new Path(CONFIG);
+    conf.addResource(fileResource);
+    assertEquals(16, conf.getInt("test.hex1", 0));
+    assertEquals(16, conf.getLong("test.hex1", 0));
+    assertEquals(15, conf.getInt("test.hex2", 0));
+    assertEquals(15, conf.getLong("test.hex2", 0));
+    assertEquals(-16, conf.getInt("test.hex3", 0));
+    assertEquals(-16, conf.getLong("test.hex3", 0));
+
+  }
+
+  public void testIntegerValues() throws IOException{
+    out=new BufferedWriter(new FileWriter(CONFIG));
+    startConfig();
+    appendProperty("test.int1", "20");
+    appendProperty("test.int2", "020");
+    appendProperty("test.int3", "-20");
+    endConfig();
+    Path fileResource = new Path(CONFIG);
+    conf.addResource(fileResource);
+    assertEquals(20, conf.getInt("test.int1", 0));
+    assertEquals(20, conf.getLong("test.int1", 0));
+    assertEquals(20, conf.getInt("test.int2", 0));
+    assertEquals(20, conf.getLong("test.int2", 0));
+    assertEquals(-20, conf.getInt("test.int3", 0));
+    assertEquals(-20, conf.getLong("test.int3", 0));
+  }
+	  
   public static void main(String[] argv) throws Exception {
     junit.textui.TestRunner.main(new String[]{
       TestConfiguration.class.getName()