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
This commit is contained in:
Tsz-wo Sze 2009-06-30 21:45:00 +00:00
parent 80bc92ac35
commit a13237975d
4 changed files with 98 additions and 0 deletions

View File

@ -468,6 +468,9 @@ Trunk (unreleased changes)
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

View File

@ -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;
@ -740,6 +741,56 @@ public String[] getStrings(String name, String... defaultValue) {
}
}
/**
* 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
* as comma delimited values.

View File

@ -319,6 +319,29 @@ public static Collection<String> getStringCollection(String str){
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 = '\\';

View File

@ -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 @@ public void testJoin() {
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));
}
}