From 3ab688fe005f4d16e2312dda427786e550adec11 Mon Sep 17 00:00:00 2001 From: wangyandong3 Date: Mon, 1 Apr 2024 10:40:55 +0800 Subject: [PATCH] feat:add sysFunction --- .../java/run/mone/sysFunc/SysFuncConst.java | 18 ++ .../java/run/mone/sysFunc/SysFuncDict.java | 19 +++ .../java/run/mone/sysFunc/SysFuncEnum.java | 32 +++- .../java/run/mone/sysFunc/SysFuncUtils.java | 161 +++++++++++++++--- .../run/mone/sysFunc/test/SysFuncTest.java | 49 +++++- 5 files changed, 250 insertions(+), 29 deletions(-) create mode 100644 jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncDict.java diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java index 5f355e733..bfeb7035b 100644 --- a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java @@ -7,4 +7,22 @@ public class SysFuncConst { public static final String FUNC_NAME_UUID = "java.uuid"; public static final String FUNC_NAME_RANDOM_NUMBER = "java.randomNumber"; + + public static final String FUNC_NAME_TIME_STAMP = "java.timeStamp"; + + public static final String FUNC_NAME_RANDOM_STRING = "java.randomString"; + + public static final String FUNC_NAME_PHONE_NUM = "java.phoneNum"; + + public static final String FUNC_NAME_UPPER_CASE = "java.upperCase"; + + public static final String FUNC_NAME_LOWER_CASE = "java.lowerCase"; + + public static final String FUNC_NAME_SELECT = "java.select"; + + public static final String FUNC_NAME_RANDOM_DOUBLE = "java.randomDouble"; + + public static final String FUNC_NAME_TIME_STAMP_TO_DATE = "java.timeStampToDate"; + + public static final String FUNC_NAME_DATE_TO_TIME_STAMP = "java.dateToTimeStamp"; } diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncDict.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncDict.java new file mode 100644 index 000000000..8138233c4 --- /dev/null +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncDict.java @@ -0,0 +1,19 @@ +package run.mone.sysFunc; + +public class SysFuncDict { + + public static final char[] charSeeds = new char[]{ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + + public static final String[] phoneNumPrefixes = new String[]{ + "134", "135", "136", "137", "138", "139", "147", "148", "150", "151", + "152", "157", "158", "159", "178", "182", "183", "184", "187", "188", + "130", "131", "132", "145", "155", "156", "166", "171", "175", "176", + "185", "186", "133", "149", "153", "173", "177", "180", "181", "189", + "190", "191", "193", "192", "199" + }; +} diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java index e31566eb0..28aafb6f7 100644 --- a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java @@ -4,19 +4,37 @@ public enum SysFuncEnum { - //报警级别 SUBSTRING(FUNC_NAME_SUBSTRING, "common", "截取字符串", "${java.substring(ceshi, 1, 3)}", "返回从指定开始索引到结束索引之间的子字符串,但不包括结束索引位置的字符"), UUID(FUNC_NAME_UUID, "common", "唯一标识符", "${java.uuid()}", "标准化的唯一标识符"), RANDOM_NUMBER(FUNC_NAME_RANDOM_NUMBER, "common", "随机数", "${java.randomNumber(2, 11)}", - "生成指定范围的整型数字"); + "生成指定范围的整型数字"), + TIME_STAMP(FUNC_NAME_TIME_STAMP, "common", "时间戳(毫秒)", "${java.timeStamp()}", + "获取的是当前时间戳,单位毫秒"), + RANDOM_STRING(FUNC_NAME_RANDOM_STRING, "common", "随机字符串", "${java.randomString(10)}", + "[A-Za-z0-9]范围的中指定长度的随机字符串"), + PHONE_NUM(FUNC_NAME_PHONE_NUM, "common", "随机手机号码", "${java.phoneNum()}", + "中国区地区的手机号码"), + LOWER_CASE(FUNC_NAME_LOWER_CASE, "common", "字符转小写", "${java.lowerCase(TxtContent)}", + "字符转小写"), + UPPER_CASE(FUNC_NAME_UPPER_CASE, "common", "字符转大写", "${java.upperCase(TxtContent)}", + "字符转大写"), + RANDOM_DOUBLE(FUNC_NAME_RANDOM_DOUBLE, "common", "随机小数", "${java.randomDouble(10,50,3)}", + "生成随机小数,三个参数分别是最小值,最大值和小数长度"), + DATE_TO_TIME_STAMP(FUNC_NAME_DATE_TO_TIME_STAMP, "common", "日期转换时间戳", "${java.dateToTimeStamp(2024-03-29 10:30:12,yyyy-MM-dd HH:mm:ss)}", + "两个参数分别是时间字符串和时间格式,两个保持一致,转换成毫秒单位的时间戳"), + TIME_STAMP_TO_DATE(FUNC_NAME_TIME_STAMP_TO_DATE, "common", "时间戳转日期字符", "${java.timeStampToDate(1711680624906,yyyy-MM-dd HH:mm:ss)}", + "两个参数分别是毫秒时间戳和要输出的日期字符串的格式"), + SELECT(FUNC_NAME_SELECT, "common", "随机选择元素", "${java.select(true,false)}", + "参数是多个使用逗号分隔的元素,随机选择其中的一个进行返回"), + ; - public String cname; - public String name; - public String type; - public String desc; - public String example; + public final String cname; + public final String name; + public final String type; + public final String desc; + public final String example; SysFuncEnum(String name, String type, String cname, String example, String desc) { this.name = name; diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java index b833e1f49..004c5fbea 100644 --- a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java @@ -2,16 +2,19 @@ import org.apache.commons.lang3.StringUtils; -import java.util.Arrays; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.IntStream; import static run.mone.sysFunc.SysFuncConst.*; +import static run.mone.sysFunc.SysFuncDict.charSeeds; +import static run.mone.sysFunc.SysFuncDict.phoneNumPrefixes; public class SysFuncUtils { @@ -28,29 +31,64 @@ public static String gen(String funcDesc) { //不做任何处理,直接返回 return funcDesc; } - String func = funcDesc.substring(2, funcDesc.length() - 1); int index = func.indexOf("("); String funcName = func.substring(0, index); String funcParams = func.substring(index + 1, func.length() - 1); String[] funcParamArr = funcParams.split(","); - List funcParamList = Arrays.asList(funcParamArr).stream().map(it -> it.trim()).collect(Collectors.toList()); + trimItem(funcParamArr); switch (funcName) { case FUNC_NAME_SUBSTRING: { - return subString(funcParamList, funcParams); + return subString(funcParamArr, funcParams); } case FUNC_NAME_UUID: { return uuid(); } case FUNC_NAME_RANDOM_NUMBER: { - return randomNumber(funcParamList, funcParams); + return randomNumber(funcParamArr, funcParams); + } + case FUNC_NAME_TIME_STAMP: { + return timeStamp(); + } + case FUNC_NAME_RANDOM_STRING: { + return randomString(funcParamArr); + } + case FUNC_NAME_PHONE_NUM: { + return phoneNum(); + } + case FUNC_NAME_LOWER_CASE: { + return lowerCase(funcParamArr); + } + case FUNC_NAME_UPPER_CASE: { + return upperCase(funcParamArr); + } + case FUNC_NAME_RANDOM_DOUBLE: { + return randomDouble(funcParamArr); + } + case FUNC_NAME_DATE_TO_TIME_STAMP: { + return dateToTimeStamp(funcParamArr); + } + case FUNC_NAME_TIME_STAMP_TO_DATE: { + return timeStampToDate(funcParamArr); + } + case FUNC_NAME_SELECT: { + return select(funcParamArr); } default: return funcDesc; } } + private static void trimItem(String[] items){ + if(items.length==0){ + return; + } + for (int i = 0; i < items.length; i++) { + items[i] = items[i].trim(); + } + } + public static List batchGen(String funcDesc, int number) { return batchOperation(() -> gen(funcDesc), number); } @@ -62,8 +100,7 @@ public static List batchOperation(Supplier supplier, int number) int n = (number / batchNumber) + 1; for (int j = 0; j <= n; j++) { - int defaultNumber = batchNumber; - IntStream.range(0, defaultNumber) + IntStream.range(0, batchNumber) .parallel() // 将流转换为并行流 .forEach(i -> { // 这里是要并行执行的操作 @@ -75,18 +112,17 @@ public static List batchOperation(Supplier supplier, int number) return res.subList(0, number); } - private static String subString(List funcParamList, String defaultStr) { + private static String subString(String[] funcParams, String defaultStr) { //substring(int beginIndex) - if (funcParamList.size() == 2) { - return funcParamList.get(0).substring(Integer.valueOf(funcParamList.get(1))); + if (funcParams.length == 2) { + return funcParams[0].substring(Integer.parseInt(funcParams[1])); } //substring(int beginIndex, int endIndex) - if (funcParamList.size() == 3) { - return funcParamList.get(0).substring(Integer.valueOf(funcParamList.get(1)), Integer.valueOf(funcParamList.get(2))); + if (funcParams.length == 3) { + return funcParams[0].substring(Integer.parseInt(funcParams[1]), Integer.parseInt(funcParams[2])); } - return defaultStr; } @@ -95,17 +131,100 @@ private static String uuid() { return uuid.toString(); } - private static String randomNumber(List funcParamList, String defaultStr) { - if (funcParamList.size() == 2) { - int randomNumberInRange = ThreadLocalRandom.current().nextInt(Integer.valueOf(funcParamList.get(0)), Integer.valueOf(funcParamList.get(1))); - return String.valueOf(randomNumberInRange); + private static String randomNumber(String[] funcParams, String defaultStr) { + if (funcParams.length == 2) { + return String.valueOf(ThreadLocalRandom.current().nextLong(Long.parseLong(funcParams[0]), Long.parseLong(funcParams[1]))); } - if (funcParamList.size() == 1) { - int randomNumberInRange = ThreadLocalRandom.current().nextInt(Integer.valueOf(funcParamList.get(0))); - return String.valueOf(randomNumberInRange); + if (funcParams.length == 1) { + return String.valueOf(ThreadLocalRandom.current().nextLong(Long.parseLong(funcParams[0]))); } return defaultStr; } + + private static String timeStamp(){ + return String.valueOf(System.currentTimeMillis()); + } + + private static String randomString(String[] funcParams){ + if (funcParams.length == 0) { + return ""; + } + int range = Integer.parseInt(funcParams[0]); + if (range <= 0) { + return ""; + } + ThreadLocalRandom localRandom = ThreadLocalRandom.current(); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < range; i++) { + builder.append(charSeeds[localRandom.nextInt(charSeeds.length)]); + } + return builder.toString(); + } + + private static String phoneNum(){ + ThreadLocalRandom localRandom = ThreadLocalRandom.current(); + StringBuilder builder = new StringBuilder(phoneNumPrefixes[ThreadLocalRandom.current().nextInt(phoneNumPrefixes.length)]); + for (int i = 0; i < 8; i++) { + builder.append(localRandom.nextInt(10)); + } + return builder.toString(); + } + + private static String upperCase(String[] funcParams){ + if(funcParams == null || funcParams.length == 0){ + return ""; + } + return funcParams[0].toUpperCase(); + } + + private static String lowerCase(String[] funcParams){ + if(funcParams == null || funcParams.length == 0){ + return ""; + } + return funcParams[0].toLowerCase(); + } + + private static String select(String[] funcParams){ + if(funcParams.length==0){ + return ""; + } + if(funcParams.length==1){ + return funcParams[0]; + } + return funcParams[ThreadLocalRandom.current().nextInt(funcParams.length)]; + } + + private static String randomDouble(String[] funcParams){ + if( funcParams.length != 3){ + return "0.0"; + } + int decimalPlaces = Integer.parseInt(funcParams[2]); + double v = ThreadLocalRandom.current().nextDouble(Double.parseDouble(funcParams[0]), Double.parseDouble(funcParams[1])); + return String.format("%." + decimalPlaces + "f", v); + } + + private static String timeStampToDate(String[] funcParams){ + if(funcParams == null || funcParams.length < 2){ + return null; + } + String data = funcParams[0]; + String format = funcParams[1]; + return new SimpleDateFormat(format).format(new Date(Long.parseLong(data))); + } + + private static String dateToTimeStamp(String[] funcParams){ + if(funcParams == null || funcParams.length < 2){ + return null; + } + String data = funcParams[0]; + String format = funcParams[1]; + try { + return String.valueOf(new SimpleDateFormat(format).parse(data).getTime()); + } catch (ParseException e) { + System.out.println("dateToTimeStamp fail ,error ="+ e); + } + return null; + } } diff --git a/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java b/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java index 9600d2ef4..6c517205e 100644 --- a/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java +++ b/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java @@ -23,7 +23,7 @@ public void testUuid() { @Test public void testRandomNumber() { - String res = SysFuncUtils.gen("${java.randomNumber(2,11)}"); + String res = SysFuncUtils.gen("${java.randomNumber(8000000000000,9000000000000)}"); System.out.println(res); } @@ -33,6 +33,53 @@ public void testRandomNumberBatch() { System.out.println(res); } + @Test + public void testTimeStamp() { + String res = SysFuncUtils.gen("${java.timeStamp()}"); + System.out.println(res); + } + @Test + public void testRandomString() { + String res = SysFuncUtils.gen("${java.randomString(15)}"); + System.out.println(res); + } + @Test + public void testPhoneNum() { + String res = SysFuncUtils.gen("${java.phoneNum()}"); + System.out.println(res); + } + + @Test + public void testUpperCase() { + String res = SysFuncUtils.gen("${java.upperCase(Txt Content)}"); + System.out.println(res); + } + + @Test + public void testLowerCase() { + String res = SysFuncUtils.gen("${java.lowerCase(Txt Content)}"); + System.out.println(res); + } + @Test + public void testSelect() { + String res = SysFuncUtils.gen("${java.select(true,false)}"); + System.out.println(res); + } + @Test + public void testRandomDouble() { + String res = SysFuncUtils.gen("${java.randomDouble(10,50,3)}"); + System.out.println(res); + } + @Test + public void testTimeStampToDate() { + String res = SysFuncUtils.gen("${java.timeStampToDate(1711680624906,yyyy-MM-dd HH:mm:ss)}"); + System.out.println(res); + } + @Test + public void testDateToTimeStamp() { + String res = SysFuncUtils.gen("${java.dateToTimeStamp(2024-03-29 10:30:12,yyyy-MM-dd HH:mm:ss)}"); + System.out.println(res); + } }