Looly 6 years ago
parent
commit
57fd7f8a2d

+ 33 - 25
hutool-core/src/main/java/cn/hutool/core/lang/Validator.java

@@ -18,9 +18,6 @@ import java.util.regex.Pattern;
  */
 public class Validator {
 
-	private Validator() {
-	}
-
 	/**
 	 * 英文字母 、数字和下划线
 	 */
@@ -364,19 +361,32 @@ public class Validator {
 	 * @param pattern 正则模式
 	 * @param value   值
 	 * @return 是否匹配正则
+	 * @deprecated 请使用 {@link #isMatchRegex(Pattern, CharSequence)}
 	 */
+	@Deprecated
 	public static boolean isMactchRegex(Pattern pattern, CharSequence value) {
 		return ReUtil.isMatch(pattern, value);
 	}
 
 	/**
+	 * 通过正则表达式验证
+	 *
+	 * @param pattern 正则模式
+	 * @param value   值
+	 * @return 是否匹配正则
+	 */
+	public static boolean isMatchRegex(Pattern pattern, CharSequence value) {
+		return ReUtil.isMatch(pattern, value);
+	}
+
+	/**
 	 * 验证是否为英文字母 、数字和下划线
 	 *
 	 * @param value 值
 	 * @return 是否为英文字母 、数字和下划线
 	 */
 	public static boolean isGeneral(CharSequence value) {
-		return isMactchRegex(GENERAL, value);
+		return isMatchRegex(GENERAL, value);
 	}
 
 	/**
@@ -574,7 +584,7 @@ public class Validator {
 	 * @since 4.1.8
 	 */
 	public static boolean isWord(CharSequence value) {
-		return isMactchRegex(PatternPool.WORD, value);
+		return isMatchRegex(PatternPool.WORD, value);
 	}
 
 	/**
@@ -601,7 +611,7 @@ public class Validator {
 	 * @return 是否为货币
 	 */
 	public static boolean isMoney(CharSequence value) {
-		return isMactchRegex(MONEY, value);
+		return isMatchRegex(MONEY, value);
 	}
 
 	/**
@@ -628,7 +638,7 @@ public class Validator {
 	 * @return 是否为邮政编码(中国)
 	 */
 	public static boolean isZipCode(CharSequence value) {
-		return isMactchRegex(ZIP_CODE, value);
+		return isMatchRegex(ZIP_CODE, value);
 	}
 
 	/**
@@ -654,7 +664,7 @@ public class Validator {
 	 * @return 否为可用邮箱地址
 	 */
 	public static boolean isEmail(CharSequence value) {
-		return isMactchRegex(EMAIL, value);
+		return isMatchRegex(EMAIL, value);
 	}
 
 	/**
@@ -680,7 +690,7 @@ public class Validator {
 	 * @return 是否为手机号码(中国)
 	 */
 	public static boolean isMobile(CharSequence value) {
-		return isMactchRegex(MOBILE, value);
+		return isMatchRegex(MOBILE, value);
 	}
 
 	/**
@@ -707,7 +717,7 @@ public class Validator {
 	 * @return 是否为身份证号码(18位中国)
 	 */
 	public static boolean isCitizenId(CharSequence value) {
-		return isMactchRegex(CITIZEN_ID, value);
+		return isMatchRegex(CITIZEN_ID, value);
 	}
 
 	/**
@@ -777,14 +787,12 @@ public class Validator {
 	 * @return 是否为生日
 	 */
 	public static boolean isBirthday(CharSequence value) {
-		if (isMactchRegex(BIRTHDAY, value)) {
-			Matcher matcher = BIRTHDAY.matcher(value);
-			if (matcher.find()) {
-				int year = Integer.parseInt(matcher.group(1));
-				int month = Integer.parseInt(matcher.group(3));
-				int day = Integer.parseInt(matcher.group(5));
-				return isBirthday(year, month, day);
-			}
+		final Matcher matcher = BIRTHDAY.matcher(value);
+		if (matcher.find()) {
+			int year = Integer.parseInt(matcher.group(1));
+			int month = Integer.parseInt(matcher.group(3));
+			int day = Integer.parseInt(matcher.group(5));
+			return isBirthday(year, month, day);
 		}
 		return false;
 	}
@@ -812,7 +820,7 @@ public class Validator {
 	 * @return 是否为IPV4地址
 	 */
 	public static boolean isIpv4(CharSequence value) {
-		return isMactchRegex(IPV4, value);
+		return isMatchRegex(IPV4, value);
 	}
 
 	/**
@@ -838,7 +846,7 @@ public class Validator {
 	 * @return 是否为IPV6地址
 	 */
 	public static boolean isIpv6(CharSequence value) {
-		return isMactchRegex(IPV6, value);
+		return isMatchRegex(IPV6, value);
 	}
 
 	/**
@@ -865,7 +873,7 @@ public class Validator {
 	 * @since 4.1.3
 	 */
 	public static boolean isMac(CharSequence value) {
-		return isMactchRegex(PatternPool.MAC_ADDRESS, value);
+		return isMatchRegex(PatternPool.MAC_ADDRESS, value);
 	}
 
 	/**
@@ -893,7 +901,7 @@ public class Validator {
 	 * @since 3.0.6
 	 */
 	public static boolean isPlateNumber(CharSequence value) {
-		return isMactchRegex(PLATE_NUMBER, value);
+		return isMatchRegex(PLATE_NUMBER, value);
 	}
 
 	/**
@@ -977,7 +985,7 @@ public class Validator {
 	 * @return 是否为中文字、英文字母、数字和下划线
 	 */
 	public static boolean isGeneralWithChinese(CharSequence value) {
-		return isMactchRegex(GENERAL_WITH_CHINESE, value);
+		return isMatchRegex(GENERAL_WITH_CHINESE, value);
 	}
 
 	/**
@@ -1004,7 +1012,7 @@ public class Validator {
 	 * @return 是否为UUID
 	 */
 	public static boolean isUUID(CharSequence value) {
-		return isMactchRegex(UUID, value) || isMactchRegex(UUID_SIMPLE, value);
+		return isMatchRegex(UUID, value) || isMatchRegex(UUID_SIMPLE, value);
 	}
 
 	/**
@@ -1032,7 +1040,7 @@ public class Validator {
 	 * @since 4.3.3
 	 */
 	public static boolean isHex(CharSequence value) {
-		return isMactchRegex(PatternPool.HEX, value);
+		return isMatchRegex(PatternPool.HEX, value);
 	}
 
 	/**

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/text/TextSimilarity.java

@@ -75,7 +75,7 @@ public class TextSimilarity {
 	}
 
 	/**
-	 * 判断字符是否为汉字,数字和字母, 因为对符号进行相似度比较没有实际意义,故符号不加入考虑范围。
+	 * 判断字符是否为汉字,数字和字母, 因为对符号进行相似度比较没有实际意义,故符号不加入考虑范围。
 	 * 
 	 * @param charValue 字符
 	 * @return true表示为非汉字,数字和字母,false反之

+ 4 - 6
hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java

@@ -1,11 +1,9 @@
 package cn.hutool.core.lang;
 
+import cn.hutool.core.exceptions.ValidateException;
 import org.junit.Assert;
 import org.junit.Test;
 
-import cn.hutool.core.exceptions.ValidateException;
-import cn.hutool.core.lang.Validator;
-
 /**
  * 验证器单元测试
  *
@@ -108,13 +106,13 @@ public class ValidatorTest {
 	@Test
 	public void isMatchTest() {
 		String url = "http://aaa-bbb.somthing.com/a.php?a=b&c=2";
-		Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url));
+		Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
 
 		url = "https://aaa-bbb.somthing.com/a.php?a=b&c=2";
-		Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url));
+		Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
 
 		url = "https://aaa-bbb.somthing.com:8080/a.php?a=b&c=2";
-		Assert.assertTrue(Validator.isMactchRegex(PatternPool.URL_HTTP, url));
+		Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, url));
 	}
 
 	@Test

+ 9 - 0
hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java

@@ -24,8 +24,17 @@ public class IdcardUtilTest {
 		boolean valid15 = IdcardUtil.isValidCard(ID_15);
 		Assert.assertTrue(valid15);
 
+		// 无效
 		String idCard = "360198910283844";
 		Assert.assertFalse(IdcardUtil.isValidCard(idCard));
+
+		// 生日无效
+		idCard = "201511221897205960";
+		Assert.assertFalse(IdcardUtil.isValidCard(idCard));
+
+		// 生日无效
+		idCard = "815727834224151";
+		Assert.assertFalse(IdcardUtil.isValidCard(idCard));
 	}
 
 	@Test

+ 30 - 11
hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java

@@ -10,9 +10,8 @@ import java.lang.reflect.Method;
 
 /**
  * 反射工具类单元测试
- * 
- * @author Looly
  *
+ * @author Looly
  */
 public class ReflectUtilTest {
 
@@ -20,17 +19,17 @@ public class ReflectUtilTest {
 	public void getMethodsTest() {
 		Method[] methods = ReflectUtil.getMethods(ExamInfoDict.class);
 		Assert.assertEquals(22, methods.length);
-		
+
 		//过滤器测试
 		methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
-		
+
 		Assert.assertEquals(4, methods.length);
 		final Method method = methods[0];
 		Assert.assertNotNull(method);
-		
+
 		//null过滤器测试
 		methods = ReflectUtil.getMethods(ExamInfoDict.class, null);
-		
+
 		Assert.assertEquals(22, methods.length);
 		final Method method2 = methods[0];
 		Assert.assertNotNull(method2);
@@ -41,22 +40,22 @@ public class ReflectUtilTest {
 		Method method = ReflectUtil.getMethod(ExamInfoDict.class, "getId");
 		Assert.assertEquals("getId", method.getName());
 		Assert.assertEquals(0, method.getParameterTypes().length);
-		
+
 		method = ReflectUtil.getMethod(ExamInfoDict.class, "getId", Integer.class);
 		Assert.assertEquals("getId", method.getName());
 		Assert.assertEquals(1, method.getParameterTypes().length);
 	}
-	
+
 	@Test
 	public void getMethodIgnoreCaseTest() {
 		Method method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "getId");
 		Assert.assertEquals("getId", method.getName());
 		Assert.assertEquals(0, method.getParameterTypes().length);
-		
+
 		method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "GetId");
 		Assert.assertEquals("getId", method.getName());
 		Assert.assertEquals(0, method.getParameterTypes().length);
-		
+
 		method = ReflectUtil.getMethodIgnoreCase(ExamInfoDict.class, "setanswerIs", Integer.class);
 		Assert.assertEquals("setAnswerIs", method.getName());
 		Assert.assertEquals(1, method.getParameterTypes().length);
@@ -75,7 +74,7 @@ public class ReflectUtilTest {
 		final Field[] fields = ReflectUtil.getFields(TestSubClass.class);
 		Assert.assertEquals(4, fields.length);
 	}
-	
+
 	@Test
 	public void setFieldTest() {
 		TestClass testClass = new TestClass();
@@ -90,6 +89,13 @@ public class ReflectUtilTest {
 		Assert.assertEquals(10, testClass.getA());
 	}
 
+	@Test
+	public void noneStaticInnerClassTest() {
+		final TestAClass testAClass = ReflectUtil.newInstanceIfPossible(TestAClass.class);
+		Assert.assertNotNull(testAClass);
+		Assert.assertEquals(2, testAClass.getA());
+	}
+
 	static class TestClass {
 		private int a;
 
@@ -101,4 +107,17 @@ public class ReflectUtilTest {
 			this.a = a;
 		}
 	}
+
+	@SuppressWarnings("InnerClassMayBeStatic")
+	class TestAClass {
+		private int a = 2;
+
+		public int getA() {
+			return a;
+		}
+
+		public void setA(int a) {
+			this.a = a;
+		}
+	}
 }