Skip to content

Commit a132379

Browse files
committed
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
1 parent 80bc92a commit a132379

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ Trunk (unreleased changes)
468468
than the max of the current length and the proposed length to improve
469469
performance reading large values. (thushara wijeratna via cdouglas)
470470

471+
HADOOP-2366. Support trimmed strings in Configuration. (Michele Catasta
472+
via szetszwo)
473+
471474
OPTIMIZATIONS
472475

473476
HADOOP-5595. NameNode does not need to run a replicator to choose a

src/java/org/apache/hadoop/conf/Configuration.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.util.ArrayList;
3333
import java.util.Collection;
34+
import java.util.Collections;
3435
import java.util.Enumeration;
3536
import java.util.HashMap;
3637
import java.util.HashSet;
@@ -739,6 +740,56 @@ public String[] getStrings(String name, String... defaultValue) {
739740
return StringUtils.getStrings(valueString);
740741
}
741742
}
743+
744+
/**
745+
* Get the comma delimited values of the <code>name</code> property as
746+
* a collection of <code>String</code>s, trimmed of the leading and trailing whitespace.
747+
* If no such property is specified then empty <code>Collection</code> is returned.
748+
*
749+
* @param name property name.
750+
* @return property value as a collection of <code>String</code>s, or empty <code>Collection</code>
751+
*/
752+
public Collection<String> getTrimmedStringCollection(String name) {
753+
String valueString = get(name);
754+
if (null == valueString) {
755+
Collection<String> empty = Collections.emptyList();
756+
return empty;
757+
}
758+
return StringUtils.getTrimmedStringCollection(valueString);
759+
}
760+
761+
/**
762+
* Get the comma delimited values of the <code>name</code> property as
763+
* an array of <code>String</code>s, trimmed of the leading and trailing whitespace.
764+
* If no such property is specified then an empty array is returned.
765+
*
766+
* @param name property name.
767+
* @return property value as an array of trimmed <code>String</code>s,
768+
* or empty array.
769+
*/
770+
public String[] getTrimmedStrings(String name) {
771+
String valueString = get(name);
772+
return StringUtils.getTrimmedStrings(valueString);
773+
}
774+
775+
/**
776+
* Get the comma delimited values of the <code>name</code> property as
777+
* an array of <code>String</code>s, trimmed of the leading and trailing whitespace.
778+
* If no such property is specified then default value is returned.
779+
*
780+
* @param name property name.
781+
* @param defaultValue The default value
782+
* @return property value as an array of trimmed <code>String</code>s,
783+
* or default value.
784+
*/
785+
public String[] getTrimmedStrings(String name, String... defaultValue) {
786+
String valueString = get(name);
787+
if (null == valueString) {
788+
return defaultValue;
789+
} else {
790+
return StringUtils.getTrimmedStrings(valueString);
791+
}
792+
}
742793

743794
/**
744795
* Set the array of string values for the <code>name</code> property as

src/java/org/apache/hadoop/util/StringUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,29 @@ public static Collection<String> getStringCollection(String str){
319319
return values;
320320
}
321321

322+
/**
323+
* Splits a comma separated value <code>String</code>, trimming leading and trailing whitespace on each value.
324+
* @param str a comma separated <String> with values
325+
* @return a <code>Collection</code> of <code>String</code> values
326+
*/
327+
public static Collection<String> getTrimmedStringCollection(String str){
328+
return Arrays.asList(getTrimmedStrings(str));
329+
}
330+
331+
/**
332+
* Splits a comma separated value <code>String</code>, trimming leading and trailing whitespace on each value.
333+
* @param str a comma separated <String> with values
334+
* @return an array of <code>String</code> values
335+
*/
336+
public static String[] getTrimmedStrings(String str){
337+
if (null == str || "".equals(str.trim())) {
338+
return emptyStringArray;
339+
}
340+
341+
return str.trim().split("\\s*,\\s*");
342+
}
343+
344+
final public static String[] emptyStringArray = {};
322345
final public static char COMMA = ',';
323346
final public static String COMMA_STR = ",";
324347
final public static char ESCAPE_CHAR = '\\';

src/test/core/org/apache/hadoop/util/TestStringUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424
import junit.framework.TestCase;
25+
import static org.junit.Assert.assertArrayEquals;
2526

2627
public class TestStringUtils extends TestCase {
2728
final private static String NULL_STR = null;
@@ -132,4 +133,24 @@ public void testJoin() {
132133
assertEquals("a:b", StringUtils.join(":", s.subList(0, 2)));
133134
assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3)));
134135
}
136+
137+
public void testGetTrimmedStrings() throws Exception {
138+
String compactDirList = "/spindle1/hdfs,/spindle2/hdfs,/spindle3/hdfs";
139+
String spacedDirList = "/spindle1/hdfs, /spindle2/hdfs, /spindle3/hdfs";
140+
String pathologicalDirList1 = " /spindle1/hdfs , /spindle2/hdfs ,/spindle3/hdfs ";
141+
String pathologicalDirList2 = " /spindle1/hdfs , /spindle2/hdfs ,/spindle3/hdfs , ";
142+
String emptyList1 = "";
143+
String emptyList2 = " ";
144+
145+
String[] expectedArray = {"/spindle1/hdfs", "/spindle2/hdfs", "/spindle3/hdfs"};
146+
String[] emptyArray = {};
147+
148+
assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(compactDirList));
149+
assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(spacedDirList));
150+
assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(pathologicalDirList1));
151+
assertArrayEquals(expectedArray, StringUtils.getTrimmedStrings(pathologicalDirList2));
152+
153+
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList1));
154+
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2));
155+
}
135156
}

0 commit comments

Comments
 (0)