-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>jcommon</artifactId> | ||
<groupId>run.mone</groupId> | ||
<version>1.4-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sysFunc</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.12.0</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
10 changes: 10 additions & 0 deletions
10
jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package run.mone.sysFunc; | ||
|
||
public class SysFuncConst { | ||
|
||
public static final String FUNC_NAME_SUBSTRING = "java.substring"; | ||
|
||
public static final String FUNC_NAME_UUID = "java.uuid"; | ||
|
||
public static final String FUNC_NAME_RANDOM_NUMBER = "java.randomNumber"; | ||
} |
28 changes: 28 additions & 0 deletions
28
jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package run.mone.sysFunc; | ||
|
||
import static run.mone.sysFunc.SysFuncConst.*; | ||
|
||
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)}", | ||
"生成指定范围的整型数字"); | ||
|
||
public String cname; | ||
public String name; | ||
public String type; | ||
public String desc; | ||
public String example; | ||
|
||
SysFuncEnum(String name, String type, String cname, String example, String desc) { | ||
this.name = name; | ||
this.type = type; | ||
this.cname = cname; | ||
this.desc = desc; | ||
this.example = example; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package run.mone.sysFunc; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.Collectors; | ||
|
||
import static run.mone.sysFunc.SysFuncConst.*; | ||
|
||
public class SysFuncUtils { | ||
|
||
/** | ||
* eg. ${java.substring("ceshi", 1, 3)} | ||
* | ||
* @param funcDesc | ||
* @return | ||
*/ | ||
public static String gen(String funcDesc) { | ||
if (StringUtils.isEmpty(funcDesc) | ||
|| !funcDesc.startsWith("${") | ||
|| !funcDesc.endsWith("}")) { | ||
//不做任何处理,直接返回 | ||
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<String> funcParamList = Arrays.asList(funcParamArr).stream().map(it -> it.trim()).collect(Collectors.toList()); | ||
|
||
switch (funcName) { | ||
case FUNC_NAME_SUBSTRING: { | ||
return subString(funcParamList, funcParams); | ||
} | ||
case FUNC_NAME_UUID: { | ||
return uuid(); | ||
} | ||
case FUNC_NAME_RANDOM_NUMBER: { | ||
return randomNumber(funcParamList, funcParams); | ||
} | ||
default: | ||
return funcDesc; | ||
} | ||
} | ||
|
||
private static String subString(List<String> funcParamList, String defaultStr) { | ||
|
||
//substring(int beginIndex) | ||
if (funcParamList.size() == 2) { | ||
return funcParamList.get(0).substring(Integer.valueOf(funcParamList.get(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))); | ||
} | ||
|
||
return defaultStr; | ||
} | ||
|
||
private static String uuid() { | ||
UUID uuid = UUID.randomUUID(); | ||
return uuid.toString(); | ||
} | ||
|
||
private static String randomNumber(List<String> 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); | ||
} | ||
|
||
if (funcParamList.size() == 1) { | ||
int randomNumberInRange = ThreadLocalRandom.current().nextInt(Integer.valueOf(funcParamList.get(0))); | ||
return String.valueOf(randomNumberInRange); | ||
} | ||
|
||
return defaultStr; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package run.mone.sysFunc.test; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import run.mone.sysFunc.SysFuncUtils; | ||
|
||
@Ignore | ||
public class SysFuncTest { | ||
|
||
@Test | ||
public void testSubstring() { | ||
String res = SysFuncUtils.gen("${java.substring(ceshi, 1, 3)}"); | ||
System.out.println(res); | ||
} | ||
|
||
@Test | ||
public void testUuid() { | ||
String res = SysFuncUtils.gen("${java.uuid()}"); | ||
System.out.println(res); | ||
} | ||
|
||
@Test | ||
public void testRandomNumber() { | ||
String res = SysFuncUtils.gen("${java.randomNumber(2,11)}"); | ||
System.out.println(res); | ||
} | ||
|
||
|
||
|
||
} |