You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by dd...@apache.org on 2008/05/14 15:18:39 UTC

svn commit: r656270 - in /hadoop/core/trunk: CHANGES.txt src/java/org/apache/hadoop/conf/Configuration.java src/test/org/apache/hadoop/conf/TestConfiguration.java

Author: ddas
Date: Wed May 14 06:18:39 2008
New Revision: 656270

URL: http://svn.apache.org/viewvc?rev=656270&view=rev
Log:
HADOOP-3355. Enhances Configuration class to accept hex numbers for getInt and getLong. Contributed by Amareshwari Sriramadasu.

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

Modified: hadoop/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/CHANGES.txt?rev=656270&r1=656269&r2=656270&view=diff
==============================================================================
--- hadoop/core/trunk/CHANGES.txt (original)
+++ hadoop/core/trunk/CHANGES.txt Wed May 14 06:18:39 2008
@@ -123,6 +123,9 @@
     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 

Modified: hadoop/core/trunk/src/java/org/apache/hadoop/conf/Configuration.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/java/org/apache/hadoop/conf/Configuration.java?rev=656270&r1=656269&r2=656270&view=diff
==============================================================================
--- hadoop/core/trunk/src/java/org/apache/hadoop/conf/Configuration.java (original)
+++ hadoop/core/trunk/src/java/org/apache/hadoop/conf/Configuration.java Wed May 14 06:18:39 2008
@@ -332,6 +332,10 @@
     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 @@
     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>.
    * 

Modified: hadoop/core/trunk/src/test/org/apache/hadoop/conf/TestConfiguration.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/test/org/apache/hadoop/conf/TestConfiguration.java?rev=656270&r1=656269&r2=656270&view=diff
==============================================================================
--- hadoop/core/trunk/src/test/org/apache/hadoop/conf/TestConfiguration.java (original)
+++ hadoop/core/trunk/src/test/org/apache/hadoop/conf/TestConfiguration.java Wed May 14 06:18:39 2008
@@ -258,6 +258,41 @@
     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()