[update] StrUtil 增加国家的二进制转换方法和不为空执行方法

This commit is contained in:
yexuejc 2024-06-05 16:23:51 +08:00
parent 0403c7c693
commit beb72c8009
2 changed files with 233 additions and 9 deletions

View File

@ -4,15 +4,7 @@ import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -473,4 +465,97 @@ public final class StrUtil {
}
return "";
}
/**
* 不是空对象时执行
*
* @param checkObj 判定对象
* @param consumer 执行器
* @param <T> 判定对象
*/
public static <T> void notNullExecute(T checkObj, Consumer<T> consumer) {
if (null != checkObj) {
consumer.accept(checkObj);
}
}
/**
* 不是空内容时执行
*
* @param checkObj 判定对象
* @param consumer 执行器
* @param <T> 判定对象
*/
public static <T> void notEmptyExecute(T checkObj, Consumer<T> consumer) {
if (isNotEmpty(checkObj)) {
consumer.accept(checkObj);
}
}
private static final List<String> COUNTRY_CODE = Arrays.asList("JPN", "KOR", "THA", "SGP", "CHN", "TWN", "HKG", "MAC", "999");
/**
* 国名を国コードに変換
*
* @param country JPN日本KOR韓国THAタイSGPシンガポールCHN中国内陸TWN中国台湾HKG中国香港MACマカオ999その他不明
* @return code byte <pre>
* 1 0 1  0      1 1 1 1 1 <br>
* 日本 韓国  タイ シンガポール 中国内陸 台湾  香港 マカオ その他
* <br>
* 0位その他1位マカオ2位香港3位台湾4位中国内陸5位シンガポール6位タイ7位韓国8位日本
* <br> 1当該国で表示0当該国表示しない
* </pre>
*/
public static byte countryToCodeByte(String country) {
String code = countryToCode(country);
return (byte) Integer.parseInt(code, 2);
}
/**
* 国家代码转換成二进制
*
* @param country JPN日本KOR韓国THA泰国CHN中国内陸SGP新加坡TWN中国台湾HKG中国香港MAC中国澳门999其他不明
* @return <pre>
* 1 0 1 0    1 1 1 1 1 <br>
* 日本 韓国  泰国 新加坡 中国内陸 台湾  香港 澳门 其他
* <br>
* 0位其他1位中国澳门2位中国香港3位中国台湾4位中国内陸5位新加坡6位泰国7位韓国8位日本
* <br> 1在该国表示0不表示该国
* </pre>
*/
public static String countryToCode(String country) {
int index = COUNTRY_CODE.indexOf(country);
if (index == -1) {
return "000000000";
}
int bn = 1 << (COUNTRY_CODE.size() - 1 - index);
// 转成二进制
String bs = Integer.toBinaryString(bn);
// 为了保证长度一致前面补0
return String.format("%09d", Integer.parseInt(bs));
}
/**
* 国家代码二进制转国家代码
*
* @param countryCode 国家代码二进制转:010000000
* <pre>
* 1 0 1 0    1 1 1 1 1 <br>
* 日本 韓国  泰国 新加坡 中国内陸 台湾  香港 澳门 其他
* <br>
* 0位其他1位中国澳门2位中国香港3位中国台湾4位中国内陸5位新加坡6位泰国7位韓国8位日本
* <br> 1在该国表示0不表示该国
* </pre>
* @return JPN日本KOR韓国THA泰国CHN中国内陸SGP新加坡TWN中国台湾HKG中国香港MAC中国澳门999其他不明
*/
public static String getCountryByCode(String countryCode) {
int i = Integer.parseInt(countryCode, 2);
int index = Integer.numberOfTrailingZeros(i);
if (index > COUNTRY_CODE.size()) {
return "O";
}
return COUNTRY_CODE.get(COUNTRY_CODE.size() - 1 - index);
}
}

View File

@ -0,0 +1,139 @@
package com.yexuejc.base.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class StrUtilTest {
@Test
void isEmpty() {
}
@Test
void isNotEmpty() {
}
@Test
void genUUID() {
}
@Test
void testGenUUID() {
}
@Test
void genNum() {
}
@Test
void toHex() {
}
@Test
void toMD5() {
}
@Test
void toSHA256() {
}
@Test
void toSHA() {
}
@Test
void iso2utf() {
}
@Test
void isNumeric() {
}
@Test
void codeId() {
}
@Test
void decodeId() {
}
@Test
void parseUrlencoded() {
}
@Test
void getSignContent() {
}
@Test
void replaceMobile() {
}
@Test
void mapSort() {
}
@Test
void setStr() {
}
@Test
void underlineToCamel() {
}
@Test
void camelToUnderline() {
}
@Test
void printStackTraceAndMessage() {
}
@Test
void printStackTrace() {
}
@Test
void notNullExecute() {
}
@Test
void notEmptyExecute() {
}
@Test
void countryToCode() {
Assertions.assertEquals(StrUtil.countryToCode("JPN"),"100000000");
Assertions.assertEquals(StrUtil.countryToCode("KOR"),"010000000");
Assertions.assertEquals(StrUtil.countryToCode("THA"),"001000000");
Assertions.assertEquals(StrUtil.countryToCode("CHN"),"000100000");
Assertions.assertEquals(StrUtil.countryToCode("SGP"),"000010000");
Assertions.assertEquals(StrUtil.countryToCode("TWN"),"000001000");
Assertions.assertEquals(StrUtil.countryToCode("HKG"),"000000100");
Assertions.assertEquals(StrUtil.countryToCode("MAC"),"000000010");
Assertions.assertEquals(StrUtil.countryToCode("999"),"000000001");
Assertions.assertEquals(StrUtil.countryToCode("O"),"000000000");
}
@Test
void getCountryByCode() {
Assertions.assertEquals(StrUtil.getCountryByCode("100000000"),"JPN");
Assertions.assertEquals(StrUtil.getCountryByCode("010000000"),"KOR");
Assertions.assertEquals(StrUtil.getCountryByCode("001000000"),"THA");
Assertions.assertEquals(StrUtil.getCountryByCode("000100000"),"CHN");
Assertions.assertEquals(StrUtil.getCountryByCode("000010000"),"SGP");
Assertions.assertEquals(StrUtil.getCountryByCode("000001000"),"TWN");
Assertions.assertEquals(StrUtil.getCountryByCode("000000100"),"HKG");
Assertions.assertEquals(StrUtil.getCountryByCode("000000010"),"MAC");
Assertions.assertEquals(StrUtil.getCountryByCode("000000001"),"999");
Assertions.assertEquals(StrUtil.getCountryByCode("000000000"),"O");
Assertions.assertEquals(StrUtil.getCountryByCode("100000000000"),"O");
Assertions.assertEquals(StrUtil.getCountryByCode("-100000000000"),"O");
}
@Test
void countryToCodeByte() {
System.out.println(String.format("%9s",Integer.toBinaryString(StrUtil.countryToCodeByte("CHN") & 0xFF)).replace(" ","0"));
}
}