From f49939ec86a09e6eb4840c7afb0af34951d32282 Mon Sep 17 00:00:00 2001 From: yixian Date: Thu, 5 Sep 2019 10:56:22 +0800 Subject: [PATCH] ato report --- .../analysis/beans/ato/ATOBulkLine.java | 12 +----------- .../analysis/beans/ato/CharacterUtils.java | 15 ++++++++++++--- .../beans/ato/CharacterUtilsTest.java | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/test/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtilsTest.java diff --git a/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/ATOBulkLine.java b/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/ATOBulkLine.java index 4b11067e8..27f9c546c 100644 --- a/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/ATOBulkLine.java +++ b/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/ATOBulkLine.java @@ -1,6 +1,5 @@ package au.com.royalpay.payment.manage.analysis.beans.ato; -import com.github.stuxuhai.jpinyin.PinyinException; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.joda.time.DateTime; @@ -18,16 +17,7 @@ public interface ATOBulkLine { default StringBuilder appendAParam(StringBuilder line, String content, int length) { content = content == null ? "" : StringUtils.trim(content); content = StringUtils.remove(StringUtils.remove(content, '\r'), '\n'); - int contentCharacterLength = content.getBytes().length; - int contentLength = content.length(); - if (contentCharacterLength != contentLength) { - try { - content = CharacterUtils.convertToAlphaBet(content); - } catch (PinyinException e) { - e.printStackTrace(); - content = ""; - } - } + content = CharacterUtils.convertToAlphaBet(content); content = content.replace("?", " "); if (content.getBytes().length != content.length()) { content = ""; diff --git a/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtils.java b/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtils.java index 5ea6b75ad..f44fdc938 100644 --- a/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtils.java +++ b/src/main/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtils.java @@ -1,10 +1,12 @@ package au.com.royalpay.payment.manage.analysis.beans.ato; +import com.github.stuxuhai.jpinyin.ChineseHelper; import com.github.stuxuhai.jpinyin.PinyinException; import com.github.stuxuhai.jpinyin.PinyinFormat; import com.github.stuxuhai.jpinyin.PinyinHelper; import org.apache.commons.lang3.StringUtils; +import java.util.Arrays; import java.util.stream.Collectors; /** @@ -12,12 +14,19 @@ import java.util.stream.Collectors; */ public class CharacterUtils { - public static String convertToAlphaBet(String source) throws PinyinException { + public static String convertToAlphaBet(String source) { return convertFullWidthToHalfWidth(convertToPinYin(source)); } - public static String convertToPinYin(String source) throws PinyinException { - return PinyinHelper.convertToPinyinString(source, "", PinyinFormat.WITHOUT_TONE).toUpperCase(); + public static String convertToPinYin(String source) { + try { + return PinyinHelper.convertToPinyinString(source, "", PinyinFormat.WITHOUT_TONE); + } catch (PinyinException e) { + return source.chars().filter(c -> !ChineseHelper.isChinese((char) c)) + .mapToObj(c -> "" + (char) c) + .collect(Collectors.joining()); + + } } public static String convertFullWidthToHalfWidth(String source) { diff --git a/src/test/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtilsTest.java b/src/test/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtilsTest.java new file mode 100644 index 000000000..89ca6af8b --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/analysis/beans/ato/CharacterUtilsTest.java @@ -0,0 +1,19 @@ +package au.com.royalpay.payment.manage.analysis.beans.ato; + +import com.github.stuxuhai.jpinyin.PinyinException; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Create by davep at 2019-09-04 11:07 + */ +public class CharacterUtilsTest { + + @Test + public void convertToAlphaBet() { + String res = CharacterUtils.convertToAlphaBet("605/110 \bQueens Road Hurstville"); + Assert.assertEquals("605/110 Queens Road Hurstville",res); + } +} \ No newline at end of file