fix:避免调用方 显示调用API 触发查找树 优化;并通过内置锁,避免因并行树优化 可能造成的不可预知结果 和 无效重复的 树优化操作

This commit is contained in:
renyp
2023-03-17 09:55:04 +08:00
27 changed files with 643 additions and 371 deletions

View File

@@ -2,7 +2,7 @@ package cn.hutool.core.collection;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import cn.hutool.core.math.PageInfo;
import cn.hutool.core.lang.page.PageInfo;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@@ -12,11 +12,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.lang.invoke.LambdaConversionException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
@@ -24,6 +21,7 @@ import java.util.Comparator;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author nasodaengineer
@@ -160,6 +158,15 @@ public class LambdaFactoryTest {
loop(count, tasks);
}
@Test
public void testConstructor() {
Constructor<Something> constructor = ((SerSupplier<Constructor<Something>>) Something.class::getConstructor).get();
Supplier<Something> constructorLambda = LambdaFactory.build(Supplier.class, constructor);
// constructorLambda can be cache or transfer
Something something = constructorLambda.get();
Assert.assertEquals(Something.class, something.getClass());
}
/**
* <p>hardCode 运行1次耗时 7600 NANOSECONDS
* <p>lambda 运行1次耗时 12400 NANOSECONDS

View File

@@ -0,0 +1,37 @@
package cn.hutool.core.lang.page;
import org.junit.Assert;
import org.junit.Test;
public class NavigatePageInfoTest {
@Test
public void naviTest1(){
// 首页
final NavigatePageInfo navigatePageInfo = new NavigatePageInfo(10, 2, 6);
Assert.assertEquals("[1] 2 3 4 5 >>", navigatePageInfo.toString());
// 中间页
navigatePageInfo.nextPage();
Assert.assertEquals("<< 1 [2] 3 4 5 >>", navigatePageInfo.toString());
// 尾页
navigatePageInfo.setPageNo(5);
Assert.assertEquals("<< 1 2 3 4 [5]", navigatePageInfo.toString());
}
@Test
public void naviTest2(){
// 首页
final NavigatePageInfo navigatePageInfo = new NavigatePageInfo(10, 2, 4);
Assert.assertEquals("[1] 2 3 4 >>", navigatePageInfo.toString());
// 中间页
navigatePageInfo.nextPage();
Assert.assertEquals("<< 1 [2] 3 4 >>", navigatePageInfo.toString());
// 尾页
navigatePageInfo.setPageNo(5);
Assert.assertEquals("<< 2 3 4 [5]", navigatePageInfo.toString());
}
}

View File

@@ -1,4 +1,4 @@
package cn.hutool.core.math;
package cn.hutool.core.lang.page;
import org.junit.Assert;
import org.junit.Test;
@@ -7,10 +7,10 @@ public class PageInfoTest {
@Test
public void pagesTest() {
PageInfo pageInfo = new PageInfo(20, 3);
Assert.assertEquals(7, pageInfo.getPages());
Assert.assertEquals(7, pageInfo.getPageCount());
pageInfo = new PageInfo(20, 4);
Assert.assertEquals(5, pageInfo.getPages());
Assert.assertEquals(5, pageInfo.getPageCount());
}
@Test

View File

@@ -1,225 +0,0 @@
package cn.hutool.core.text.dfa;
import cn.hutool.core.date.StopWatch;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
public class AutomatonTest extends TestCase {
/**
* 密集匹配 测试查找结果并与WordTree对比效率
*/
public void testFind() {
Automaton automaton = new Automaton();
WordTree wordTree = new WordTree();
automaton.insert("say", "her", "he", "she", "shr");
// automaton.buildAc();
wordTree.addWords("say", "her", "he", "she", "shr");
StopWatch stopWatch = new StopWatch();
String input = "sasherhsay";
stopWatch.start("automaton_char_find");
List<FoundWord> ans1 = automaton.find(input);
stopWatch.stop();
assertEquals("she,he,her,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(2), ans1.get(0).getStartIndex());
assertEquals(Integer.valueOf(4), ans1.get(0).getEndIndex());
assertEquals(Integer.valueOf(3), ans1.get(1).getStartIndex());
assertEquals(Integer.valueOf(4), ans1.get(1).getEndIndex());
assertEquals(Integer.valueOf(3), ans1.get(2).getStartIndex());
assertEquals(Integer.valueOf(5), ans1.get(2).getEndIndex());
assertEquals(Integer.valueOf(7), ans1.get(3).getStartIndex());
assertEquals(Integer.valueOf(9), ans1.get(3).getEndIndex());
stopWatch.start("wordtree_char_find");
List<String> ans2 = wordTree.matchAll(input, -1, true, true);
stopWatch.stop();
assertEquals("she,he,her,say", String.join(",", ans2));
System.out.println(stopWatch.prettyPrint());
}
/**
* 非密集匹配 测试查找结果并与WordTree对比效率
*/
public void testFindNotDensity() {
Automaton automaton = new Automaton();
WordTree wordTree = new WordTree();
automaton.insert("say", "her", "he", "she", "shr");
// automaton.buildAc();
wordTree.addWords("say", "her", "he", "she", "shr");
StopWatch stopWatch = new StopWatch();
String input = "sasherhsay";
stopWatch.start("automaton_char_find_not_density");
List<FoundWord> ans1 = automaton.find(input, false);
stopWatch.stop();
assertEquals("she,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(2), ans1.get(0).getStartIndex());
assertEquals(Integer.valueOf(4), ans1.get(0).getEndIndex());
assertEquals(Integer.valueOf(7), ans1.get(1).getStartIndex());
assertEquals(Integer.valueOf(9), ans1.get(1).getEndIndex());
stopWatch.start("wordtree_char_find_not_density");
List<String> ans2 = wordTree.matchAll(input, -1, false, true);
stopWatch.stop();
assertEquals("she,say", String.join(",", ans2));
System.out.println(stopWatch.prettyPrint());
}
/**
* 密集匹配 测试建树和查找并与WordTree对比效率
*/
public void testBuildAndFind() {
StopWatch stopWatch = new StopWatch();
String input = "sasherhsay";
stopWatch.start("automaton_char_buid_find");
Automaton automatonLocal = new Automaton();
automatonLocal.insert("say", "her", "he", "she", "shr");
// automatonLocal.buildAc();
List<FoundWord> ans1 = automatonLocal.find(input);
stopWatch.stop();
assertEquals("she,he,her,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(2), ans1.get(0).getStartIndex());
assertEquals(Integer.valueOf(4), ans1.get(0).getEndIndex());
assertEquals(Integer.valueOf(3), ans1.get(1).getStartIndex());
assertEquals(Integer.valueOf(4), ans1.get(1).getEndIndex());
assertEquals(Integer.valueOf(3), ans1.get(2).getStartIndex());
assertEquals(Integer.valueOf(5), ans1.get(2).getEndIndex());
assertEquals(Integer.valueOf(7), ans1.get(3).getStartIndex());
assertEquals(Integer.valueOf(9), ans1.get(3).getEndIndex());
stopWatch.start("wordtree_char_build_find");
WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("say", "her", "he", "she", "shr");
List<String> ans2 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
assertEquals("she,he,her,say", String.join(",", ans2));
System.out.println(stopWatch.prettyPrint());
}
/**
* 密集匹配 构建树和查找 测试中文字符并与wordTree对比效率
*/
@Test
public void testBuildFindCnChar() {
StopWatch stopWatch = new StopWatch();
String input = "赵啊三在做什么";
stopWatch.start("automaton_cn_build_find");
Automaton automatonLocal = new Automaton();
automatonLocal.insert("", "赵啊", "赵啊三");
// automatonLocal.buildAc();
final List<FoundWord> result = automatonLocal.find(input);
stopWatch.stop();
Assert.assertEquals(3, result.size());
Assert.assertEquals("赵,赵啊,赵啊三", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(0), result.get(0).getStartIndex());
assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
assertEquals(Integer.valueOf(0), result.get(1).getStartIndex());
assertEquals(Integer.valueOf(1), result.get(1).getEndIndex());
assertEquals(Integer.valueOf(0), result.get(2).getStartIndex());
assertEquals(Integer.valueOf(2), result.get(2).getEndIndex());
stopWatch.start("wordtree_cn_build_find");
WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
final List<String> result1 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
Assert.assertEquals(3, result1.size());
Assert.assertEquals("赵,赵啊,赵啊三", String.join(",", result1));
System.out.println(stopWatch.prettyPrint());
}
/**
* 密集匹配 测试构建树和查找 中文字符并与wordTree对比效率
*/
@Test
public void testFindCNChar() {
StopWatch stopWatch = new StopWatch();
String input = "赵啊三在做什么";
Automaton automatonLocal = new Automaton();
automatonLocal.insert("", "赵啊", "赵啊三");
// automatonLocal.buildAc();
stopWatch.start("automaton_cn_find");
final List<FoundWord> result = automatonLocal.find(input);
stopWatch.stop();
Assert.assertEquals(3, result.size());
Assert.assertEquals("赵,赵啊,赵啊三", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(0), result.get(0).getStartIndex());
assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
assertEquals(Integer.valueOf(0), result.get(1).getStartIndex());
assertEquals(Integer.valueOf(1), result.get(1).getEndIndex());
assertEquals(Integer.valueOf(0), result.get(2).getStartIndex());
assertEquals(Integer.valueOf(2), result.get(2).getEndIndex());
WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
stopWatch.start("wordtree_cn_find");
final List<String> result1 = wordTreeLocal.matchAllWords(input, -1, true, true).stream().map(FoundWord::getWord)
.collect(Collectors.toList());
stopWatch.stop();
Assert.assertEquals(3, result1.size());
Assert.assertEquals("赵,赵啊,赵啊三", String.join(",", result1));
System.out.println(stopWatch.prettyPrint());
}
/**
* 非密集匹配 测试构建树和查找 中文字符并与wordTree对比效率
*/
@Test
public void testFindCNCharNotDensity() {
StopWatch stopWatch = new StopWatch();
String input = "赵啊三在做什么";
Automaton automatonLocal = new Automaton();
automatonLocal.insert("", "赵啊", "赵啊三");
// automatonLocal.buildAc();
stopWatch.start("automaton_cn_find_not_density");
final List<FoundWord> result = automatonLocal.find(input, false);
stopWatch.stop();
Assert.assertEquals(1, result.size());
Assert.assertEquals("", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
assertEquals(Integer.valueOf(0), result.get(0).getStartIndex());
assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
stopWatch.start("wordtree_cn_find_not_density");
final List<String> result1 =
wordTreeLocal.matchAllWords(input, -1, false, true).stream().map(FoundWord::getWord)
.collect(Collectors.toList());
stopWatch.stop();
Assert.assertEquals(1, result1.size());
Assert.assertEquals("", String.join(",", result1));
System.out.println(stopWatch.prettyPrint());
}
}

View File

@@ -0,0 +1,228 @@
package cn.hutool.core.text.dfa;
import cn.hutool.core.date.StopWatch;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
public class NFATest {
/**
* 密集匹配 测试查找结果并与WordTree对比效率
*/
@Test
public void testFind() {
final NFA NFA = new NFA();
NFA.insert("say", "her", "he", "she", "shr");
// NFA.buildAc();
final WordTree wordTree = new WordTree();
wordTree.addWords("say", "her", "he", "she", "shr");
final StopWatch stopWatch = new StopWatch();
final String input = "sasherhsay";
stopWatch.start("automaton_char_find");
final List<FoundWord> ans1 = NFA.find(input);
stopWatch.stop();
Assert.assertEquals("she,he,her,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(2, ans1.get(0).getBeginIndex().intValue());
Assert.assertEquals(4, ans1.get(0).getEndIndex().intValue());
Assert.assertEquals(3, ans1.get(1).getBeginIndex().intValue());
Assert.assertEquals(4, ans1.get(1).getEndIndex().intValue());
Assert.assertEquals(3, ans1.get(2).getBeginIndex().intValue());
Assert.assertEquals(5, ans1.get(2).getEndIndex().intValue());
Assert.assertEquals(7, ans1.get(3).getBeginIndex().intValue());
Assert.assertEquals(9, ans1.get(3).getEndIndex().intValue());
stopWatch.start("wordtree_char_find");
final List<String> ans2 = wordTree.matchAll(input, -1, true, true);
stopWatch.stop();
Assert.assertEquals("she,he,her,say", String.join(",", ans2));
//Console.log(stopWatch.prettyPrint());
}
/**
* 非密集匹配 测试查找结果并与WordTree对比效率
*/
@Test
public void testFindNotDensity() {
final NFA NFA = new NFA();
NFA.insert("say", "her", "he", "she", "shr");
// NFA.buildAc();
final WordTree wordTree = new WordTree();
wordTree.addWords("say", "her", "he", "she", "shr");
final StopWatch stopWatch = new StopWatch();
final String input = "sasherhsay";
stopWatch.start("automaton_char_find_not_density");
final List<FoundWord> ans1 = NFA.find(input, false);
stopWatch.stop();
Assert.assertEquals("she,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(2, ans1.get(0).getBeginIndex().intValue());
Assert.assertEquals(4, ans1.get(0).getEndIndex().intValue());
Assert.assertEquals(7, ans1.get(1).getBeginIndex().intValue());
Assert.assertEquals(9, ans1.get(1).getEndIndex().intValue());
stopWatch.start("wordtree_char_find_not_density");
final List<String> ans2 = wordTree.matchAll(input, -1, false, true);
stopWatch.stop();
Assert.assertEquals("she,say", String.join(",", ans2));
//Console.log(stopWatch.prettyPrint());
}
/**
* 密集匹配 测试建树和查找并与WordTree对比效率
*/
@Test
public void testBuildAndFind() {
final StopWatch stopWatch = new StopWatch();
final String input = "sasherhsay";
stopWatch.start("automaton_char_buid_find");
final NFA NFALocal = new NFA();
NFALocal.insert("say", "her", "he", "she", "shr");
// NFALocal.buildAc();
final List<FoundWord> ans1 = NFALocal.find(input);
stopWatch.stop();
Assert.assertEquals("she,he,her,say", ans1.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(2, ans1.get(0).getBeginIndex().intValue());
Assert.assertEquals(4, ans1.get(0).getEndIndex().intValue());
Assert.assertEquals(3, ans1.get(1).getBeginIndex().intValue());
Assert.assertEquals(4, ans1.get(1).getEndIndex().intValue());
Assert.assertEquals(3, ans1.get(2).getBeginIndex().intValue());
Assert.assertEquals(5, ans1.get(2).getEndIndex().intValue());
Assert.assertEquals(7, ans1.get(3).getBeginIndex().intValue());
Assert.assertEquals(9, ans1.get(3).getEndIndex().intValue());
stopWatch.start("wordtree_char_build_find");
final WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("say", "her", "he", "she", "shr");
final List<String> ans2 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
Assert.assertEquals("she,he,her,say", String.join(",", ans2));
//Console.log(stopWatch.prettyPrint());
}
/**
* 密集匹配 构建树和查找 测试中文字符并与wordTree对比效率
*/
@Test
public void buildFindCnCharTest() {
final StopWatch stopWatch = new StopWatch();
final String input = "赵啊三在做什么";
stopWatch.start("automaton_cn_build_find");
final NFA NFALocal = new NFA();
NFALocal.insert("", "赵啊", "赵啊三");
// NFALocal.buildAc();
final List<FoundWord> result = NFALocal.find(input);
stopWatch.stop();
Assert.assertEquals(3, result.size());
Assert.assertEquals("赵,赵啊,赵啊三", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(Integer.valueOf(0), result.get(0).getBeginIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(1).getBeginIndex());
Assert.assertEquals(Integer.valueOf(1), result.get(1).getEndIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(2).getBeginIndex());
Assert.assertEquals(Integer.valueOf(2), result.get(2).getEndIndex());
stopWatch.start("wordtree_cn_build_find");
final WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
final List<String> result1 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
Assert.assertEquals(3, result1.size());
Assert.assertEquals("赵,赵啊,赵啊三", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}
/**
* 密集匹配 测试构建树和查找 中文字符并与wordTree对比效率
*/
@Test
public void testFindCNChar() {
final StopWatch stopWatch = new StopWatch();
final String input = "赵啊三在做什么";
final NFA NFALocal = new NFA();
NFALocal.insert("", "赵啊", "赵啊三");
// NFALocal.buildAc();
stopWatch.start("automaton_cn_find");
final List<FoundWord> result = NFALocal.find(input);
stopWatch.stop();
Assert.assertEquals(3, result.size());
Assert.assertEquals("赵,赵啊,赵啊三", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(Integer.valueOf(0), result.get(0).getBeginIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(1).getBeginIndex());
Assert.assertEquals(Integer.valueOf(1), result.get(1).getEndIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(2).getBeginIndex());
Assert.assertEquals(Integer.valueOf(2), result.get(2).getEndIndex());
final WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
stopWatch.start("wordtree_cn_find");
final List<String> result1 = wordTreeLocal.matchAllWords(input, -1, true, true).stream().map(FoundWord::getWord)
.collect(Collectors.toList());
stopWatch.stop();
Assert.assertEquals(3, result1.size());
Assert.assertEquals("赵,赵啊,赵啊三", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}
/**
* 非密集匹配 测试构建树和查找 中文字符并与wordTree对比效率
*/
@Test
public void testFindCNCharNotDensity() {
final StopWatch stopWatch = new StopWatch();
final String input = "赵啊三在做什么";
final NFA NFALocal = new NFA();
NFALocal.insert("", "赵啊", "赵啊三");
// NFALocal.buildAc();
stopWatch.start("automaton_cn_find_not_density");
final List<FoundWord> result = NFALocal.find(input, false);
stopWatch.stop();
Assert.assertEquals(1, result.size());
Assert.assertEquals("", result.stream().map(FoundWord::getWord).collect(Collectors.joining(",")));
Assert.assertEquals(Integer.valueOf(0), result.get(0).getBeginIndex());
Assert.assertEquals(Integer.valueOf(0), result.get(0).getEndIndex());
final WordTree wordTreeLocal = new WordTree();
wordTreeLocal.addWords("", "赵啊", "赵啊三");
stopWatch.start("wordtree_cn_find_not_density");
final List<String> result1 =
wordTreeLocal.matchAllWords(input, -1, false, true).stream().map(FoundWord::getWord)
.collect(Collectors.toList());
stopWatch.stop();
Assert.assertEquals(1, result1.size());
Assert.assertEquals("", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}
}

View File

@@ -0,0 +1,48 @@
package cn.hutool.core.tree;
import cn.hutool.core.lang.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class IssueI6NR2ZTest {
@Test
public void getNodeTest() {
final List<TreeNode<Integer>> list = new ArrayList<>();
final TreeNode<Integer> treeNode1 = new TreeNode<>();
treeNode1.setId(1);
treeNode1.setParentId(0);
list.add(treeNode1);
final TreeNode<Integer> treeNode2 = new TreeNode<>();
treeNode2.setId(2);
treeNode2.setParentId(1);
list.add(treeNode2);
final TreeNode<Integer> treeNode3 = new TreeNode<>();
treeNode3.setId(3);
treeNode3.setParentId(1);
list.add(treeNode3);
final TreeNode<Integer> treeNode4 = new TreeNode<>();
treeNode4.setId(21);
treeNode4.setParentId(2);
list.add(treeNode4);
final TreeNode<Integer> treeNode5 = new TreeNode<>();
treeNode5.setId(31);
treeNode5.setParentId(3);
list.add(treeNode5);
final TreeNode<Integer> treeNode6 = new TreeNode<>();
treeNode6.setId(211);
treeNode6.setParentId(21);
list.add(treeNode6);
final List<MapTree<Integer>> build = TreeUtil.build(list);
final MapTree<Integer> node = TreeUtil.getNode(build.get(0), 31);
Assert.notNull(node);
}
}