com.zxy.commons.lang.utils.StringsUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.zxy.commons.lang.utils.StringsUtils.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.zxy.commons.lang.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.zxy.commons.lang.constant.CommonConstant;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * String
 * 
 * <p>
 * <a href="StringsUtils.java"><i>View Source</i></a>
 * 
 * @author zhaoxunyong@qq.com
 * @version 1.0
 * @since 1.0
 */
public final class StringsUtils {
    private static final String DEFAULT_SPLIT = CommonConstant.DEFAULT_SPLIT;
    private static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
    private static final long KB = 1024;
    private static final long MB = 1024 * 1024;
    private static final long GB = 1024 * 1024 * 1024;

    private StringsUtils() {
    }

    /**
     * ??
     * 
     * @param len ??
     * @return ??
     */
    public static String generatePassword(int len) {
        return RandomStringUtils.random(len, "0123456789abcdefghijklmnopqrstuvwxyz");
    }

    /**
     * {@value #DEFAULT_SPLIT}?List?
     * 
     * @param source 
     * @return ?
     */
    public static List<String> toList(String source) {
        return toList(source, DEFAULT_SPLIT);
    }

    /**
     * {@code separator}?List?
     * 
     * @param source 
     * @param separator 
     * @return ?
     */
    public static List<String> toList(String source, String separator) {
        return Splitter.on(separator).trimResults().splitToList(source);
        //        List<String> result = new ArrayList<String>();
        //
        //        if (source != null) {
        //            String[] tmps = source.split(separator);
        //            for (String tmp : tmps) {
        //                if (StringUtils.isNotBlank(tmp)) {
        //                    result.add(tmp);
        //                }
        //            }
        //        }
        //        return result;
    }

    /**
     * ?List,?
     * 
     * @param source 
     * @return List?
     */
    public static List<String> toUniqList(String source) {
        return toUniqList(source, DEFAULT_SPLIT);
    }

    /**
     * ?List,?
     * 
     * @param source 
     * @param separator 
     * @return List?
     */
    public static List<String> toUniqList(String source, String separator) {
        List<String> result = new ArrayList<String>();
        if (source != null) {
            List<String> tmps = toList(source, separator);
            for (String tmp : tmps) {
                if (!result.contains(tmp)) {
                    result.add(tmp);
                }
            }
        }
        return result;
    }

    /**
     * null""
     * 
     * @param str 
     * @return ??
     */
    public static String toString(String str) {
        return StringUtils.isEmpty(str) ? "" : str;
    }

    /**
     * String
     * 
     * @param bytes 
     * @param charset ???
     * @return String
     * @throws UnsupportedEncodingException UnsupportedEncodingException
     */
    public static String toString(byte[] bytes, String charset) throws UnsupportedEncodingException {
        return new String(bytes, charset);
    }

    /**
     * String
     * 
     * @param bytes 
     * @return String
     * @throws UnsupportedEncodingException UnsupportedEncodingException
     */
    public static String toString(byte[] bytes) throws UnsupportedEncodingException {
        return new String(bytes, DEFAULT_CHARSET);
    }

    /**
     * ??
     * 
     * @param str 
     * @return ??
     */
    public static boolean isLetter(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
                    && !(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
                return false;
            }
        }
        return true;
    }

    /**
     * ??????
     * 
     * @param size ?
     * @return ?
     */
    public static String formatSize(long size) {
        String result = "";
        if (size >= 0 && size < KB) {
            result = size + "B";
        } else if (size >= KB && size < MB) {
            result = size / KB + "KB";
        } else if (size >= MB && size < GB) {
            result = size / MB + "MB";
        } else if (size >= GB) {
            result = size / GB + "GB";
        }
        return result;
    }

    /**
     * List??
     * 
     * @param arrList ?
     * @return ????
     */
    public static List<String> removeDuplicate(List<String> arrList) {
        if (arrList == null || arrList.isEmpty()) {
            return new ArrayList<String>();
        }
        LinkedHashSet<String> hSet = new LinkedHashSet<String>(arrList);
        List<String> tmp = new ArrayList<String>(hSet.size());
        tmp.addAll(hSet);
        return tmp;
    }

    /**
     * ??
     * 
     * @param arr 
     * @return 
     */
    public static String array2String(String[] arr) {
        StringBuilder tmp = new StringBuilder();
        if (arr != null && arr.length > 0) {
            for (String a : arr) {
                tmp.append(DEFAULT_SPLIT).append(a);
            }
            tmp.deleteCharAt(0);
        }
        return tmp.toString();
    }

    /**
     * ip?
     * 
     * @param ip ip
     * @return 
     */
    @SuppressFBWarnings("DM_BOXED_PRIMITIVE_FOR_PARSING")
    public static long ip2Long(String ip) {
        if (StringUtils.isBlank(ip)) {
            return 0L;
        }
        String[] items = ip.split("\\.");
        return Long.valueOf(items[0]) << 24 | Long.valueOf(items[1]) << 16 | Long.valueOf(items[2]) << 8
                | Long.valueOf(items[3]);
    }

    /**
     * ??null?
     * 
     * @param strs ?
     * @return ??
     */
    public static boolean isAnyBlank(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        }
        for (String str : strs) {
            if (StringUtils.isBlank(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * ??null
     * 
     * @param strs ?
     * @return ??
     */
    public static boolean isAnyEmpty(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        }
        for (String str : strs) {
            if (StringUtils.isEmpty(str)) {
                return true;
            }
        }
        return false;
    }
}