com.mmj.app.common.util.StringFormatter.java Source code

Java tutorial

Introduction

Here is the source code for com.mmj.app.common.util.StringFormatter.java

Source

/*
 * Copyright 2014-2017 MSUN.com All right reserved. This software is the confidential and proprietary information of
 * MSUN.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with MSUN.com.
 */
package com.mmj.app.common.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

import com.mmj.app.common.core.lang.Assert;
// import org.joda.time.Duration;
// import org.joda.time.format.PeriodFormatter;
// import org.joda.time.format.PeriodFormatterBuilder;

/**
 * ?
 * 
 * <pre>
 *  ?http://blog.csdn.net/yohop/article/details/2534907?
 *  ??%[][?][][]??
 *  :
 *       - ? ?????
 *  ?:
 *      ???
 *  :
 *       ??????
 *       e,Ef????.2??2
 *       s??
 *       
 *  ??:
 *       s,f,%(?"\")
 * 
 * </pre>
 * 
 * @author zxc Jun 16, 2014 12:41:07 AM
 */
public class StringFormatter {

    private static final char[] array = new char[] { ',', ' ', '(', ')' };

    public static final String ENCODE = "utf-8";

    // static PeriodFormatter formatter = new
    // PeriodFormatterBuilder().appendDays().appendSuffix("").appendHours().appendSuffix("?").appendMinutes().appendSuffix("").appendSeconds().appendSuffix("").appendMillis3Digit().appendSuffix("").toFormatter();

    /***
     * ?Float0
     * 
     * @param f
     * @return
     */
    public static String floatFormat(Float f) {
        if (f == null) {
            return 0 + "";
        }
        String s = f.toString();
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");// 0
            s = s.replaceAll("[.]$", "");// ??.
        }
        return s;
    }

    public static String decode(String s) {
        try {
            return URLDecoder.decode(s, ENCODE);
        } catch (Exception e) {
            return s;
        }
    }

    public static String encode(String s) {
        try {
            return URLEncoder.encode(s, ENCODE);
        } catch (Exception e) {
            return s;
        }
    }

    // public static String formatDuration(long durationMs) {
    // return formatDuration(durationMs, null);
    // }

    // public static String formatDuration(long durationMs, String prefix) {
    // Duration duration = new Duration(durationMs);
    // String print = formatter.print(duration.toPeriod());
    // return (prefix == null ? "" : prefix) + print + " (" + durationMs + ")ms";
    // }

    public static String formatFloat(Float value, int delimiter, String suffix) {
        float _value = value == null ? 0f : value;
        suffix = suffix == null ? "" : suffix;
        return String.format("%." + delimiter + "f" + suffix, _value);
    }

    public static String formatFloat(Number denominator, Number molecule) {
        if (denominator == null || molecule == null) {
            return null;
        }
        return formatFloat(denominator.floatValue() / molecule.floatValue(), 2, null);
    }

    public static String formatFloat(Float value) {
        return formatFloat(value, 2, null);
    }

    /**
     * ?
     */
    public static String formatPercent(Float value) {
        return formatFloat(value, 2, "%%");
    }

    public static String formatString(String value, boolean alignLeft, int minLength, int maxLength) {
        return String.format("%" + (alignLeft ? "-" : "") + minLength + "." + maxLength + "s", value);
    }

    public static String matcherRegex(String str, String regex) {
        return matcherRegex(str, regex, true);
    }

    public static String matcherRegex(String s, String regex, boolean needTrim) {
        if (StringUtils.isBlank(s)) {
            return StringUtils.EMPTY;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        return needTrim ? m.replaceAll(StringUtils.EMPTY).trim() : m.replaceAll(StringUtils.EMPTY);
    }

    public static boolean matchsRegex(String str, String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static boolean containsRegex(String str, String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * source?
     */
    public static Set<String> containsAny(String source, String[] testArray) {
        Assert.assertNotNull(source);
        Assert.assertNotNull(testArray);
        Set<String> result = new HashSet<String>(testArray.length);
        for (String testWord : testArray) {
            if (source.contains(testWord)) {
                result.add(testWord);
            }
        }
        return result;
    }

    /**
     * ??????
     */
    public static Set<String> matchLetterAndDigit(String source) {
        Set<String> result = new HashSet<String>();
        char[] charByte = new char[source.length()];
        int offset = 0;
        for (int i = 0, j = source.length(); i < j; i++) {
            char charAt = source.charAt(i);
            if (isMatch(charAt)) {// ?
                charByte[offset] = charAt;
                offset++;
            } else {
                if (offset > 0) {
                    char[] copyOfRange = Arrays.copyOfRange(charByte, 0, offset);
                    if (copyOfRange.length > 1) {
                        result.add(new String(copyOfRange));
                    }
                    offset = 0;
                }
            }
            if (i == (j - 1) && offset > 0) {
                char[] copyOfRange = Arrays.copyOfRange(charByte, 0, offset);
                if (copyOfRange.length > 1) {
                    result.add(new String(copyOfRange));
                }
            }
        }
        return result;
    }

    private static boolean isMatch(char charAt) {
        String binaryString = Integer.toBinaryString(charAt);
        if (binaryString.length() > 8) {
            return false;
        } else {
            return !ArrayUtils.contains(array, charAt);
        }
    }

    public static boolean isContainsRegex(String str, String regex) {
        if (StringUtils.isBlank(str) || StringUtils.isBlank(regex)) {
            return false;
        }
        return Pattern.compile(regex).matcher(str).find();
    }

    // double
    public static float getWordSize(String o) {
        int l = o.length();
        o = StringFormatter.matcherRegex(o, "[^\\x00-\\xff]", false);// ?
        return (float) (o.length() * 0.5) + l - o.length();
    }

    // 2
    public static int getEnWordSize(String o) {
        int l = o.length();
        o = StringFormatter.matcherRegex(o, "[^\\x00-\\xff]", false);// ?
        return 2 * l - o.length();
    }

    public static void main(String[] args) {
        // String word = "!@#3232";
        // if (Pattern.compile("(?i)[a-z]").matcher(word).find()) {
        // System.out.println("?");
        // } else if (Pattern.compile("(?i)[0-9]").matcher(word).find()) {
        // System.out.println("");
        // }
        // System.out.println(matchLetterAndDigit("60D- 55Z "));
        // System.out.println(matchLetterAndDigit("60D-55Z "));
        // System.out.println(matchLetterAndDigit("60D,55Z "));
        // System.out.println(matchLetterAndDigit("60D#55Z "));
        // System.out.println(matchLetterAndDigit("T?"));

        System.out.println(getEnWordSize("60D^^??"));
        System.out.println(getWordSize("2XU Compression Top "));
    }
}