mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-03 04:24:05 +08:00
[update] StrUtil 增加国家的二进制转换方法和不为空执行方法
This commit is contained in:
parent
0403c7c693
commit
beb72c8009
@ -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:その他、0:不明
|
||||
* @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:其他、0:不明
|
||||
* @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:其他、0:不明
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
139
src/test/java/com/yexuejc/base/util/StrUtilTest.java
Normal file
139
src/test/java/com/yexuejc/base/util/StrUtilTest.java
Normal 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"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user