Merge remote-tracking branch 'origin/v5-dev' into v5-dev

This commit is contained in:
duandazhi 2021-06-29 19:40:12 +08:00
commit 9a6b8cbf45
11 changed files with 447 additions and 21 deletions

View File

@ -24,6 +24,8 @@
* 【core 】 修复SqlFormatter部分SQL空指针问题issue#I3XS44@Gitee
* 【core 】 修复DateRange计算问题issue#I3Y1US@Gitee
* 【core 】 修复BeanCopier中setFieldNameEditor失效问题pr#349@Gitee
* 【core 】 修复ArrayUtil.indexOfSub查找bugissue#1683@Github
* 【core 】 修复Node的权重比较空指针问题issue#1681@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -239,7 +239,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
// 对key做映射映射后为null的忽略之
// 这里 copyOptions.editFieldName() 不能少否则导致 CopyOptions setFieldNameEditor 失效
final String providerKey = copyOptions.editFieldName(copyOptions.getMappedFieldName(fieldName, false));
final String providerKey = copyOptions.editFieldName(copyOptions.getMappedFieldName(fieldName, true));
if(null == providerKey){
return;
}

View File

@ -1,5 +1,7 @@
package cn.hutool.core.lang.tree;
import cn.hutool.core.comparator.CompareUtil;
import java.io.Serializable;
/**
@ -74,11 +76,11 @@ public interface Node<T> extends Comparable<Node<T>>, Serializable {
@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@Override
default int compareTo(Node node) {
final Comparable weight = this.getWeight();
if (null != weight) {
final Comparable weightOther = node.getWeight();
return weight.compareTo(weightOther);
if(null == node){
return 1;
}
return 0;
final Comparable weight = this.getWeight();
final Comparable weightOther = node.getWeight();
return CompareUtil.compare(weight, weightOther);
}
}

View File

@ -5,6 +5,7 @@ import cn.hutool.core.comparator.VersionComparator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
@ -4276,12 +4277,11 @@ public class CharSequenceUtil {
* @return 是否全部匹配
* @since 3.2.3
*/
public static boolean isAllCharMatch(CharSequence value, cn.hutool.core.lang.Matcher<Character> matcher) {
public static boolean isAllCharMatch(CharSequence value, Matcher<Character> matcher) {
if (StrUtil.isBlank(value)) {
return false;
}
int len = value.length();
for (int i = 0; i < len; i++) {
for (int i = value.length(); --i >= 0;) {
if (false == matcher.match(value.charAt(i))) {
return false;
}
@ -4289,6 +4289,17 @@ public class CharSequenceUtil {
return true;
}
/**
* 检查字符串是否都为数字组成
*
* @param str 字符串
* @return 是否都为数字组成
* @since 5.7.3
*/
public static boolean isNumeric(CharSequence str) {
return isAllCharMatch(str, Character::isDigit);
}
/**
* 循环位移指定位置的字符串为指定距离<br>
* 当moveLength大于0向右位移小于0向左位移0不位移<br>
@ -4329,4 +4340,14 @@ public class CharSequenceUtil {
return strBuilder.toString();
}
/**
* 检查给定字符串的所有字符是否都一样
*
* @param str 字符出啊
* @return 给定字符串的所有字符是否都一样
* @since 5.7.3
*/
public static boolean isCharEquals(String str) {
return isBlank(str.replace(str.charAt(0), CharUtil.SPACE));
}
}

View File

@ -0,0 +1,288 @@
package cn.hutool.core.text;
import cn.hutool.core.util.StrUtil;
/**
* 检测密码强度<br>
* 来自https://github.com/venshine/CheckPasswordStrength
*
* @author venshine
* @since 5.7.3
*/
public class PasswdStrength {
/**
* 密码等级枚举
*/
public enum PASSWD_LEVEL {
EASY, MIDIUM, STRONG, VERY_STRONG, EXTREMELY_STRONG
}
/**
* 字符类型枚举
*/
public enum CHAR_TYPE {
NUM, SMALL_LETTER, CAPITAL_LETTER, OTHER_CHAR
}
/**
* 简单密码字典
*/
private final static String[] DICTIONARY = {"password", "abc123", "iloveyou", "adobe123", "123123", "sunshine",
"1314520", "a1b2c3", "123qwe", "aaa111", "qweasd", "admin", "passwd"};
/**
* 数字长度
*/
private final static int[] SIZE_TABLE = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999,
Integer.MAX_VALUE};
/**
* 检查密码的健壮性
*
* @param passwd 密码
* @return strength level
*/
public static int check(String passwd) {
if (null == passwd) {
throw new IllegalArgumentException("password is empty");
}
int len = passwd.length();
int level = 0;
// increase points
if (countLetter(passwd, CHAR_TYPE.NUM) > 0) {
level++;
}
if (countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0) {
level++;
}
if (len > 4 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0) {
level++;
}
if (len > 6 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0) {
level++;
}
if (len > 4 && countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0
|| countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0
|| countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0
|| countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0
|| countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0
|| countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0) {
level++;
}
if (len > 6 && countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0 || countLetter(passwd, CHAR_TYPE.NUM) > 0
&& countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0
|| countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0
&& countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0 || countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0) {
level++;
}
if (len > 8 && countLetter(passwd, CHAR_TYPE.NUM) > 0 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) > 0
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) > 0 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) > 0) {
level++;
}
if (len > 6 && countLetter(passwd, CHAR_TYPE.NUM) >= 3 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 3
|| countLetter(passwd, CHAR_TYPE.NUM) >= 3 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 3
|| countLetter(passwd, CHAR_TYPE.NUM) >= 3 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2
|| countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 3 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 3
|| countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 3 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2
|| countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 3 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2) {
level++;
}
if (len > 8 && countLetter(passwd, CHAR_TYPE.NUM) >= 2 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 2
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 2 || countLetter(passwd, CHAR_TYPE.NUM) >= 2
&& countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 2 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2
|| countLetter(passwd, CHAR_TYPE.NUM) >= 2 && countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 2
&& countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2 || countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 2
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 2 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2) {
level++;
}
if (len > 10 && countLetter(passwd, CHAR_TYPE.NUM) >= 2 && countLetter(passwd, CHAR_TYPE.SMALL_LETTER) >= 2
&& countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) >= 2 && countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 2) {
level++;
}
if (countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 3) {
level++;
}
if (countLetter(passwd, CHAR_TYPE.OTHER_CHAR) >= 6) {
level++;
}
if (len > 12) {
level++;
if (len >= 16) {
level++;
}
}
// decrease points
if ("abcdefghijklmnopqrstuvwxyz".indexOf(passwd) > 0 || "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(passwd) > 0) {
level--;
}
if ("qwertyuiop".indexOf(passwd) > 0 || "asdfghjkl".indexOf(passwd) > 0 || "zxcvbnm".indexOf(passwd) > 0) {
level--;
}
if (StrUtil.isNumeric(passwd) && ("01234567890".indexOf(passwd) > 0 || "09876543210".indexOf(passwd) > 0)) {
level--;
}
if (countLetter(passwd, CHAR_TYPE.NUM) == len || countLetter(passwd, CHAR_TYPE.SMALL_LETTER) == len
|| countLetter(passwd, CHAR_TYPE.CAPITAL_LETTER) == len) {
level--;
}
if (len % 2 == 0) { // aaabbb
String part1 = passwd.substring(0, len / 2);
String part2 = passwd.substring(len / 2);
if (part1.equals(part2)) {
level--;
}
if (StrUtil.isCharEquals(part1) && StrUtil.isCharEquals(part2)) {
level--;
}
}
if (len % 3 == 0) { // ababab
String part1 = passwd.substring(0, len / 3);
String part2 = passwd.substring(len / 3, len / 3 * 2);
String part3 = passwd.substring(len / 3 * 2);
if (part1.equals(part2) && part2.equals(part3)) {
level--;
}
}
if (StrUtil.isNumeric(passwd) && len >= 6) { // 19881010 or 881010
int year = 0;
if (len == 8 || len == 6) {
year = Integer.parseInt(passwd.substring(0, len - 4));
}
int size = sizeOfInt(year);
int month = Integer.parseInt(passwd.substring(size, size + 2));
int day = Integer.parseInt(passwd.substring(size + 2, len));
if (year >= 1950 && year < 2050 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
level--;
}
}
for (String s : DICTIONARY) {
if (passwd.equals(s) || s.contains(passwd)) {
level--;
break;
}
}
if (len <= 6) {
level--;
if (len <= 4) {
level--;
if (len <= 3) {
level = 0;
}
}
}
if (StrUtil.isCharEquals(passwd)) {
level = 0;
}
if (level < 0) {
level = 0;
}
return level;
}
/**
* Get password strength level, includes easy, midium, strong, very strong, extremely strong
*
* @param passwd 密码
* @return 密码等级枚举
*/
public static PASSWD_LEVEL getLevel(String passwd) {
int level = check(passwd);
switch (level) {
case 0:
case 1:
case 2:
case 3:
return PASSWD_LEVEL.EASY;
case 4:
case 5:
case 6:
return PASSWD_LEVEL.MIDIUM;
case 7:
case 8:
case 9:
return PASSWD_LEVEL.STRONG;
case 10:
case 11:
case 12:
return PASSWD_LEVEL.VERY_STRONG;
default:
return PASSWD_LEVEL.EXTREMELY_STRONG;
}
}
/**
* Check character's type, includes num, capital letter, small letter and other character.
* 检查字符类型
*
* @param c 字符
* @return 类型
*/
private static CHAR_TYPE checkCharacterType(char c) {
if (c >= 48 && c <= 57) {
return CHAR_TYPE.NUM;
}
if (c >= 65 && c <= 90) {
return CHAR_TYPE.CAPITAL_LETTER;
}
if (c >= 97 && c <= 122) {
return CHAR_TYPE.SMALL_LETTER;
}
return CHAR_TYPE.OTHER_CHAR;
}
/**
* 计算密码中指定字符类型的数量
*
* @param passwd 密码
* @param type 类型
* @return 数量
*/
private static int countLetter(String passwd, CHAR_TYPE type) {
int count = 0;
if (null != passwd) {
final int length = passwd.length();
if (length > 0) {
for (int i = 0; i < length; i++) {
if (checkCharacterType(passwd.charAt(i)) == type) {
count++;
}
}
}
}
return count;
}
/**
* calculate the size of an integer number
*
* @param x
* @return 数字长度
*/
private static int sizeOfInt(int x) {
for (int i = 0; ; i++)
if (x <= SIZE_TABLE[i]) {
return i + 1;
}
}
}

