Browse Source

HADOOP-2366. Support trimmed strings in Configuration. Contributed by Michele Catasta

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@789973 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze 16 years ago
parent
commit
a13237975d

+ 3 - 0
CHANGES.txt

@@ -468,6 +468,9 @@ Trunk (unreleased changes)
     than the max of the current length and the proposed length to improve
     than the max of the current length and the proposed length to improve
     performance reading large values. (thushara wijeratna via cdouglas)
     performance reading large values. (thushara wijeratna via cdouglas)
 
 
+    HADOOP-2366. Support trimmed strings in Configuration.  (Michele Catasta
+    via szetszwo)
+
   OPTIMIZATIONS
   OPTIMIZATIONS
 
 
     HADOOP-5595. NameNode does not need to run a replicator to choose a
     HADOOP-5595. NameNode does not need to run a replicator to choose a

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

@@ -31,6 +31,7 @@ import java.io.Reader;
 import java.net.URL;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
@@ -739,6 +740,56 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
       return StringUtils.getStrings(valueString);
       return StringUtils.getStrings(valueString);
     }
     }
   }
   }
+  
+  /** 
+   * Get the comma delimited values of the <code>name</code> property as 
+   * a collection of <code>String</code>s, trimmed of the leading and trailing whitespace.  
+   * If no such property is specified then empty <code>Collection</code> is returned.
+   *
+   * @param name property name.
+   * @return property value as a collection of <code>String</code>s, or empty <code>Collection</code> 
+   */
+  public Collection<String> getTrimmedStringCollection(String name) {
+    String valueString = get(name);
+    if (null == valueString) {
+      Collection<String> empty = Collections.emptyList();
+      return empty;
+    }
+    return StringUtils.getTrimmedStringCollection(valueString);
+  }
+  
+  /** 
+   * Get the comma delimited values of the <code>name</code> property as 
+   * an array of <code>String</code>s, trimmed of the leading and trailing whitespace.
+   * If no such property is specified then an empty array is returned.
+   * 
+   * @param name property name.
+   * @return property value as an array of trimmed <code>String</code>s, 
+   *         or empty array. 
+   */
+  public String[] getTrimmedStrings(String name) {
+    String valueString = get(name);
+    return StringUtils.getTrimmedStrings(valueString);
+  }
+
+  /** 
+   * Get the comma delimited values of the <code>name</code> property as 
+   * an array of <code>String</code>s, trimmed of the leading and trailing whitespace.
+   * If no such property is specified then default value is returned.
+   * 
+   * @param name property name.
+   * @param defaultValue The default value
+   * @return property value as an array of trimmed <code>String</code>s, 
+   *         or default value. 
+   */
+  public String[] getTrimmedStrings(String name, String... defaultValue) {
+    String valueString = get(name);
+    if (null == valueString) {
+      return defaultValue;
+    } else {
+      return StringUtils.getTrimmedStrings(valueString);
+    }
+  }
 
 
   /** 
   /** 
    * Set the array of string values for the <code>name</code> property as 
    * Set the array of string values for the <code>name</code> property as 

+ 23 - 0
src/java/org/apache/hadoop/util/StringUtils.java

@@ -319,6 +319,29 @@ public class StringUtils {
     return values;
     return values;
   }
   }
 
 
+  /**
+   * Splits a comma separated value <code>String</code>, trimming leading and trailing whitespace on each value.
+   * @param str a comma separated <String> with values
+   * @return a <code>Collection</code> of <code>String</code> values
+   */
+  public static Collection<String> getTrimmedStringCollection(String str){
+    return Arrays.asList(getTrimmedStrings(str));
+  }
+  
+  /**
+   * Splits a comma separated value <code>String</code>, trimming leading and trailing whitespace on each value.
+   * @param str a comma separated <String> with values
+   * @return an array of <code>String</code> values
+   */
+  public static String[] getTrimmedStrings(String str){
+    if (null == str || "".equals(str.trim())) {
+      return emptyStringArray;
+    }
+
+    return str.trim().split("\\s*,\\s*");
+  }
+
+  final public static String[] emptyStringArray = {};
   final public static char COMMA = ',';
   final public static char COMMA = ',';
   final public static String COMMA_STR = ",";
   final public static String COMMA_STR = ",";
   final public static char ESCAPE_CHAR = '\\';
   final public static char ESCAPE_CHAR = '\\';

+ 21 - 0
src/test/core/org/apache/hadoop/util/TestStringUtils.java

@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 
 
 import junit.framework.TestCase;
 import junit.framework.TestCase;
+import static org.junit.Assert.assertArrayEquals;
 
 
 public class TestStringUtils extends TestCase {
 public class TestStringUtils extends TestCase {
   final private static String NULL_STR = null;
   final private static String NULL_STR = null;
@@ -132,4 +133,24 @@ public class TestStringUtils extends TestCase {
     assertEquals("a:b", StringUtils.join(":", s.subList(0, 2)));
     assertEquals("a:b", StringUtils.join(":", s.subList(0, 2)));
     assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3)));
     assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3)));
   }
   }
+  
+  public void testGetTrimmedStrings() throws Exception {
+    String compactDirList = "/spindle1/hdfs,/spindle2/hdfs,/spindle3/hdfs";
+    String spacedDirList = "/spindle1/hdfs, /spindle2/hdfs, /spindle3/hdfs";
+    String pathologicalDirList1 = " /spindle1/hdfs  ,  /spindle2/hdfs ,/spindle3/hdfs ";
+    String pathologicalDirList2 = " /spindle1/hdfs  ,  /spindle2/hdfs ,/spindle3/hdfs , ";
+    String emptyList1 = "";
+    String emptyList2 = "   ";
+    
+    String[] expectedArray = {"/spindle1/hdfs", "/spindle2/hdfs", "/spindle3/hdfs"};
+    String[] emptyArray = {};
+    
+    assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(compactDirList));
+    assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(spacedDirList));
+    assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(pathologicalDirList1));
+    assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(pathologicalDirList2));
+    
+    assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList1));
+    assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2));
+  } 
 }
 }