一、前言
关于alibaba的dubbo源码包com.alibaba.dubbo.common.utils.StringUtils字符串工具类,对字符串/字符及其集合数组进行类型判断isInteger/isJavaIdentifier/isNumeric、是否相等isEquals、判空isEmpty、包含contain、分割split、联合join等常见处理,详情参见源码说明。
二、源码说明
package com.alibaba.dubbo.common.utils;@b@@b@import com.alibaba.dubbo.common.Constants;@b@import com.alibaba.dubbo.common.io.UnsafeStringWriter;@b@import com.alibaba.fastjson.JSON;@b@import com.alibaba.dubbo.common.logger.Logger;@b@import com.alibaba.dubbo.common.logger.LoggerFactory;@b@@b@import java.io.PrintWriter;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.TreeMap;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@/**@b@ * StringUtils@b@ */@b@@b@public final class StringUtils {@b@@b@ public static final String[] EMPTY_STRING_ARRAY = new String[0];@b@ private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);@b@ private static final Pattern KVP_PATTERN = Pattern.compile("([_.a-zA-Z0-9][-_.a-zA-Z0-9]*)[=](.*)"); //key value pair pattern.@b@@b@ private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");@b@@b@ private StringUtils() {@b@ }@b@@b@ public static boolean isBlank(String str) {@b@ if (str == null || str.length() == 0)@b@ return true;@b@ return false;@b@ }@b@@b@ /**@b@ * is empty string.@b@ *@b@ * @param str source string.@b@ * @return is empty.@b@ */@b@ public static boolean isEmpty(String str) {@b@ if (str == null || str.length() == 0)@b@ return true;@b@ return false;@b@ }@b@@b@ /**@b@ * is not empty string.@b@ *@b@ * @param str source string.@b@ * @return is not empty.@b@ */@b@ public static boolean isNotEmpty(String str) {@b@ return str != null && str.length() > 0;@b@ }@b@@b@ /**@b@ * @param s1@b@ * @param s2@b@ * @return equals@b@ */@b@ public static boolean isEquals(String s1, String s2) {@b@ if (s1 == null && s2 == null)@b@ return true;@b@ if (s1 == null || s2 == null)@b@ return false;@b@ return s1.equals(s2);@b@ }@b@@b@ /**@b@ * is integer string.@b@ *@b@ * @param str@b@ * @return is integer@b@ */@b@ public static boolean isInteger(String str) {@b@ if (str == null || str.length() == 0)@b@ return false;@b@ return INT_PATTERN.matcher(str).matches();@b@ }@b@@b@ public static int parseInteger(String str) {@b@ if (!isInteger(str))@b@ return 0;@b@ return Integer.parseInt(str);@b@ }@b@@b@ /**@b@ * Returns true if s is a legal Java identifier.<p>@b@ * <a href="http://www.exampledepot.com/egs/java.lang/IsJavaId.html">more info.</a>@b@ */@b@ public static boolean isJavaIdentifier(String s) {@b@ if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {@b@ return false;@b@ }@b@ for (int i = 1; i < s.length(); i++) {@b@ if (!Character.isJavaIdentifierPart(s.charAt(i))) {@b@ return false;@b@ }@b@ }@b@ return true;@b@ }@b@@b@ public static boolean isContains(String values, String value) {@b@ if (values == null || values.length() == 0) {@b@ return false;@b@ }@b@ return isContains(Constants.COMMA_SPLIT_PATTERN.split(values), value);@b@ }@b@@b@ /**@b@ * @param values@b@ * @param value@b@ * @return contains@b@ */@b@ public static boolean isContains(String[] values, String value) {@b@ if (value != null && value.length() > 0 && values != null && values.length > 0) {@b@ for (String v : values) {@b@ if (value.equals(v)) {@b@ return true;@b@ }@b@ }@b@ }@b@ return false;@b@ }@b@@b@ public static boolean isNumeric(String str) {@b@ if (str == null) {@b@ return false;@b@ }@b@ int sz = str.length();@b@ for (int i = 0; i < sz; i++) {@b@ if (Character.isDigit(str.charAt(i)) == false) {@b@ return false;@b@ }@b@ }@b@ return true;@b@ }@b@@b@ /**@b@ * @param e@b@ * @return string@b@ */@b@ public static String toString(Throwable e) {@b@ UnsafeStringWriter w = new UnsafeStringWriter();@b@ PrintWriter p = new PrintWriter(w);@b@ p.print(e.getClass().getName());@b@ if (e.getMessage() != null) {@b@ p.print(": " + e.getMessage());@b@ }@b@ p.println();@b@ try {@b@ e.printStackTrace(p);@b@ return w.toString();@b@ } finally {@b@ p.close();@b@ }@b@ }@b@@b@ /**@b@ * @param msg@b@ * @param e@b@ * @return string@b@ */@b@ public static String toString(String msg, Throwable e) {@b@ UnsafeStringWriter w = new UnsafeStringWriter();@b@ w.write(msg + "\n");@b@ PrintWriter p = new PrintWriter(w);@b@ try {@b@ e.printStackTrace(p);@b@ return w.toString();@b@ } finally {@b@ p.close();@b@ }@b@ }@b@@b@ /**@b@ * translat.@b@ *@b@ * @param src source string.@b@ * @param from src char table.@b@ * @param to target char table.@b@ * @return String.@b@ */@b@ public static String translat(String src, String from, String to) {@b@ if (isEmpty(src)) return src;@b@ StringBuilder sb = null;@b@ int ix;@b@ char c;@b@ for (int i = 0, len = src.length(); i < len; i++) {@b@ c = src.charAt(i);@b@ ix = from.indexOf(c);@b@ if (ix == -1) {@b@ if (sb != null)@b@ sb.append(c);@b@ } else {@b@ if (sb == null) {@b@ sb = new StringBuilder(len);@b@ sb.append(src, 0, i);@b@ }@b@ if (ix < to.length())@b@ sb.append(to.charAt(ix));@b@ }@b@ }@b@ return sb == null ? src : sb.toString();@b@ }@b@@b@ /**@b@ * split.@b@ *@b@ * @param ch char.@b@ * @return string array.@b@ */@b@ public static String[] split(String str, char ch) {@b@ List<String> list = null;@b@ char c;@b@ int ix = 0, len = str.length();@b@ for (int i = 0; i < len; i++) {@b@ c = str.charAt(i);@b@ if (c == ch) {@b@ if (list == null)@b@ list = new ArrayList<String>();@b@ list.add(str.substring(ix, i));@b@ ix = i + 1;@b@ }@b@ }@b@ if (ix > 0)@b@ list.add(str.substring(ix));@b@ return list == null ? EMPTY_STRING_ARRAY : (String[]) list.toArray(EMPTY_STRING_ARRAY);@b@ }@b@@b@ /**@b@ * join string.@b@ *@b@ * @param array String array.@b@ * @return String.@b@ */@b@ public static String join(String[] array) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (String s : array)@b@ sb.append(s);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * join string like javascript.@b@ *@b@ * @param array String array.@b@ * @param split split@b@ * @return String.@b@ */@b@ public static String join(String[] array, char split) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < array.length; i++) {@b@ if (i > 0)@b@ sb.append(split);@b@ sb.append(array[i]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * join string like javascript.@b@ *@b@ * @param array String array.@b@ * @param split split@b@ * @return String.@b@ */@b@ public static String join(String[] array, String split) {@b@ if (array.length == 0) return "";@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < array.length; i++) {@b@ if (i > 0)@b@ sb.append(split);@b@ sb.append(array[i]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String join(Collection<String> coll, String split) {@b@ if (coll.isEmpty()) return "";@b@@b@ StringBuilder sb = new StringBuilder();@b@ boolean isFirst = true;@b@ for (String s : coll) {@b@ if (isFirst) isFirst = false;@b@ else sb.append(split);@b@ sb.append(s);@b@ }@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * parse key-value pair.@b@ *@b@ * @param str string.@b@ * @param itemSeparator item separator.@b@ * @return key-value map;@b@ */@b@ private static Map<String, String> parseKeyValuePair(String str, String itemSeparator) {@b@ String[] tmp = str.split(itemSeparator);@b@ Map<String, String> map = new HashMap<String, String>(tmp.length);@b@ for (int i = 0; i < tmp.length; i++) {@b@ Matcher matcher = KVP_PATTERN.matcher(tmp[i]);@b@ if (matcher.matches() == false)@b@ continue;@b@ map.put(matcher.group(1), matcher.group(2));@b@ }@b@ return map;@b@ }@b@@b@ public static String getQueryStringValue(String qs, String key) {@b@ Map<String, String> map = StringUtils.parseQueryString(qs);@b@ return map.get(key);@b@ }@b@@b@ /**@b@ * parse query string to Parameters.@b@ *@b@ * @param qs query string.@b@ * @return Parameters instance.@b@ */@b@ public static Map<String, String> parseQueryString(String qs) {@b@ if (qs == null || qs.length() == 0)@b@ return new HashMap<String, String>();@b@ return parseKeyValuePair(qs, "\\&");@b@ }@b@@b@ public static String getServiceKey(Map<String, String> ps) {@b@ StringBuilder buf = new StringBuilder();@b@ String group = ps.get(Constants.GROUP_KEY);@b@ if (group != null && group.length() > 0) {@b@ buf.append(group).append("/");@b@ }@b@ buf.append(ps.get(Constants.INTERFACE_KEY));@b@ String version = ps.get(Constants.VERSION_KEY);@b@ if (version != null && version.length() > 0) {@b@ buf.append(":").append(version);@b@ }@b@ return buf.toString();@b@ }@b@@b@ public static String toQueryString(Map<String, String> ps) {@b@ StringBuilder buf = new StringBuilder();@b@ if (ps != null && ps.size() > 0) {@b@ for (Map.Entry<String, String> entry : new TreeMap<String, String>(ps).entrySet()) {@b@ String key = entry.getKey();@b@ String value = entry.getValue();@b@ if (key != null && key.length() > 0@b@ && value != null && value.length() > 0) {@b@ if (buf.length() > 0) {@b@ buf.append("&");@b@ }@b@ buf.append(key);@b@ buf.append("=");@b@ buf.append(value);@b@ }@b@ }@b@ }@b@ return buf.toString();@b@ }@b@@b@ public static String camelToSplitName(String camelName, String split) {@b@ if (camelName == null || camelName.length() == 0) {@b@ return camelName;@b@ }@b@ StringBuilder buf = null;@b@ for (int i = 0; i < camelName.length(); i++) {@b@ char ch = camelName.charAt(i);@b@ if (ch >= 'A' && ch <= 'Z') {@b@ if (buf == null) {@b@ buf = new StringBuilder();@b@ if (i > 0) {@b@ buf.append(camelName.substring(0, i));@b@ }@b@ }@b@ if (i > 0) {@b@ buf.append(split);@b@ }@b@ buf.append(Character.toLowerCase(ch));@b@ } else if (buf != null) {@b@ buf.append(ch);@b@ }@b@ }@b@ return buf == null ? camelName : buf.toString();@b@ }@b@@b@ public static String toArgumentString(Object[] args) {@b@ StringBuilder buf = new StringBuilder();@b@ for (Object arg : args) {@b@ if (buf.length() > 0) {@b@ buf.append(Constants.COMMA_SEPARATOR);@b@ }@b@ if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {@b@ buf.append(arg);@b@ } else {@b@ try {@b@ buf.append(JSON.toJSONString(arg));@b@ } catch (Exception e) {@b@ logger.warn(e.getMessage(), e);@b@ buf.append(arg);@b@ }@b@ }@b@ }@b@ return buf.toString();@b@ }@b@}