View File

@ -183,8 +183,23 @@ public class ArrayUtil extends PrimitiveArrayUtil {
*/
@SuppressWarnings("unchecked")
public static <T> int matchIndex(Matcher<T> matcher, T... array) {
return matchIndex(matcher, 0, array);
}
/**
* 返回数组中第一个匹配规则的值的位置
*
* @param <T> 数组元素类型
* @param matcher 匹配接口实现此接口自定义匹配规则
* @param beginIndexInclude 检索开始的位置
* @param array 数组
* @return 匹配到元素的位置-1表示未匹配到
* @since 5.7.3
*/
@SuppressWarnings("unchecked")
public static <T> int matchIndex(Matcher<T> matcher, int beginIndexInclude, T... array) {
if (isNotEmpty(array)) {
for (int i = 0; i < array.length; i++) {
for (int i = beginIndexInclude; i < array.length; i++) {
if (matcher.match(array[i])) {
return i;
}
@ -728,6 +743,20 @@ public class ArrayUtil extends PrimitiveArrayUtil {
// ------------------------------------------------------------------- indexOf and lastIndexOf and contains
/**
* 返回数组中指定元素所在位置未找到返回{@link #INDEX_NOT_FOUND}
*
* @param <T> 数组类型
* @param array 数组
* @param value 被检查的元素
* @param beginIndexInclude 检索开始的位置
* @return 数组中指定元素所在位置未找到返回{@link #INDEX_NOT_FOUND}
* @since 3.0.7
*/
public static <T> int indexOf(T[] array, Object value, int beginIndexInclude) {
return matchIndex((obj) -> ObjectUtil.equal(value, obj), beginIndexInclude, array);
}
/**
* 返回数组中指定元素所在位置未找到返回{@link #INDEX_NOT_FOUND}
*
@ -770,8 +799,25 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.7
*/
public static <T> int lastIndexOf(T[] array, Object value) {
if (null != array) {
for (int i = array.length - 1; i >= 0; i--) {
if (isEmpty(array)) {
return INDEX_NOT_FOUND;
}
return lastIndexOf(array, value, array.length - 1);
}
/**
* 返回数组中指定元素所在最后的位置未找到返回{@link #INDEX_NOT_FOUND}
*
* @param <T> 数组类型
* @param array 数组
* @param value 被检查的元素
* @param endInclude 查找方式为从后向前查找查找的数组结束位置一般为array.length-1
* @return 数组中指定元素所在位置未找到返回{@link #INDEX_NOT_FOUND}
* @since 5.7.3
*/
public static <T> int lastIndexOf(T[] array, Object value, int endInclude) {
if (isNotEmpty(array)) {
for (int i = endInclude; i >= 0; i--) {
if (ObjectUtil.equal(value, array[i])) {
return i;
}
@ -1146,7 +1192,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 5.3.3
*/
public static <T> String join(T[] array, CharSequence conjunction, Editor<T> editor) {
return StrJoiner.of(conjunction).append(array, (t)-> String.valueOf(editor.edit(t))).toString();
return StrJoiner.of(conjunction).append(array, (t) -> String.valueOf(editor.edit(t))).toString();
}
/**
@ -1658,17 +1704,30 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 5.4.8
*/
public static <T> int indexOfSub(T[] array, T[] subArray) {
return indexOfSub(array, 0, subArray);
}
/**
* 查找子数组的位置
*
* @param array 数组
* @param subArray 子数组
* @param <T> 数组元素类型
* @return 子数组的开始位置即子数字第一个元素在数组中的位置
* @since 5.4.8
*/
public static <T> int indexOfSub(T[] array, int beginInclude, T[] subArray) {
if (isEmpty(array) || isEmpty(subArray) || subArray.length > array.length) {
return INDEX_NOT_FOUND;
}
int firstIndex = indexOf(array, subArray[0]);
int firstIndex = indexOf(array, subArray[0], beginInclude);
if (firstIndex < 0 || firstIndex + subArray.length > array.length) {
return INDEX_NOT_FOUND;
}
for (int i = 0; i < subArray.length; i++) {
if (false == ObjectUtil.equal(array[i + firstIndex], subArray[i])) {
return INDEX_NOT_FOUND;
return indexOfSub(array, firstIndex + 1, subArray);
}
}
@ -1685,7 +1744,23 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 5.4.8
*/
public static <T> int lastIndexOfSub(T[] array, T[] subArray) {
if (isEmpty(array) || isEmpty(subArray) || subArray.length > array.length) {
if (isEmpty(array) || isEmpty(subArray)) {
return INDEX_NOT_FOUND;
}
return lastIndexOfSub(array, array.length - 1, subArray);
}
/**
* 查找最后一个子数组的开始位置
*
* @param array 数组
* @param subArray 子数组
* @param <T> 数组元素类型
* @return 最后一个子数组的开始位置即子数字第一个元素在数组中的位置
* @since 5.4.8
*/
public static <T> int lastIndexOfSub(T[] array, int endInclude, T[] subArray) {
if (isEmpty(array) || isEmpty(subArray) || subArray.length > array.length || endInclude < 0) {
return INDEX_NOT_FOUND;
}
@ -1696,7 +1771,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
for (int i = 0; i < subArray.length; i++) {
if (false == ObjectUtil.equal(array[i + firstIndex], subArray[i])) {
return INDEX_NOT_FOUND;
return lastIndexOfSub(array, firstIndex - 1, subArray);
}
}

View File

@ -12,5 +12,4 @@ public class TreeBuilderTest {
of.build();
of.append(new ArrayList<>());
}
}

View File

@ -28,7 +28,7 @@ public class TreeTest {
@Test
public void sampleTree() {
public void sampleTreeTest() {
List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
for (Tree<String> tree : treeList) {
Assert.assertNotNull(tree);
@ -43,7 +43,7 @@ public class TreeTest {
}
@Test
public void tree() {
public void treeTest() {
//配置
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
@ -66,5 +66,4 @@ public class TreeTest {
Assert.assertEquals(treeNodes.size(), 2);
}
}

View File

@ -0,0 +1,12 @@
package cn.hutool.core.text;
import org.junit.Assert;
import org.junit.Test;
public class PasswdStrengthTest {
@Test
public void strengthTest(){
String passwd = "2hAj5#mne-ix.86H";
Assert.assertEquals(13, PasswdStrength.check(passwd));
}
}

View File

@ -356,6 +356,14 @@ public class ArrayUtilTest {
Assert.assertEquals(-1, i);
}
@Test
public void indexOfSubTest2(){
Integer[] a = {0x12, 0x56, 0x34, 0x56, 0x78, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
Assert.assertEquals(3, i);
}
@Test
public void lastIndexOfSubTest() {
Integer[] a = {0x12, 0x34, 0x56, 0x78, 0x9A};
@ -386,6 +394,14 @@ public class ArrayUtilTest {
Assert.assertEquals(-1, i);
}
@Test
public void lastIndexOfSubTest2(){
Integer[] a = {0x12, 0x56, 0x78, 0x56, 0x21, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
Assert.assertEquals(1, i);
}
@Test
public void reverseTest(){
int[] a = {1,2,3,4};

View File

@ -518,4 +518,16 @@ public class StrUtilTest {
Assert.assertEquals("jackduan@163.com", StrUtil.hide("jackduan@163.com", 16, 17));
}
@Test
public void isCharEqualsTest(){
String a = "aaaaaaaaa";
Assert.assertTrue(StrUtil.isCharEquals(a));
}
@Test
public void isNumericTest(){
String a = "2142342422423423";
Assert.assertTrue(StrUtil.isNumeric(a));
}
}