Author: szetszwo
Date: Tue Jun 30 21:45:00 2009
New Revision: 789973
URL: http://svn.apache.org/viewvc?rev=789973&view=rev
Log:
HADOOP-2366. Support trimmed strings in Configuration. Contributed by Michele
Catasta
Modified:
hadoop/common/trunk/CHANGES.txt
hadoop/common/trunk/src/java/org/apache/hadoop/conf/Configuration.java
hadoop/common/trunk/src/java/org/apache/hadoop/util/StringUtils.java
hadoop/common/trunk/src/test/core/org/apache/hadoop/util/TestStringUtils.java
Modified: hadoop/common/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=789973&r1=789972&r2=789973&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Tue Jun 30 21:45:00 2009
@@ -468,6 +468,9 @@
than the max of the current length and the proposed length to improve
performance reading large values. (thushara wijeratna via cdouglas)
+ HADOOP-2366. Support trimmed strings in Configuration. (Michele Catasta
+ via szetszwo)
+
OPTIMIZATIONS
HADOOP-5595. NameNode does not need to run a replicator to choose a
Modified: hadoop/common/trunk/src/java/org/apache/hadoop/conf/Configuration.java
URL:
http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/conf/Configuration.java?rev=789973&r1=789972&r2=789973&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/conf/Configuration.java
(original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/conf/Configuration.java Tue
Jun 30 21:45:00 2009
@@ -31,6 +31,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
@@ -739,6 +740,56 @@
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
Modified: hadoop/common/trunk/src/java/org/apache/hadoop/util/StringUtils.java
URL:
http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/util/StringUtils.java?rev=789973&r1=789972&r2=789973&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/util/StringUtils.java
(original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/util/StringUtils.java Tue
Jun 30 21:45:00 2009
@@ -319,6 +319,29 @@
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 String COMMA_STR = ",";
final public static char ESCAPE_CHAR = '\\';
Modified:
hadoop/common/trunk/src/test/core/org/apache/hadoop/util/TestStringUtils.java
URL:
http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/util/TestStringUtils.java?rev=789973&r1=789972&r2=789973&view=diff
==============================================================================
---
hadoop/common/trunk/src/test/core/org/apache/hadoop/util/TestStringUtils.java
(original)
+++
hadoop/common/trunk/src/test/core/org/apache/hadoop/util/TestStringUtils.java
Tue Jun 30 21:45:00 2009
@@ -22,6 +22,7 @@
import java.util.List;
import junit.framework.TestCase;
+import static org.junit.Assert.assertArrayEquals;
public class TestStringUtils extends TestCase {
final private static String NULL_STR = null;
@@ -132,4 +133,24 @@
assertEquals("a:b", StringUtils.join(":", s.subList(0, 2)));
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));
+ }
}