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

com.github.dennisit.vplus.data.utils.MosaicUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package com.github.dennisit.vplus.data.utils;

import com.spring.boxes.dollar.RegexUtils;

import java.util.regex.Pattern;

public class MosaicUtils {

    public static String mosaicAddress(String address) {
        return StringUtils.isBlank(address) ? "" : address.replaceAll("\\d", "*");
    }

    public static String mosaicPhone(String phone) {
        if (phone == null) {
            return "";
        } else {
            if (Pattern.compile(RegexUtils.CHN_PHONE).matcher(phone).matches()) {
                phone = phone.substring(0, 3) + "****" + phone.substring(7, 11);
            } else if (Pattern.compile(RegexUtils.HK_OR_MO_PHONE).matcher(phone).matches()) {
                phone = phone.substring(0, 2) + "****" + phone.substring(6, 8);
            } else if (Pattern.compile(RegexUtils.TW_PHONE).matcher(phone).matches()) {
                phone = phone.substring(0, 2) + "****" + phone.substring(6, 9);
            } else if (Pattern.compile(RegexUtils.TELEPHONE).matcher(phone).matches()) {
                String[] split = phone.split("-");
                StringBuilder temp = new StringBuilder();

                for (int i = 0; i < split.length; ++i) {
                    if (i > 0) {
                        temp.append("-");
                    }

                    String s = split[i];
                    if (s.length() <= 3) {
                        temp.append(s);
                    } else {
                        temp.append(s.substring(0, s.length() - 4).replaceAll(".", "*")).append(s.substring(s.length() - 4, s.length()));
                    }
                }

                phone = temp.toString();
            } else if (phone.length() > 3) {
                phone = phone.substring(0, phone.length() - 4).replaceAll(".", "*") + phone.substring(phone.length() - 4, phone.length());
            }

            return phone;
        }
    }

    public static String mosaicName(String name) {
        if (name == null) {
            return "";
        } else {
            if (name.length() > 1) {
                name = name.substring(0, 1) + name.substring(1, name.length()).replaceAll(".", "*");
            }

            return name;
        }
    }


    /**
     * 【固定电话】 显示后四位,其他隐藏,比如:*******3241
     *
     * @param num 文本
     * @return 打码结果
     */
    public static String fixedPhone(String num) {
        if (org.apache.commons.lang3.StringUtils.isBlank(num)) {
            return "";
        }
        return org.apache.commons.lang3.StringUtils.leftPad(org.apache.commons.lang3.StringUtils.right(num, 4), org.apache.commons.lang3.StringUtils.length(num), "*");
    }

    /**
     * 【手机号码】前三位,后四位,其他隐藏,比如:135****6810
     *
     * @param num 文本
     * @return 打码结果
     */
    public static String mobilePhone(String num) {
        if (org.apache.commons.lang3.StringUtils.isBlank(num)) {
            return "";
        }
        return org.apache.commons.lang3.StringUtils.left(num, 3).concat(org.apache.commons.lang3.StringUtils.removeStart(org.apache.commons.lang3.StringUtils.leftPad(org.apache.commons.lang3.StringUtils.right(num, 4), org.apache.commons.lang3.StringUtils.length(num), "*"), "***"));
    }


    /**
     * 【密码】密码的全部字符都用*代替,比如:******
     *
     * @param password 文本
     * @return 打码结果
     */
    public static String password(String password) {
        if (org.apache.commons.lang3.StringUtils.isBlank(password)) {
            return "";
        }
        String pwd = org.apache.commons.lang3.StringUtils.left(password, 0);
        return org.apache.commons.lang3.StringUtils.rightPad(pwd, org.apache.commons.lang3.StringUtils.length(password), "*");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy