HADOOP-6657. Add a capitalization method to StringUtils for MAPREDUCE-1545

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@934619 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Steve Loughran 2010-04-15 22:06:52 +00:00
parent a0ef69df26
commit 6f89a64657
3 changed files with 54 additions and 0 deletions

View File

@ -70,6 +70,9 @@ Trunk (unreleased changes)
HADOOP-6580. UGI should contain authentication method. (jnp via boryas)
HADOOP-6657. Add a capitalization method to StringUtils for MAPREDUCE-1545.
(Luke Lu via Steve Loughran)
IMPROVEMENTS
HADOOP-6283. Improve the exception messages thrown by

View File

@ -720,4 +720,20 @@ public static String join(CharSequence separator, Iterable<String> strings) {
}
return sb.toString();
}
/**
* Convert SOME_STUFF to SomeStuff
*
* @param s input string
* @return camelized string
*/
public static String camelize(String s) {
StringBuilder sb = new StringBuilder();
String[] words = split(s.toLowerCase(Locale.US), ESCAPE_CHAR, '_');
for (String word : words)
sb.append(org.apache.commons.lang.StringUtils.capitalize(word));
return sb.toString();
}
}

View File

@ -153,4 +153,39 @@ public void testGetTrimmedStrings() throws Exception {
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList1));
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2));
}
public void testCamelize() {
// common use cases
assertEquals("Map", StringUtils.camelize("MAP"));
assertEquals("JobSetup", StringUtils.camelize("JOB_SETUP"));
assertEquals("SomeStuff", StringUtils.camelize("some_stuff"));
// sanity checks for ascii alphabet against unexpected locale issues.
assertEquals("Aa", StringUtils.camelize("aA"));
assertEquals("Bb", StringUtils.camelize("bB"));
assertEquals("Cc", StringUtils.camelize("cC"));
assertEquals("Dd", StringUtils.camelize("dD"));
assertEquals("Ee", StringUtils.camelize("eE"));
assertEquals("Ff", StringUtils.camelize("fF"));
assertEquals("Gg", StringUtils.camelize("gG"));
assertEquals("Hh", StringUtils.camelize("hH"));
assertEquals("Ii", StringUtils.camelize("iI"));
assertEquals("Jj", StringUtils.camelize("jJ"));
assertEquals("Kk", StringUtils.camelize("kK"));
assertEquals("Ll", StringUtils.camelize("lL"));
assertEquals("Mm", StringUtils.camelize("mM"));
assertEquals("Nn", StringUtils.camelize("nN"));
assertEquals("Oo", StringUtils.camelize("oO"));
assertEquals("Pp", StringUtils.camelize("pP"));
assertEquals("Qq", StringUtils.camelize("qQ"));
assertEquals("Rr", StringUtils.camelize("rR"));
assertEquals("Ss", StringUtils.camelize("sS"));
assertEquals("Tt", StringUtils.camelize("tT"));
assertEquals("Uu", StringUtils.camelize("uU"));
assertEquals("Vv", StringUtils.camelize("vV"));
assertEquals("Ww", StringUtils.camelize("wW"));
assertEquals("Xx", StringUtils.camelize("xX"));
assertEquals("Yy", StringUtils.camelize("yY"));
assertEquals("Zz", StringUtils.camelize("zZ"));
}
}