From e9fe82acf289cd250eb4d5087cfcb2653fba1c3c Mon Sep 17 00:00:00 2001 From: tangcent Date: Mon, 10 Feb 2020 12:20:08 +0800 Subject: [PATCH] add method $tool.hump2Underline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 驼峰转下划线 --- .../java/com/sjhy/plugin/tool/NameUtils.java | 26 ++++++++++++- .../description/templateDescription.html | 1 + .../com/sjhy/plugin/tool/GlobalToolTest.java | 38 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java diff --git a/src/main/java/com/sjhy/plugin/tool/NameUtils.java b/src/main/java/com/sjhy/plugin/tool/NameUtils.java index 7e40b16..abc8080 100644 --- a/src/main/java/com/sjhy/plugin/tool/NameUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/NameUtils.java @@ -38,7 +38,8 @@ public class NameUtils { /** * 转驼峰命名正则匹配规则 */ - private final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])"); + private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])"); + private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+"); /** * 首字母大写方法 @@ -60,6 +61,29 @@ public class NameUtils { return StringUtils.uncapitalize(name); } + /** + * 驼峰转下划线,全小写 + * + * @param str 驼峰字符串 + * @return 下划线字符串 + */ + public String hump2Underline(String str) { + if (StringUtils.isEmpty(str)) { + return str; + } + Matcher matcher = TO_LINE_PATTERN.matcher(str); + StringBuffer buffer = new StringBuffer(); + while (matcher.find()) { + if (matcher.start() > 0) { + matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase()); + } else { + matcher.appendReplacement(buffer, matcher.group(0).toLowerCase()); + } + } + matcher.appendTail(buffer); + return buffer.toString(); + } + /** * 通过java全名获取类名 * diff --git a/src/main/resources/description/templateDescription.html b/src/main/resources/description/templateDescription.html index 3be45ec..afc00bf 100644 --- a/src/main/resources/description/templateDescription.html +++ b/src/main/resources/description/templateDescription.html @@ -45,6 +45,7 @@ getClsNameByFullName(String fullName) 通过包全名获取类名 getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名) getClassName(String name) 将下划线分割字符串转驼峰命名(类名) + hump2Underline(String str) 将驼峰字符串转下划线字符串 append(Object... objs) 多个数据进行拼接 newHashSet(Object... objs) 创建一个HashSet对象 newArrayList(Object... objs) 创建一个ArrayList对象 diff --git a/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java b/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java new file mode 100644 index 0000000..730dc6b --- /dev/null +++ b/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java @@ -0,0 +1,38 @@ +package com.sjhy.plugin.tool; + +import org.junit.Assert; +import org.junit.Test; + +/** + * GlobalTool相关测试 + **/ +public class GlobalToolTest { + + @Test + public void hump2Underline() { + GlobalTool tool = GlobalTool.getInstance(); + String out; + //正常 + System.out.println(out = tool.hump2Underline("asianInfrastructureInvestmentBank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //首字母大写 + System.out.println(out = tool.hump2Underline("AsianInfrastructureInvestmentBank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //已经是下划线字符串 + System.out.println(out = tool.hump2Underline("asian_infrastructure_investment_bank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //包含重复的大写字符 + System.out.println(out = tool.hump2Underline("AAsianIInfrastructureInvestmentBank")); + Assert.assertEquals("aasian_iinfrastructure_investment_bank", out); + //空字符串 + System.out.println(out = tool.hump2Underline("")); + Assert.assertEquals("", out); + //null + System.out.println(out = tool.hump2Underline(null)); + Assert.assertEquals(null, out); + //全大写字符串 + System.out.println(out = tool.hump2Underline("AASIANINFRASTRUCTUREINVESTMENTBANK")); + Assert.assertEquals("aasianinfrastructureinvestmentbank", out); + } + +} -- Gitee