浏览代码

support edge

Looly 5 年之前
父节点
当前提交
e344c4ef54

+ 2 - 0
CHANGELOG.md

@@ -8,6 +8,8 @@
 ### 新特性
 * 【core   】     增加逻辑,对于原始类型注入,使用默认值(issue#797@Github)
 * 【core   】     增加CityHash算法
+* 【core   】     PageUtil支持setFirstPageNo自定义第一页的页码(issue#I1CGNZ@Gitee)
+* 【http   】     UserAgentUtil增加Chromium内核的Edge浏览器支持(issue#800@Github)
 
 ### Bug修复
 * 【core   】     修复NumberWordFormatter拼写错误(issue#799@Github)

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java

@@ -2128,7 +2128,7 @@ public class CollUtil {
 		int resultSize = list.size();
 		// 每页条目数大于总数直接返回所有
 		if (resultSize <= pageSize) {
-			if (pageNo <= 1) {
+			if (pageNo < 1) {
 				return Collections.unmodifiableList(list);
 			} else {
 				// 越界直接返回空

+ 125 - 32
hutool-core/src/main/java/cn/hutool/core/util/PageUtil.java

@@ -2,69 +2,144 @@ package cn.hutool.core.util;
 
 /**
  * 分页工具类
- * 
+ *
  * @author xiaoleilu
- * 
  */
 public class PageUtil {
 
+	private static int firstPageNo = 0;
+
+	/**
+	 * 获得首页的页码,可以为0或者1
+	 *
+	 * @return 首页页码
+	 */
+	public static int getFirstPageNo() {
+		return firstPageNo;
+	}
+
+	/**
+	 * 设置首页页码,可以为0或者1
+	 *
+	 * <pre>
+	 *     当设置为0时,页码0表示第一页,开始位置为0
+	 *     当设置为1时,页码1表示第一页,开始位置为0
+	 * </pre>
+	 *
+	 * @param customFirstPageNo 自定义的首页页码,为0或者1
+	 */
+	public static void setFirstPageNo(int customFirstPageNo) {
+		firstPageNo = customFirstPageNo;
+	}
+
+	/**
+	 * 设置首页页码为1
+	 *
+	 * <pre>
+	 *     当设置为1时,页码1表示第一页,开始位置为0
+	 * </pre>
+	 */
+	public static void setOneAsFirstPageNo() {
+		setFirstPageNo(1);
+	}
+
 	/**
 	 * 将页数和每页条目数转换为开始位置<br>
 	 * 此方法用于不包括结束位置的分页方法<br>
 	 * 例如:
-	 * 
+	 *
 	 * <pre>
 	 * 页码:0,每页10 =》 0
 	 * 页码:1,每页10 =》 10
 	 * ……
 	 * </pre>
-	 * 
-	 * @param pageNo 页码(从0计数)
+	 *
+	 * <p>
+	 * 当{@link #setFirstPageNo(int)}设置为1时:
+	 * <pre>
+	 * 页码:1,每页10 =》 0
+	 * 页码:2,每页10 =》 10
+	 * ……
+	 * </pre>
+	 *
+	 * @param pageNo   页码(从0计数)
 	 * @param pageSize 每页条目数
 	 * @return 开始位置
 	 */
 	public static int getStart(int pageNo, int pageSize) {
-		if (pageNo < 0) {
-			pageNo = 0;
+		if (pageNo < firstPageNo) {
+			pageNo = firstPageNo;
 		}
 
 		if (pageSize < 1) {
 			pageSize = 0;
 		}
 
-		return pageNo * pageSize;
+		return (pageNo - firstPageNo) * pageSize;
+	}
+
+	/**
+	 * 将页数和每页条目数转换为结束位置<br>
+	 * 此方法用于不包括结束位置的分页方法<br>
+	 * 例如:
+	 *
+	 * <pre>
+	 * 页码:0,每页10 =》 9
+	 * 页码:1,每页10 =》 19
+	 * ……
+	 * </pre>
+	 *
+	 * <p>
+	 * 当{@link #setFirstPageNo(int)}设置为1时:
+	 * <pre>
+	 * 页码:1,每页10 =》 9
+	 * 页码:2,每页10 =》 19
+	 * ……
+	 * </pre>
+	 *
+	 * @param pageNo   页码(从0计数)
+	 * @param pageSize 每页条目数
+	 * @return 开始位置
+	 * @since 5.2.5
+	 */
+	public static int getEnd(int pageNo, int pageSize) {
+		final int start = getStart(pageNo, pageSize);
+		return getEndByStart(start, pageSize);
 	}
 
 	/**
 	 * 将页数和每页条目数转换为开始位置和结束位置<br>
 	 * 此方法用于包括结束位置的分页方法<br>
 	 * 例如:
-	 * 
+	 *
 	 * <pre>
 	 * 页码:0,每页10 =》 [0, 10]
 	 * 页码:1,每页10 =》 [10, 20]
 	 * ……
 	 * </pre>
-	 * 
-	 * @param pageNo 页码(从0计数)
+	 *
+	 * <p>
+	 * 当{@link #setFirstPageNo(int)}设置为1时:
+	 * <pre>
+	 * 页码:1,每页10 =》 [0, 10]
+	 * 页码:2,每页10 =》 [10, 20]
+	 * ……
+	 * </pre>
+	 *
+	 * @param pageNo   页码(从0计数)
 	 * @param pageSize 每页条目数
 	 * @return 第一个数为开始位置,第二个数为结束位置
 	 */
 	public static int[] transToStartEnd(int pageNo, int pageSize) {
 		final int start = getStart(pageNo, pageSize);
-		if (pageSize < 1) {
-			pageSize = 0;
-		}
-		final int end = start + pageSize;
-
-		return new int[] { start, end };
+		return new int[]{start, getEndByStart(start, pageSize)};
 	}
 
 	/**
 	 * 根据总数计算总页数
-	 * 
+	 *
 	 * @param totalCount 总数
-	 * @param pageSize 每页数
+	 * @param pageSize   每页数
 	 * @return 总页数
 	 */
 	public static int totalPage(int totalCount, int pageSize) {
@@ -78,13 +153,13 @@ public class PageUtil {
 	 * 分页彩虹算法<br>
 	 * 来自:https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java<br>
 	 * 通过传入的信息,生成一个分页列表显示
-	 * 
-	 * @param currentPage 当前页
-	 * @param pageCount 总页数
+	 *
+	 * @param pageNo       当前页
+	 * @param totalPage    总页数
 	 * @param displayCount 每屏展示的页数
 	 * @return 分页条
 	 */
-	public static int[] rainbow(int currentPage, int pageCount, int displayCount) {
+	public static int[] rainbow(int pageNo, int totalPage, int displayCount) {
 		boolean isEven = displayCount % 2 == 0;
 		int left = displayCount / 2;
 		int right = displayCount / 2;
@@ -93,22 +168,22 @@ public class PageUtil {
 		if (isEven) {
 			right++;
 		}
-		if (pageCount < displayCount) {
-			length = pageCount;
+		if (totalPage < displayCount) {
+			length = totalPage;
 		}
 		int[] result = new int[length];
-		if (pageCount >= displayCount) {
-			if (currentPage <= left) {
+		if (totalPage >= displayCount) {
+			if (pageNo <= left) {
 				for (int i = 0; i < result.length; i++) {
 					result[i] = i + 1;
 				}
-			} else if (currentPage > pageCount - right) {
+			} else if (pageNo > totalPage - right) {
 				for (int i = 0; i < result.length; i++) {
-					result[i] = i + pageCount - displayCount + 1;
+					result[i] = i + totalPage - displayCount + 1;
 				}
 			} else {
 				for (int i = 0; i < result.length; i++) {
-					result[i] = i + currentPage - left + (isEven ? 1 : 0);
+					result[i] = i + pageNo - left + (isEven ? 1 : 0);
 				}
 			}
 		} else {
@@ -123,12 +198,30 @@ public class PageUtil {
 	/**
 	 * 分页彩虹算法(默认展示10页)<br>
 	 * 来自:https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java
-	 * 
+	 *
 	 * @param currentPage 当前页
-	 * @param pageCount 总页数
+	 * @param pageCount   总页数
 	 * @return 分页条
 	 */
 	public static int[] rainbow(int currentPage, int pageCount) {
 		return rainbow(currentPage, pageCount, 10);
 	}
+
+	//------------------------------------------------------------------------- Private method start
+
+	/**
+	 * 根据起始位置获取结束位置
+	 *
+	 * @param start    起始位置
+	 * @param pageSize 每页条目数
+	 * @return 结束位置
+	 */
+	private static int getEndByStart(int start, int pageSize) {
+		if (pageSize < 1) {
+			pageSize = 0;
+		}
+		return start + pageSize;
+	}
+
+	//------------------------------------------------------------------------- Private method end
 }

+ 9 - 9
hutool-db/src/main/java/cn/hutool/db/Page.java

@@ -1,12 +1,12 @@
 package cn.hutool.db;
 
-import java.io.Serializable;
-import java.util.Arrays;
-
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.PageUtil;
 import cn.hutool.db.sql.Order;
 
+import java.io.Serializable;
+import java.util.Arrays;
+
 /**
  * 分页对象
  * 
@@ -50,11 +50,11 @@ public class Page implements Serializable {
 	 * 构造
 	 * 
 	 * @param pageNumber 页码,0表示第一页
-	 * @param numPerPage 每页结果数
+	 * @param pageSize 每页结果数
 	 * @param order 排序对象
 	 */
-	public Page(int pageNumber, int numPerPage, Order order) {
-		this(pageNumber, numPerPage);
+	public Page(int pageNumber, int pageSize, Order order) {
+		this(pageNumber, pageSize);
 		this.orders = new Order[] { order };
 	}
 	// ---------------------------------------------------------- Constructor start
@@ -109,7 +109,7 @@ public class Page implements Serializable {
 	 * @param pageSize 每页结果数
 	 */
 	public void setPageSize(int pageSize) {
-		this.pageSize = pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize;
+		this.pageSize = (pageSize <= 0) ? DEFAULT_PAGE_SIZE : pageSize;
 	}
 
 	/**
@@ -145,14 +145,14 @@ public class Page implements Serializable {
 	 * @return 开始位置
 	 */
 	public int getStartPosition() {
-		return getStartEnd()[0];
+		return PageUtil.getStart(this.pageNumber, this.pageSize);
 	}
 
 	/**
 	 * @return 结束位置
 	 */
 	public int getEndPosition() {
-		return getStartEnd()[1];
+		return PageUtil.getEnd(this.pageNumber, this.pageSize);
 	}
 
 	/**

+ 4 - 4
hutool-http/src/main/java/cn/hutool/http/useragent/Browser.java

@@ -1,11 +1,11 @@
 package cn.hutool.http.useragent;
 
-import java.util.List;
-import java.util.regex.Pattern;
-
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ReUtil;
 
+import java.util.List;
+import java.util.regex.Pattern;
+
 /**
  * 浏览器对象
  * 
@@ -23,7 +23,7 @@ public class Browser extends UserAgentInfo {
 	 * 支持的浏览器类型
 	 */
 	public static final List<Browser> browers = CollUtil.newArrayList(//
-			new Browser("MSEdge", "Edge", "edge\\/([\\d\\w\\.\\-]+)"), //
+			new Browser("MSEdge", "Edge|Edg", "edge|Edg\\/([\\d\\w\\.\\-]+)"), //
 			new Browser("Chrome", "chrome", "chrome\\/([\\d\\w\\.\\-]+)"), //
 			new Browser("Firefox", "firefox", Other_Version), //
 			new Browser("IEMobile", "iemobile", Other_Version), //

+ 6 - 6
hutool-http/src/main/java/cn/hutool/http/useragent/UserAgentParser.java

@@ -1,9 +1,9 @@
 package cn.hutool.http.useragent;
 
-import java.util.regex.Pattern;
-
 import cn.hutool.core.util.ReUtil;
 
+import java.util.regex.Pattern;
+
 /**
  * User-Agent解析器
  * 
@@ -46,9 +46,9 @@ public class UserAgentParser {
 	 * @return 浏览器类型
 	 */
 	private static Browser parseBrowser(String userAgentString) {
-		for (Browser brower : Browser.browers) {
-			if (brower.isMatch(userAgentString)) {
-				return brower;
+		for (Browser browser : Browser.browers) {
+			if (browser.isMatch(userAgentString)) {
+				return browser;
 			}
 		}
 		return Browser.Unknown;
@@ -77,7 +77,7 @@ public class UserAgentParser {
 	 * @return 引擎版本
 	 */
 	private static String parseEngineVersion(Engine engine, String userAgentString) {
-		final String regexp = engine.getName() + "[\\/\\- ]([\\d\\w\\.\\-]+)";
+		final String regexp = engine.getName() + "[/\\- ]([\\d\\w.\\-]+)";
 		final Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
 		return ReUtil.getGroup1(pattern, userAgentString);
 	}

+ 13 - 0
hutool-http/src/test/java/cn/hutool/http/useragent/UserAgentUtilTest.java

@@ -149,4 +149,17 @@ public class UserAgentUtilTest {
 		Assert.assertEquals("Windows", ua.getPlatform().toString());
 		Assert.assertFalse(ua.isMobile());
 	}
+
+	@Test
+	public void parseEdgeTest() {
+		String uaStr = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.69 Safari/537.36 Edg/81.0.416.34";
+		UserAgent ua = UserAgentUtil.parse(uaStr);
+		Assert.assertEquals("MSEdge", ua.getBrowser().toString());
+		Assert.assertEquals("81.0.416.34", ua.getVersion());
+		Assert.assertEquals("Webkit", ua.getEngine().toString());
+		Assert.assertEquals("537.36", ua.getEngineVersion());
+		Assert.assertEquals("Windows 10 or Windows Server 2016", ua.getOs().toString());
+		Assert.assertEquals("Windows", ua.getPlatform().toString());
+		Assert.assertFalse(ua.isMobile());
+	}
 }

+ 6 - 0
hutool-setting/src/test/java/cn/hutool/setting/test/SettingUtilTest.java

@@ -14,6 +14,12 @@ public class SettingUtilTest {
 	}
 
 	@Test
+	public void getTest2() {
+		String driver = SettingUtil.get("example/example").get("demo", "key");
+		Assert.assertEquals("value", driver);
+	}
+
+	@Test
 	public void getFirstFoundTest() {
 		String driver = SettingUtil.getFirstFound("test2", "test").get("demo", "driver");
 		Assert.assertEquals("com.mysql.jdbc.Driver", driver);