From 6ea24f4d64a386139d40249e87ce552aeed3c5ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=BC=9F=E9=BE=99?= Date: Thu, 19 Mar 2020 09:49:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/lang/PatternPool.java | 11 ++--------- .../java/cn/hutool/core/lang/Validator.java | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java b/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java index 3beaad1bf..db8efd5e1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/PatternPool.java @@ -69,17 +69,10 @@ public class PatternPool { /** 社会统一信用代码 */ - public static final String REGEX = "^[A-Z0-9]{18}$"; + public static final String CREDIT_CODE = "^[A-Z0-9]{18}$"; - public static final String BASE_CODE_STRING = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + public static final String BASE_CODE_REGEX = "[0-9A-Y]{18}"; - public static final String BASE_CODE_REGEX = "[" + BASE_CODE_STRING + "]{18}"; - - public static final List BASE_CODES = new ArrayList<>(); - - public static final int[] WEIGHT = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}; - - public static final char[] BASE_CODE_ARRAY = BASE_CODE_STRING.toCharArray(); // ------------------------------------------------------------------------------------------------------------------------------------------------------------------- /** Pattern池 */ diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java index 1023fe04a..372169145 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Validator.java @@ -8,6 +8,8 @@ import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -87,6 +89,17 @@ public class Validator { */ public final static Pattern PLATE_NUMBER = PatternPool.PLATE_NUMBER; + /** + * 社会统一信用代码 + */ + public static final List BASE_CODES = new ArrayList<>(); + + public static final String BASE_CODE_STRING = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + + public static final int[] WEIGHT = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}; + + public static final char[] BASE_CODE_ARRAY = BASE_CODE_STRING.toCharArray(); + /** * 给定值是否为true * @@ -1115,7 +1128,7 @@ public class Validator { if (StrUtil.isBlank(creditCode)) { return false; } - return Pattern.matches(PatternPool.REGEX, creditCode); + return Pattern.matches(PatternPool.CREDIT_CODE, creditCode); } /** @@ -1133,9 +1146,9 @@ public class Validator { int sum = 0, length = 17; for (int i = 0; i < length; i++) { char key = businessCodeArray[i]; - sum += (PatternPool.BASE_CODES.indexOf(key) * PatternPool.WEIGHT[i]); + sum += (BASE_CODES.indexOf(key) * WEIGHT[i]); } int value = 31 - sum % 31; - return check == PatternPool.BASE_CODE_ARRAY[value % 31]; + return check == BASE_CODE_ARRAY[value % 31]; } }