All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.summer9102.develop.common.util.CommonUtil Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.gitee.summer9102.develop.common.util;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;

public class CommonUtil {

    /**
     * 拼接string
     *
     * @param list  集合
     * @param split 分隔符
     * @param key   元素取值方法
     */
    public static  String listToString(List list, String split, Function key) {
        boolean first = true;
        StringBuilder b = new StringBuilder();
        for (T t : list) {
            if (first) {
                first = false;
            } else {
                b.append(split);
            }
            b.append(key.apply(t));
        }
        return b.toString();
    }

    /**
     * 拼接string
     *
     * @param array 数组
     * @param split 分隔符
     * @param key   元素取值方法
     */
    public static  String listToString(T[] array, String split, Function key) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                b.append(split);
            }
            b.append(key.apply(array[i]));
        }
        return b.toString();
    }

    /**
     * 获取ip
     *
     * @param request request
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取request参数
     *
     * @param request
     * @return
     */
    public static Map getRequestParam(HttpServletRequest request) {
        Map requestParams = request.getParameterMap();
        Map params = new HashMap<>(requestParams.size());
        requestParams.forEach((name, values) -> {
            String valueStr = listToString(values, ",", Function.identity());
            params.put(name, valueStr);
        });
        return params;
    }

    /**
     * 获取url正则对象
     *
     * @return
     */
    public static Pattern getUrlPattern() {
        return Pattern.compile("(https?)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
    }

    /**
     * Stream.filter()去重方法
     *
     * @param key
     * @param 
     * @return
     */
    public static  Predicate distinctByKey(Function key) {
        Set set = new TreeSet<>();
        return t -> set.add(key.apply(t));
    }

    /**
     * URLEncode
     *
     * @param param
     * @return
     */
    public static String urlEncode(String param) {
        try {
            return URLEncoder.encode(param, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return param;
        }
    }

    /**
     * URLDecode
     *
     * @param param
     * @return
     */
    public static String urlDecode(String param) {
        try {
            return URLDecoder.decode(param, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return param;
        }
    }
}