From a13237975d02b3db913b95845d4b8d6d22f2bac7 Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Tue, 30 Jun 2009 21:45:00 +0000 Subject: [PATCH] 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 --- CHANGES.txt | 3 ++ .../org/apache/hadoop/conf/Configuration.java | 51 +++++++++++++++++++ .../org/apache/hadoop/util/StringUtils.java | 23 +++++++++ .../apache/hadoop/util/TestStringUtils.java | 21 ++++++++ 4 files changed, 98 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index d4ff7b1b73..36cf7ab5ea 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 diff --git a/src/java/org/apache/hadoop/conf/Configuration.java b/src/java/org/apache/hadoop/conf/Configuration.java index e1381f3bb6..16958186ee 100644 --- a/src/java/org/apache/hadoop/conf/Configuration.java +++ b/src/java/org/apache/hadoop/conf/Configuration.java @@ -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 @@ public String[] getStrings(String name, String... defaultValue) { return StringUtils.getStrings(valueString); } } + + /** + * Get the comma delimited values of the name property as + * a collection of Strings, trimmed of the leading and trailing whitespace. + * If no such property is specified then empty Collection is returned. + * + * @param name property name. + * @return property value as a collection of Strings, or empty Collection + */ + public Collection getTrimmedStringCollection(String name) { + String valueString = get(name); + if (null == valueString) { + Collection empty = Collections.emptyList(); + return empty; + } + return StringUtils.getTrimmedStringCollection(valueString); + } + + /** + * Get the comma delimited values of the name property as + * an array of Strings, 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 Strings, + * or empty array. + */ + public String[] getTrimmedStrings(String name) { + String valueString = get(name); + return StringUtils.getTrimmedStrings(valueString); + } + + /** + * Get the comma delimited values of the name property as + * an array of Strings, 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 Strings, + * 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 name property as diff --git a/src/java/org/apache/hadoop/util/StringUtils.java b/src/java/org/apache/hadoop/util/StringUtils.java index 2c1a5fe37b..12d5ae5841 100644 --- a/src/java/org/apache/hadoop/util/StringUtils.java +++ b/src/java/org/apache/hadoop/util/StringUtils.java @@ -319,6 +319,29 @@ public static Collection getStringCollection(String str){ return values; } + /** + * Splits a comma separated value String, trimming leading and trailing whitespace on each value. + * @param str a comma separated with values + * @return a Collection of String values + */ + public static Collection getTrimmedStringCollection(String str){ + return Arrays.asList(getTrimmedStrings(str)); + } + + /** + * Splits a comma separated value String, trimming leading and trailing whitespace on each value. + * @param str a comma separated with values + * @return an array of String 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 = '\\'; diff --git a/src/test/core/org/apache/hadoop/util/TestStringUtils.java b/src/test/core/org/apache/hadoop/util/TestStringUtils.java index 7aa6a7a333..ae66b3f9b8 100644 --- a/src/test/core/org/apache/hadoop/util/TestStringUtils.java +++ b/src/test/core/org/apache/hadoop/util/TestStringUtils.java @@ -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)); + } }