result = new ArrayList<>();
final Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
final int startGroup = withGroup0 ? 0 : 1;
@@ -893,7 +893,6 @@ public class ReUtil {
/**
* 替换所有正则匹配的文本,并使用自定义函数决定如何替换
* replaceFun可以通过{@link Matcher}提取出匹配到的内容的不同部分,然后经过重新处理、组装变成新的内容放回原位。
- *
*
* replaceAll(this.content, "(\\d+)", parameters -> "-" + parameters.group(1) + "-")
* // 结果为:"ZZZaaabbbccc中文-1234-"
@@ -912,7 +911,6 @@ public class ReUtil {
/**
* 替换所有正则匹配的文本,并使用自定义函数决定如何替换
* replaceFun可以通过{@link Matcher}提取出匹配到的内容的不同部分,然后经过重新处理、组装变成新的内容放回原位。
- *
*
* replaceAll(this.content, "(\\d+)", parameters -> "-" + parameters.group(1) + "-")
* // 结果为:"ZZZaaabbbccc中文-1234-"
diff --git a/hutool-core/src/main/java/cn/hutool/core/text/dfa/NFA.java b/hutool-core/src/main/java/cn/hutool/core/text/dfa/NFA.java
new file mode 100644
index 000000000..468658a62
--- /dev/null
+++ b/hutool-core/src/main/java/cn/hutool/core/text/dfa/NFA.java
@@ -0,0 +1,145 @@
+package cn.hutool.core.text.dfa;
+
+import java.util.*;
+
+/**
+ *
+ *
+ * 基于非确定性有穷自动机(NFA) 实现的多模匹配工具
+ *
+ * @author renyp
+ */
+public class NFA {
+ private final Node root;
+
+ /**
+ * 默认构造
+ */
+ public NFA() {
+ this.root = new Node();
+ }
+
+ /**
+ * 构造函数 并 初始化词库
+ *
+ * @param words 添加的新词
+ */
+ public NFA(final String... words) {
+ this();
+ this.insert(words);
+ }
+
+ /**
+ * 词库添加新词,初始化查找树
+ *
+ * @param word 添加的新词
+ */
+ public void insert(final String word) {
+ Node p = root;
+ for (final char curr : word.toCharArray()) {
+ if (p.next.get((int) curr) == null) {
+ p.next.put((int) curr, new Node());
+ }
+ p = p.next.get((int) curr);
+ }
+ p.flag = true;
+ p.str = word;
+ }
+
+ /**
+ * 词库批量添加新词,初始化查找树
+ *
+ * @param words 添加的新词
+ */
+ public void insert(final String... words) {
+ for (final String word : words) {
+ this.insert(word);
+ }
+ }
+
+ /**
+ * 构建基于NFA模型的 AC自动机
+ */
+ public void buildAc() {
+ final Queue queue = new LinkedList<>();
+ final Node p = root;
+ for (final Integer key : p.next.keySet()) {
+ p.next.get(key).fail = root;
+ queue.offer(p.next.get(key));
+ }
+ while (!queue.isEmpty()) {
+ final Node curr = queue.poll();
+ for (final Integer key : curr.next.keySet()) {
+ Node fail = curr.fail;
+ // 查找当前节点匹配失败,他对应等效匹配的节点是哪个
+ while (fail != null && fail.next.get(key) == null) {
+ fail = fail.fail;
+ }
+ // 代码到这,有两种可能,fail不为null,说明找到了fail;fail为null,没有找到,那么就把fail指向root节点(当到该节点匹配失败,那么从root节点开始重新匹配)
+ if (fail != null) {
+ fail = fail.next.get(key);
+ } else {
+ fail = root;
+ }
+ curr.next.get(key).fail = fail;
+ queue.offer(curr.next.get(key));
+ }
+ }
+ }
+
+ /**
+ * @param text 查询的文本(母串)
+ * @return 关键字列表
+ */
+ public List find(final String text) {
+ return this.find(text, true);
+ }
+
+ /**
+ * @param text 查找的文本(母串)
+ * @param isDensityMatch 是否密集匹配
+ * @return 关键字列表
+ */
+ public List find(final String text, final boolean isDensityMatch) {
+ final List ans = new ArrayList<>();
+ Node p = root, k;
+ for (int i = 0, len = text.length(); i < len; i++) {
+ final int ind = text.charAt(i);
+ // 状态转移(沿着fail指针链接的链表,此处区别于DFA模型)
+ while (p != null && p.next.get(ind) == null) {
+ p = p.fail;
+ }
+ if (p == null) {
+ p = root;
+ } else {
+ p = p.next.get(ind);
+ }
+ // 提取结果(沿着fail指针链接的链表,此处区别于DFA模型)
+ k = p;
+ while (k != null) {
+ if (k.flag) {
+ ans.add(new FoundWord(k.str, k.str, i - k.str.length() + 1, i));
+ if (!isDensityMatch) {
+ p = root;
+ break;
+ }
+ }
+ k = k.fail;
+ }
+ }
+ return ans;
+ }
+
+ private static class Node {
+
+ boolean flag;
+ Node fail;
+ String str;
+ Map next;
+
+ public Node() {
+ this.flag = false;
+ next = new HashMap<>();
+ }
+ }
+}
diff --git a/hutool-core/src/main/java/cn/hutool/core/text/dfa/SensitiveUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/dfa/SensitiveUtil.java
index 07e1cb64e..ba03bb2a6 100755
--- a/hutool-core/src/main/java/cn/hutool/core/text/dfa/SensitiveUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/text/dfa/SensitiveUtil.java
@@ -171,7 +171,7 @@ public final class SensitiveUtil {
} : sensitiveProcessor;
final Map foundWordMap = new HashMap<>(foundWordList.size(), 1);
- foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
+ foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getBeginIndex(), foundWord));
final int length = text.length();
final StringBuilder textStringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
diff --git a/hutool-core/src/main/java/cn/hutool/core/text/split/SplitUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/split/SplitUtil.java
index e6505b214..0836700b4 100644
--- a/hutool-core/src/main/java/cn/hutool/core/text/split/SplitUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/text/split/SplitUtil.java
@@ -1,5 +1,6 @@
package cn.hutool.core.text.split;
+import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.regex.PatternPool;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.text.finder.CharFinder;
@@ -302,7 +303,8 @@ public class SplitUtil {
/**
* 切分字符串
- * 如果为空字符串或者null 则返回空集合
+ * 如果提供的字符串为{@code null},则返回一个空的{@link ArrayList}
+ * 如果提供的字符串为"",则当ignoreEmpty时返回空的{@link ArrayList},否则返回只有一个""元素的{@link ArrayList}
*
* @param text 被切分的字符串
* @param separator 分隔符字符串
@@ -314,8 +316,10 @@ public class SplitUtil {
* @since 3.2.1
*/
public static List split(final CharSequence text, final String separator, final int limit, final boolean isTrim, final boolean ignoreEmpty, final boolean ignoreCase) {
- if (StrUtil.isEmpty(text)) {
+ if(null == text){
return new ArrayList<>(0);
+ } else if (0 == text.length()) {
+ return ignoreEmpty ? new ArrayList<>(0) : ListUtil.of(StrUtil.EMPTY);
}
final SplitIter splitIter = new SplitIter(text, new StrFinder(separator, ignoreCase), limit, ignoreEmpty);
return splitIter.toList(isTrim);
diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java b/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java
index de79b82eb..17d126d08 100644
--- a/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java
@@ -140,7 +140,7 @@ public class ThreadUtil {
*
* 1. 核心线程数与最大线程数为nThreads指定的大小
* 2. 默认使用LinkedBlockingQueue,默认队列大小为1024
- * 3. 如果isBlocked为{code true},当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被{@link Thread#interrupt()}中断
+ * 3. 如果isBlocked为{@code true},当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被{@link Thread#interrupt()}中断
*
*
* @param nThreads 线程池大小
@@ -159,7 +159,7 @@ public class ThreadUtil {
*
* 1. 核心线程数与最大线程数为nThreads指定的大小
* 2. 默认使用LinkedBlockingQueue
- * 3. 如果isBlocked为{code true},当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被{@link Thread#interrupt()}中断
+ * 3. 如果isBlocked为{@code true},当执行拒绝策略的时候会处于阻塞状态,直到能添加到队列中或者被{@link Thread#interrupt()}中断
*
*
* @param nThreads 线程池大小
diff --git a/hutool-core/src/main/java/cn/hutool/core/util/JNDIUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/JNDIUtil.java
index aca69c6cc..a1b9175ed 100755
--- a/hutool-core/src/main/java/cn/hutool/core/util/JNDIUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/util/JNDIUtil.java
@@ -28,7 +28,7 @@ public class JNDIUtil {
/**
* 创建{@link InitialDirContext}
*
- * @param environment 环境参数,{code null}表示无参数
+ * @param environment 环境参数,{@code null}表示无参数
* @return {@link InitialDirContext}
*/
public static InitialDirContext createInitialDirContext(final Map environment) {
@@ -45,7 +45,7 @@ public class JNDIUtil {
/**
* 创建{@link InitialContext}
*
- * @param environment 环境参数,{code null}表示无参数
+ * @param environment 环境参数,{@code null}表示无参数
* @return {@link InitialContext}
*/
public static InitialContext createInitialContext(final Map environment) {
diff --git a/hutool-core/src/main/java/cn/hutool/core/util/PageUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/PageUtil.java
deleted file mode 100644
index 3c49f1d5e..000000000
--- a/hutool-core/src/main/java/cn/hutool/core/util/PageUtil.java
+++ /dev/null
@@ -1,273 +0,0 @@
-package cn.hutool.core.util;
-
-import cn.hutool.core.lang.DefaultSegment;
-import cn.hutool.core.lang.Segment;
-
-/**
- * 分页工具类
- *
- * @author xiaoleilu
- */
-public class PageUtil {
-
- private static int firstPageNo = 0;
-
- /**
- * 获得首页的页码,可以为0或者1
- *
- * @return 首页页码
- */
- public static int getFirstPageNo() {
- return firstPageNo;
- }
-
- /**
- * 设置首页页码,可以为0或者1
- *
- *
- * 当设置为0时,页码0表示第一页,开始位置为0
- * 当设置为1时,页码1表示第一页,开始位置为0
- *
- *
- * @param customFirstPageNo 自定义的首页页码,为0或者1
- */
- synchronized public static void setFirstPageNo(final int customFirstPageNo) {
- firstPageNo = customFirstPageNo;
- }
-
- /**
- * 设置首页页码为1
- *
- *
- * 当设置为1时,页码1表示第一页,开始位置为0
- *
- */
- public static void setOneAsFirstPageNo() {
- setFirstPageNo(1);
- }
-
- /**
- * 将页数和每页条目数转换为开始位置
- * 此方法用于不包括结束位置的分页方法
- * 例如:
- *
- *
- * 页码:0,每页10 =》 0
- * 页码:1,每页10 =》 10
- * ……
- *
- *
- *
- * 当{@link #setFirstPageNo(int)}设置为1时:
- *
- * 页码:1,每页10 =》 0
- * 页码:2,每页10 =》 10
- * ……
- *
- *
- * @param pageNo 页码(从0计数)
- * @param pageSize 每页条目数
- * @return 开始位置
- */
- public static int getStart(int pageNo, int pageSize) {
- if (pageNo < firstPageNo) {
- pageNo = firstPageNo;
- }
-
- if (pageSize < 1) {
- pageSize = 0;
- }
-
- return (pageNo - firstPageNo) * pageSize;
- }
-
- /**
- * 将页数和每页条目数转换为结束位置
- * 此方法用于不包括结束位置的分页方法
- * 例如:
- *
- *
- * 页码:0,每页10 =》 9
- * 页码:1,每页10 =》 19
- * ……
- *
- *
- *
- * 当{@link #setFirstPageNo(int)}设置为1时:
- *
- * 页码:1,每页10 =》 9
- * 页码:2,每页10 =》 19
- * ……
- *
- *
- * @param pageNo 页码(从0计数)
- * @param pageSize 每页条目数
- * @return 开始位置
- * @since 5.2.5
- */
- public static int getEnd(final int pageNo, final int pageSize) {
- final int start = getStart(pageNo, pageSize);
- return getEndByStart(start, pageSize);
- }
-
- /**
- * 将页数和每页条目数转换为开始位置和结束位置
- * 此方法用于包括结束位置的分页方法
- * 例如:
- *
- *
- * 页码:0,每页10 =》 [0, 10]
- * 页码:1,每页10 =》 [10, 20]
- * ……
- *
- *
- *
- * 当{@link #setFirstPageNo(int)}设置为1时:
- *
- * 页码:1,每页10 =》 [0, 10]
- * 页码:2,每页10 =》 [10, 20]
- * ……
- *
- *
- * @param pageNo 页码(从0计数)
- * @param pageSize 每页条目数
- * @return 第一个数为开始位置,第二个数为结束位置
- */
- public static int[] transToStartEnd(final int pageNo, final int pageSize) {
- final int start = getStart(pageNo, pageSize);
- return new int[]{start, getEndByStart(start, pageSize)};
- }
-
- /**
- * 将页数和每页条目数转换为开始位置和结束位置
- * 此方法用于包括结束位置的分页方法
- * 例如:
- *
- *
- * 页码:0,每页10 =》 [0, 10]
- * 页码:1,每页10 =》 [10, 20]
- * ……
- *
- *
- *
- * 当{@link #setFirstPageNo(int)}设置为1时:
- *
- * 页码:1,每页10 =》 [0, 10]
- * 页码:2,每页10 =》 [10, 20]
- * ……
- *
- *
- * @param pageNo 页码(从0计数)
- * @param pageSize 每页条目数
- * @return {@link Segment}
- * @since 5.5.3
- */
- public static Segment toSegment(final int pageNo, final int pageSize) {
- final int[] startEnd = transToStartEnd(pageNo, pageSize);
- return new DefaultSegment<>(startEnd[0], startEnd[1]);
- }
-
- /**
- * 根据总数计算总页数
- *
- * @param totalCount 总数
- * @param pageSize 每页数
- * @return 总页数
- */
- public static int totalPage(final int totalCount, final int pageSize) {
- return totalPage((long) totalCount, pageSize);
- }
-
- /**
- * 根据总数计算总页数
- *
- * @param totalCount 总数
- * @param pageSize 每页数
- * @return 总页数
- * @since 5.8.5
- */
- public static int totalPage(final long totalCount, final int pageSize) {
- if (pageSize == 0) {
- return 0;
- }
- return Math.toIntExact(totalCount % pageSize == 0
- ? (totalCount / pageSize) : (totalCount / pageSize + 1));
- }
-
- /**
- * 分页彩虹算法
- * 来自:https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java
- * 通过传入的信息,生成一个分页列表显示
- *
- * @param pageNo 当前页
- * @param totalPage 总页数
- * @param displayCount 每屏展示的页数
- * @return 分页条
- */
- public static int[] rainbow(final int pageNo, final int totalPage, final int displayCount) {
- // displayCount % 2
- final boolean isEven = (displayCount & 1) == 0;
- final int left = displayCount >> 1;
- int right = displayCount >> 1;
-
- int length = displayCount;
- if (isEven) {
- right++;
- }
- if (totalPage < displayCount) {
- length = totalPage;
- }
- final int[] result = new int[length];
- if (totalPage >= displayCount) {
- if (pageNo <= left) {
- for (int i = 0; i < result.length; i++) {
- result[i] = i + 1;
- }
- } else if (pageNo > totalPage - right) {
- for (int i = 0; i < result.length; i++) {
- result[i] = i + totalPage - displayCount + 1;
- }
- } else {
- for (int i = 0; i < result.length; i++) {
- result[i] = i + pageNo - left + (isEven ? 1 : 0);
- }
- }
- } else {
- for (int i = 0; i < result.length; i++) {
- result[i] = i + 1;
- }
- }
- return result;
-
- }
-
- /**
- * 分页彩虹算法(默认展示10页)
- * 来自:https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java
- *
- * @param currentPage 当前页
- * @param pageCount 总页数
- * @return 分页条
- */
- public static int[] rainbow(final int currentPage, final int pageCount) {
- return rainbow(currentPage, pageCount, 10);
- }
-
- //------------------------------------------------------------------------- Private method start
-
- /**
- * 根据起始位置获取结束位置
- *
- * @param start 起始位置
- * @param pageSize 每页条目数
- * @return 结束位置
- */
- private static int getEndByStart(final int start, int pageSize) {
- if (pageSize < 1) {
- pageSize = 0;
- }
- return start + pageSize;
- }
-
- //------------------------------------------------------------------------- Private method end
-}
diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
index fc5f4a2b4..f87b0586e 100755
--- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java
@@ -427,6 +427,26 @@ public class CollUtilTest {
Assert.assertEquals("李四", groupByField.get(1).get(0).getName());
}
+ @Test
+ public void groupByFuncTest() {
+ final List list = ListUtil.of(new TestBean("张三", 12), new TestBean("李四", 13), new TestBean("王五", 12));
+ final List> groupByField = CollUtil.groupByFunc(list, TestBean::getAge);
+ Assert.assertEquals("张三", groupByField.get(0).get(0).getName());
+ Assert.assertEquals("王五", groupByField.get(0).get(1).getName());
+
+ Assert.assertEquals("李四", groupByField.get(1).get(0).getName());
+ }
+
+ @Test
+ public void groupByFunc2Test() {
+ final List list = ListUtil.of(new TestBean("张三", 12), new TestBean("李四", 13), new TestBean("王五", 12));
+ final List> groupByField = CollUtil.groupByFunc(list, a -> a.getAge() > 12);
+ Assert.assertEquals("张三", groupByField.get(0).get(0).getName());
+ Assert.assertEquals("王五", groupByField.get(0).get(1).getName());
+
+ Assert.assertEquals("李四", groupByField.get(1).get(0).getName());
+ }
+
@Test
public void sortByPropertyTest() {
final List list = ListUtil.of(
@@ -844,7 +864,7 @@ public class CollUtilTest {
objects.add(Dict.of().set("name", "姓名:" + i));
}
- Assert.assertEquals(0, CollUtil.page(3, 5, objects).size());
+ Assert.assertEquals(0, ListUtil.page(objects, 3, 5).size());
}
@Test
@@ -854,7 +874,7 @@ public class CollUtilTest {
final List result = CollUtil.subtractToList(list1, list2);
Assert.assertEquals(1, result.size());
- Assert.assertEquals(1L, (long)result.get(0));
+ Assert.assertEquals(1L, (long) result.get(0));
}
@Test
diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java
index c8d330e88..e4e40d0ac 100644
--- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java
@@ -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.util.PageUtil;
+import cn.hutool.core.math.PageInfo;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -10,11 +10,7 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class ListUtilTest {
@@ -105,51 +101,32 @@ public class ListUtilTest {
}
@Test
- public void pageTest() {
+ public void pageTest1() {
final List a = ListUtil.ofLinked(1, 2, 3, 4, 5);
- PageUtil.setFirstPageNo(1);
- final int[] a_1 = ListUtil.page(1, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] a1 = ListUtil.page(1, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] a2 = ListUtil.page(2, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] a3 = ListUtil.page(3, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] a4 = ListUtil.page(4, 2, a).stream().mapToInt(Integer::valueOf).toArray();
+ final int[] a_1 = ListUtil.page(a, 0, 2).stream().mapToInt(Integer::valueOf).toArray();
+ final int[] a1 = ListUtil.page(a, 0, 2).stream().mapToInt(Integer::valueOf).toArray();
+ final int[] a2 = ListUtil.page(a, 1, 2).stream().mapToInt(Integer::valueOf).toArray();
+ final int[] a3 = ListUtil.page(a, 2, 2).stream().mapToInt(Integer::valueOf).toArray();
+ final int[] a4 = ListUtil.page(a, 3, 2).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1, 2}, a_1);
Assert.assertArrayEquals(new int[]{1, 2}, a1);
Assert.assertArrayEquals(new int[]{3, 4}, a2);
Assert.assertArrayEquals(new int[]{5}, a3);
Assert.assertArrayEquals(new int[]{}, a4);
+ }
-
- PageUtil.setFirstPageNo(2);
- final int[] b_1 = ListUtil.page(1, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] b1 = ListUtil.page(2, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] b2 = ListUtil.page(3, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] b3 = ListUtil.page(4, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] b4 = ListUtil.page(5, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- Assert.assertArrayEquals(new int[]{1, 2}, b_1);
- Assert.assertArrayEquals(new int[]{1, 2}, b1);
- Assert.assertArrayEquals(new int[]{3, 4}, b2);
- Assert.assertArrayEquals(new int[]{5}, b3);
- Assert.assertArrayEquals(new int[]{}, b4);
-
- PageUtil.setFirstPageNo(0);
- final int[] c_1 = ListUtil.page(-1, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] c1 = ListUtil.page(0, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] c2 = ListUtil.page(1, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] c3 = ListUtil.page(2, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- final int[] c4 = ListUtil.page(3, 2, a).stream().mapToInt(Integer::valueOf).toArray();
- Assert.assertArrayEquals(new int[]{1, 2}, c_1);
- Assert.assertArrayEquals(new int[]{1, 2}, c1);
- Assert.assertArrayEquals(new int[]{3, 4}, c2);
- Assert.assertArrayEquals(new int[]{5}, c3);
- Assert.assertArrayEquals(new int[]{}, c4);
-
-
- PageUtil.setFirstPageNo(1);
- final int[] d1 = ListUtil.page(0, 8, a).stream().mapToInt(Integer::valueOf).toArray();
+ @Test
+ public void pageTest2() {
+ final List a = ListUtil.ofLinked(1, 2, 3, 4, 5);
+ final int[] d1 = ListUtil.page(a, PageInfo.of(a.size(), 8).setFirstPageNo(0).setPageNo(0))
+ .stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, d1);
+ }
+ @Test
+ public void pageTest3() {
+ final List a = ListUtil.ofLinked(1, 2, 3, 4, 5);
// page with consumer
final List> pageListData = new ArrayList<>();
ListUtil.page(a, 2, pageListData::add);
@@ -168,9 +145,6 @@ public class ListUtilTest {
Assert.assertArrayEquals(new int[]{}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray());
Assert.assertArrayEquals(new int[]{3, 4}, pageListData.get(1).stream().mapToInt(Integer::valueOf).toArray());
Assert.assertArrayEquals(new int[]{5}, pageListData.get(2).stream().mapToInt(Integer::valueOf).toArray());
-
- // 恢复默认值,避免影响其他测试用例
- PageUtil.setFirstPageNo(0);
}
@Test
diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateTimeTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateTimeTest.java
index 8729ec3ec..4a9122342 100644
--- a/hutool-core/src/test/java/cn/hutool/core/date/DateTimeTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/date/DateTimeTest.java
@@ -109,10 +109,10 @@ public class DateTimeTest {
public void toStringTest2() {
final DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
- String dateStr = dateTime.toString(DatePattern.UTC_WITH_ZONE_OFFSET_PATTERN);
+ String dateStr = dateTime.toString(DatePattern.ISO8601_WITH_ZONE_OFFSET_PATTERN);
Assert.assertEquals("2017-01-05T12:34:23+0800", dateStr);
- dateStr = dateTime.toString(DatePattern.UTC_WITH_XXX_OFFSET_PATTERN);
+ dateStr = dateTime.toString(DatePattern.ISO8601_WITH_XXX_OFFSET_PATTERN);
Assert.assertEquals("2017-01-05T12:34:23+08:00", dateStr);
}
diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java
index 9d2bafeff..167c59c8e 100755
--- a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java
@@ -1,6 +1,5 @@
package cn.hutool.core.date;
-import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.BetweenFormatter.Level;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.lang.Console;
@@ -14,15 +13,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Locale;
-import java.util.Objects;
-import java.util.TimeZone;
+import java.util.*;
/**
* 时间工具单元测试
@@ -796,23 +787,6 @@ public class DateUtilTest {
Assert.assertEquals(0, DateUtil.compare(date11, date22, DatePattern.NORM_MONTH_PATTERN));
}
- @Test
- public void yearAndQTest() {
- final String yearAndQuarter = DateUtil.yearAndQuarter(DateUtil.parse("2018-12-01"));
- Assert.assertEquals("20184", yearAndQuarter);
-
- final LinkedHashSet yearAndQuarters = DateUtil.yearAndQuarter(DateUtil.parse("2018-09-10"), DateUtil.parse("2018-12-20"));
- final List list = ListUtil.of(false, yearAndQuarters);
- Assert.assertEquals(2, list.size());
- Assert.assertEquals("20183", list.get(0));
- Assert.assertEquals("20184", list.get(1));
-
- final LinkedHashSet yearAndQuarters2 = DateUtil.yearAndQuarter(DateUtil.parse("2018-10-10"), DateUtil.parse("2018-12-10"));
- final List list2 = ListUtil.of(false, yearAndQuarters2);
- Assert.assertEquals(1, list2.size());
- Assert.assertEquals("20184", list2.get(0));
- }
-
@Test
public void formatHttpDateTest() {
final String formatHttpDate = DateUtil.formatHttpDate(DateUtil.parse("2019-01-02 22:32:01"));
diff --git a/hutool-core/src/test/java/cn/hutool/core/date/Issue2981Test.java b/hutool-core/src/test/java/cn/hutool/core/date/Issue2981Test.java
new file mode 100755
index 000000000..432459ff0
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/date/Issue2981Test.java
@@ -0,0 +1,22 @@
+package cn.hutool.core.date;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class Issue2981Test {
+ /**
+ * https://github.com/dromara/hutool/issues/2981
+ * 按照ISO8601规范,以Z结尾表示UTC时间,否则为当地时间
+ */
+ @SuppressWarnings("DataFlowIssue")
+ @Test
+ public void parseUTCTest() {
+ final String str1 = "2019-01-01T00:00:00.000Z";
+ final String str2 = "2019-01-01T00:00:00.000";
+ final String str3 = "2019-01-01 00:00:00.000";
+
+ Assert.assertEquals(1546300800000L, DateUtil.parse(str1).getTime());
+ Assert.assertEquals(1546272000000L, DateUtil.parse(str2).getTime());
+ Assert.assertEquals(1546272000000L, DateUtil.parse(str3).getTime());
+ }
+}
diff --git a/hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java
index 75f8f8aa1..3ff41ae9f 100644
--- a/hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java
@@ -27,12 +27,22 @@ public class TimeUtilTest {
final String dateStr = "2020-01-23T12:23:56";
final DateTime dt = DateUtil.parse(dateStr);
- LocalDateTime of = TimeUtil.of(dt);
+ final LocalDateTime of = TimeUtil.of(dt);
Assert.assertNotNull(of);
Assert.assertEquals(dateStr, of.toString());
+ }
- of = TimeUtil.ofUTC(dt.getTime());
- Assert.assertEquals(dateStr, of.toString());
+ @SuppressWarnings("DataFlowIssue")
+ @Test
+ public void ofUTCTest() {
+ final String dateStr = "2020-01-23T12:23:56Z";
+ final DateTime dt = DateUtil.parse(dateStr);
+
+ final LocalDateTime of = TimeUtil.of(dt);
+ final LocalDateTime of2 = TimeUtil.ofUTC(dt.getTime());
+ Assert.assertNotNull(of);
+ Assert.assertNotNull(of2);
+ Assert.assertEquals(of, of2);
}
@Test
diff --git a/hutool-core/src/test/java/cn/hutool/core/math/PageInfoTest.java b/hutool-core/src/test/java/cn/hutool/core/math/PageInfoTest.java
new file mode 100644
index 000000000..a5778a98f
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/math/PageInfoTest.java
@@ -0,0 +1,32 @@
+package cn.hutool.core.math;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PageInfoTest {
+ @Test
+ public void pagesTest() {
+ PageInfo pageInfo = new PageInfo(20, 3);
+ Assert.assertEquals(7, pageInfo.getPages());
+
+ pageInfo = new PageInfo(20, 4);
+ Assert.assertEquals(5, pageInfo.getPages());
+ }
+
+ @Test
+ public void getSegmentTest() {
+ final PageInfo page = PageInfo.of(20, 10);
+ Assert.assertEquals("[0, 9]", page.getSegment().toString());
+ Assert.assertEquals("[10, 19]", page.nextPage().getSegment().toString());
+ Assert.assertEquals("[20, 20]", page.nextPage().getSegment().toString());
+ }
+
+ @Test
+ public void getSegmentTest2() {
+ final PageInfo page = PageInfo.of(20, 10);
+ page.setFirstPageNo(0).setPageNo(0);
+ Assert.assertEquals("[0, 9]", page.getSegment().toString());
+ Assert.assertEquals("[10, 19]", page.nextPage().getSegment().toString());
+ Assert.assertEquals("[20, 20]", page.nextPage().getSegment().toString());
+ }
+}
diff --git a/hutool-core/src/test/java/cn/hutool/core/text/dfa/DfaTest.java b/hutool-core/src/test/java/cn/hutool/core/text/dfa/DfaTest.java
index 3847800db..739f02fd0 100755
--- a/hutool-core/src/test/java/cn/hutool/core/text/dfa/DfaTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/text/dfa/DfaTest.java
@@ -91,15 +91,15 @@ public class DfaTest {
Assert.assertEquals(3, result.size());
Assert.assertEquals("赵", result.get(0).getWord());
- Assert.assertEquals(0, result.get(0).getStartIndex().intValue());
+ Assert.assertEquals(0, result.get(0).getBeginIndex().intValue());
Assert.assertEquals(0, result.get(0).getEndIndex().intValue());
Assert.assertEquals("赵阿", result.get(1).getWord());
- Assert.assertEquals(0, result.get(1).getStartIndex().intValue());
+ Assert.assertEquals(0, result.get(1).getBeginIndex().intValue());
Assert.assertEquals(1, result.get(1).getEndIndex().intValue());
Assert.assertEquals("赵阿三", result.get(2).getWord());
- Assert.assertEquals(0, result.get(2).getStartIndex().intValue());
+ Assert.assertEquals(0, result.get(2).getBeginIndex().intValue());
Assert.assertEquals(2, result.get(2).getEndIndex().intValue());
}
diff --git a/hutool-core/src/test/java/cn/hutool/core/text/dfa/NFATest.java b/hutool-core/src/test/java/cn/hutool/core/text/dfa/NFATest.java
new file mode 100644
index 000000000..e38be87f3
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/text/dfa/NFATest.java
@@ -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 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 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 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 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 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 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 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 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 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 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 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 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());
+ }
+}
diff --git a/hutool-core/src/test/java/cn/hutool/core/text/split/StrSplitterTest.java b/hutool-core/src/test/java/cn/hutool/core/text/split/StrSplitterTest.java
index 0cea8de30..69a059374 100644
--- a/hutool-core/src/test/java/cn/hutool/core/text/split/StrSplitterTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/text/split/StrSplitterTest.java
@@ -1,5 +1,6 @@
package cn.hutool.core.text.split;
+import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@@ -59,16 +60,25 @@ public class StrSplitterTest {
final String str = "";
final String[] split = str.split(",");
final String[] strings = SplitUtil.splitToArray(str, ",", -1, false, false);
+
Assert.assertNotNull(strings);
Assert.assertArrayEquals(split, strings);
+
+ final String[] strings2 = SplitUtil.splitToArray(str, ",", -1, false, true);
+ Assert.assertEquals(0, strings2.length);
}
+ @SuppressWarnings("ConstantValue")
@Test
public void splitNullTest(){
final String str = null;
final String[] strings = SplitUtil.splitToArray(str, ",", -1, false, false);
Assert.assertNotNull(strings);
Assert.assertEquals(0, strings.length);
+
+ final String[] strings2 = SplitUtil.splitToArray(str, ",", -1, false, true);
+ Assert.assertNotNull(strings2);
+ Assert.assertEquals(0, strings2.length);
}
/**
diff --git a/hutool-core/src/test/java/cn/hutool/core/util/PageUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/PageUtilTest.java
deleted file mode 100644
index 10ec440db..000000000
--- a/hutool-core/src/test/java/cn/hutool/core/util/PageUtilTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package cn.hutool.core.util;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * 分页单元测试
- *
- * @author Looly
- */
-public class PageUtilTest {
-
- @Test
- public void transToStartEndTest() {
- final int[] startEnd1 = PageUtil.transToStartEnd(0, 10);
- Assert.assertEquals(0, startEnd1[0]);
- Assert.assertEquals(10, startEnd1[1]);
-
- final int[] startEnd2 = PageUtil.transToStartEnd(1, 10);
- Assert.assertEquals(10, startEnd2[0]);
- Assert.assertEquals(20, startEnd2[1]);
- }
-
- @Test
- public void totalPage() {
- final int totalPage = PageUtil.totalPage(20, 3);
- Assert.assertEquals(7, totalPage);
- }
-
- @Test
- public void rainbowTest() {
- final int[] rainbow = PageUtil.rainbow(5, 20, 6);
- Assert.assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, rainbow);
- }
-}
diff --git a/hutool-db/src/main/java/cn/hutool/db/Page.java b/hutool-db/src/main/java/cn/hutool/db/Page.java
index 7e0c93836..176d7f42d 100644
--- a/hutool-db/src/main/java/cn/hutool/db/Page.java
+++ b/hutool-db/src/main/java/cn/hutool/db/Page.java
@@ -1,8 +1,8 @@
package cn.hutool.db;
import cn.hutool.core.lang.Segment;
+import cn.hutool.core.math.PageInfo;
import cn.hutool.core.util.ArrayUtil;
-import cn.hutool.core.util.PageUtil;
import cn.hutool.db.sql.Order;
import java.io.Serializable;
@@ -16,6 +16,9 @@ import java.util.Arrays;
public class Page implements Segment, Serializable {
private static final long serialVersionUID = 97792549823353462L;
+ /**
+ * 默认
+ */
public static final int DEFAULT_PAGE_SIZE = 20;
/**
@@ -140,15 +143,16 @@ public class Page implements Segment, Serializable {
/**
* @return 开始位置
- * @see #getStartIndex()
+ * @see #getBeginIndex()
*/
public int getStartPosition() {
- return getStartIndex();
+ return getBeginIndex();
}
@Override
- public Integer getStartIndex() {
- return PageUtil.getStart(this.pageNumber, this.pageSize);
+ public Integer getBeginIndex() {
+ return PageInfo.of(Integer.MAX_VALUE, this.pageSize)
+ .setFirstPageNo(0).setPageNo(this.pageNumber).getBeginIndex();
}
/**
@@ -161,7 +165,7 @@ public class Page implements Segment, Serializable {
@Override
public Integer getEndIndex() {
- return PageUtil.getEnd(this.pageNumber, this.pageSize);
+ return PageInfo.of(Integer.MAX_VALUE, this.pageSize).setFirstPageNo(0).getEndIndex();
}
/**
@@ -178,7 +182,9 @@ public class Page implements Segment, Serializable {
* @return 第一个数为开始位置,第二个数为结束位置
*/
public int[] getStartEnd() {
- return PageUtil.transToStartEnd(pageNumber, pageSize);
+ final PageInfo pageInfo = PageInfo.of(Integer.MAX_VALUE, this.pageSize)
+ .setFirstPageNo(0).setPageNo(this.pageNumber);
+ return new int[]{pageInfo.getBeginIndex(), pageInfo.getEndIndexExclude()};
}
@Override
diff --git a/hutool-db/src/main/java/cn/hutool/db/PageResult.java b/hutool-db/src/main/java/cn/hutool/db/PageResult.java
index cb410d263..70118f2a6 100644
--- a/hutool-db/src/main/java/cn/hutool/db/PageResult.java
+++ b/hutool-db/src/main/java/cn/hutool/db/PageResult.java
@@ -1,6 +1,6 @@
package cn.hutool.db;
-import cn.hutool.core.util.PageUtil;
+import cn.hutool.core.math.PageInfo;
import java.util.ArrayList;
@@ -16,7 +16,7 @@ public class PageResult extends ArrayList {
public static final int DEFAULT_PAGE_SIZE = Page.DEFAULT_PAGE_SIZE;
/**
- * 页码,{@link PageUtil#getFirstPageNo()}表示第一页
+ * 页码
*/
private int page;
/**
@@ -65,7 +65,7 @@ public class PageResult extends ArrayList {
this(page, pageSize);
this.total = total;
- this.totalPage = PageUtil.totalPage(total, pageSize);
+ this.totalPage = PageInfo.of(total, pageSize).getPages();
}
//---------------------------------------------------------- Constructor end
@@ -142,7 +142,7 @@ public class PageResult extends ArrayList {
* @return 是否第一页
*/
public boolean isFirst() {
- return this.page == PageUtil.getFirstPageNo();
+ return this.page == 0;
}
/**
diff --git a/hutool-db/src/test/java/cn/hutool/db/DbTest.java b/hutool-db/src/test/java/cn/hutool/db/DbTest.java
index 1d2f6047c..49d8da933 100644
--- a/hutool-db/src/test/java/cn/hutool/db/DbTest.java
+++ b/hutool-db/src/test/java/cn/hutool/db/DbTest.java
@@ -34,7 +34,8 @@ public class DbTest {
@Test
public void pageTest() {
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
- final List page0 = Db.of().page(Entity.of("user"), Page.of(0, 3));
+ final List page0 = Db.of().page(Entity.of("user"),
+ Page.of(0, 3));
Assert.assertEquals(3, page0.size());
final List page1 = Db.of().page(Entity.of("user"), Page.of(1, 3));
diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java
index e87bda2b1..d0e52df3b 100644
--- a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java
+++ b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java
@@ -35,12 +35,12 @@ public class JSONUtilTest {
}
/**
- * 数字解析为JSONObject忽略
+ * 数字解析为JSONArray报错
*/
- @Test
+ @Test(expected = JSONException.class)
public void parseNumberTest2() {
final JSONObject json = JSONUtil.parseObj(123L);
- Assert.assertEquals(new JSONObject(), json);
+ Assert.assertNotNull(json);
}
@Test
@@ -85,7 +85,7 @@ public class JSONUtilTest {
@Test
public void toJsonStrTest3() {
// 验证某个字段为JSON字符串时转义是否规范
- final JSONObject object = new JSONObject(true);
+ final JSONObject object = new JSONObject(JSONConfig.of().setIgnoreError(true));
object.set("name", "123123");
object.set("value", "\\");
object.set("value2", "");