diff --git a/common/src/java/org/apache/hive/common/util/HiveStringUtils.java b/common/src/java/org/apache/hive/common/util/HiveStringUtils.java index 6d28396..4f31357 100644 --- a/common/src/java/org/apache/hive/common/util/HiveStringUtils.java +++ b/common/src/java/org/apache/hive/common/util/HiveStringUtils.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -39,13 +40,14 @@ import java.util.Properties; import java.util.StringTokenizer; +import com.google.common.base.Function; import com.google.common.collect.Interner; import com.google.common.collect.Interners; +import com.google.common.collect.Iterables; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.common.classification.InterfaceAudience; import org.apache.hadoop.hive.common.classification.InterfaceStability; import org.apache.hadoop.io.Text; -import org.apache.hadoop.util.StringUtils; /** * HiveStringUtils @@ -77,6 +79,17 @@ STRING_INTERNER = Interners.newWeakInterner(); } + private static final Function INTERN_FUNC = new Function() { + public String apply(String str) { return intern(str); } + }; + + private static final Function, String[]> INTERN_FUNC_ENTRY = + new Function, String[]>() { + public String[] apply(Map.Entry entry) { + return new String[]{intern(entry.getKey()), intern(entry.getValue())}; + } + }; + /** * Return the internalized string, or null if the given string is null. * @param str The string to intern @@ -95,14 +108,16 @@ public static String intern(String str) { * @return An identical list with its strings interned. */ public static List intern(List list) { - if(list == null) { - return null; - } - List newList = new ArrayList(list.size()); - for(String str : list) { - newList.add(intern(str)); + if (list == null || list.isEmpty()) { + return list; } - return newList; + Iterable transform = Iterables.transform(list, INTERN_FUNC); + String[] array = Iterables.toArray(transform, String.class); + + list.clear(); + Collections.addAll(list, array); + + return list; } /** @@ -111,14 +126,18 @@ public static String intern(String str) { * @return An identical map with its strings interned. */ public static Map intern(Map map) { - if(map == null) { - return null; + if (map == null || map.isEmpty()) { + return map; } - Map newMap = new HashMap(map.size()); - for(Map.Entry entry : map.entrySet()) { - newMap.put(intern(entry.getKey()), intern(entry.getValue())); + Iterable transform = Iterables.transform(map.entrySet(), INTERN_FUNC_ENTRY); + String[][] array = Iterables.toArray(transform, String[].class); + + map.clear(); + for (String[] interned : array) { + map.put(interned[0], interned[1]); } - return newMap; + + return map; } /** @@ -306,7 +325,7 @@ public static String uriToString(URI[] uris){ * * Given a finish and start time in long milliseconds, returns a * String in the format Xhrs, Ymins, Z sec, for the time difference between two times. - * If finish time comes before start time then negative valeus of X, Y and Z wil return. + * If finish time comes before start time then negative values of X, Y and Z wil return. * * @param finishTime finish time * @param startTime start time @@ -350,7 +369,7 @@ public static String formatTime(long timeDiff){ * If finish time is 0, empty string is returned, if start time is 0 * then difference is not appended to return value. * @param dateFormat date format to use - * @param finishTime fnish time + * @param finishTime finish time * @param startTime start time * @return formatted value. */ @@ -368,8 +387,8 @@ public static String getFormattedTimeWithDiff(DateFormat dateFormat, /** * Returns an arraylist of strings. - * @param str the comma seperated string values - * @return the arraylist of the comma seperated string values + * @param str the comma separated string values + * @return the arraylist of the comma separated string values */ public static String[] getStrings(String str){ Collection values = getStringCollection(str); @@ -381,7 +400,7 @@ public static String getFormattedTimeWithDiff(DateFormat dateFormat, /** * Returns a collection of strings. - * @param str comma seperated string values + * @param str comma separated string values * @return an ArrayList of string values */ public static Collection getStringCollection(String str){