Oracle中dubbo转字符串,关于alibaba的dubbo源码包中StringUtils字符串工具类对字符串String、字符char及其相关集合数组处理...

一、前言

关于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.

@b@     * more info.@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  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  list = null;@b@        char c;@b@        int ix = 0, len = str.length();@b@        for (int i = 0; i ();@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  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  0)@b@                sb.append(split);@b@            sb.append(array[i]);@b@        }@b@        return sb.toString();@b@    }@b@@b@    public static String join(Collection 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 parseKeyValuePair(String str, String itemSeparator) {@b@        String[] tmp = str.split(itemSeparator);@b@        Map map = new HashMap(tmp.length);@b@        for (int i = 0; i  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 parseQueryString(String qs) {@b@        if (qs == null || qs.length() == 0)@b@            return new HashMap();@b@        return parseKeyValuePair(qs, "\\&");@b@    }@b@@b@    public static String getServiceKey(Map 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 ps) {@b@        StringBuilder buf = new StringBuilder();@b@        if (ps != null && ps.size() > 0) {@b@            for (Map.Entry entry : new TreeMap(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 = '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@}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值