mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add JWTValidator
This commit is contained in:
@@ -118,6 +118,9 @@ public class ConverterRegistry implements Serializable {
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public ConverterRegistry() {
|
||||
defaultConverter();
|
||||
putCustomBySpi();
|
||||
|
@@ -724,8 +724,8 @@ public class DateTime extends Date {
|
||||
* 当前日期是否在日期指定范围内<br>
|
||||
* 起始日期和结束日期可以互换
|
||||
*
|
||||
* @param beginDate 起始日期
|
||||
* @param endDate 结束日期
|
||||
* @param beginDate 起始日期(包含)
|
||||
* @param endDate 结束日期(包含)
|
||||
* @return 是否在范围内
|
||||
* @since 3.0.8
|
||||
*/
|
||||
|
@@ -1464,8 +1464,8 @@ public class DateUtil extends CalendarUtil {
|
||||
* 起始日期和结束日期可以互换
|
||||
*
|
||||
* @param date 被检查的日期
|
||||
* @param beginDate 起始日期
|
||||
* @param endDate 结束日期
|
||||
* @param beginDate 起始日期(包含)
|
||||
* @param endDate 结束日期(包含)
|
||||
* @return 是否在范围内
|
||||
* @since 3.0.8
|
||||
*/
|
||||
|
@@ -88,7 +88,7 @@ public class PatternPool {
|
||||
*
|
||||
* @see <a href="https://baike.baidu.com/item/800">800</a>
|
||||
*/
|
||||
public final static Pattern TEL_400_800 = Pattern.compile("(?:(?:0\\d{2,3}[\\- ]?[1-9]\\d{6,7})|(?:[48]00[\\- ]?[1-9]\\d{6}))");
|
||||
public final static Pattern TEL_400_800 = Pattern.compile("0\\d{2,3}[\\- ]?[1-9]\\d{6,7}|[48]00[\\- ]?[1-9]\\d{6}");
|
||||
/**
|
||||
* 18位身份证号码
|
||||
*/
|
||||
@@ -124,7 +124,7 @@ public class PatternPool {
|
||||
/**
|
||||
* MAC地址正则
|
||||
*/
|
||||
public static final Pattern MAC_ADDRESS = Pattern.compile("((?:[A-F0-9]{1,2}[:-]){5}[A-F0-9]{1,2})|(?:0x)(\\d{12})(?:.+ETHER)", Pattern.CASE_INSENSITIVE);
|
||||
public static final Pattern MAC_ADDRESS = Pattern.compile("((?:[A-F0-9]{1,2}[:-]){5}[A-F0-9]{1,2})|0x(\\d{12}).+ETHER", Pattern.CASE_INSENSITIVE);
|
||||
/**
|
||||
* 16进制字符串
|
||||
*/
|
||||
|
@@ -14,7 +14,14 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 字段验证器
|
||||
* 字段验证器(验证器),分两种类型的验证:
|
||||
*
|
||||
* <ul>
|
||||
* <li>isXXX:通过返回boolean值判断是否满足给定格式。</li>
|
||||
* <li>validateXXX:通过抛出异常{@link ValidateException}检查是否满足给定格式。</li>
|
||||
* </ul>
|
||||
*
|
||||
* 主要验证字段非空、是否为满足指定格式等(如是否为Email、电话等)
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
|
@@ -164,11 +164,22 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Node<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有子节点
|
||||
*
|
||||
* @return 所有子节点
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Tree<T>> getChildren() {
|
||||
return (List<Tree<T>>) this.get(treeNodeConfig.getChildrenKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置子节点,设置后会覆盖所有原有子节点
|
||||
*
|
||||
* @param children 子节点列表
|
||||
* @return this
|
||||
*/
|
||||
public Tree<T> setChildren(List<Tree<T>> children) {
|
||||
this.put(treeNodeConfig.getChildrenKey(), children);
|
||||
return this;
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.core.lang.tree;
|
||||
|
||||
import cn.hutool.core.builder.Builder;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.tree.parser.NodeParser;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -21,6 +22,7 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
|
||||
private final Tree<E> root;
|
||||
private final Map<E, Tree<E>> idTreeMap;
|
||||
private boolean isBuild;
|
||||
|
||||
/**
|
||||
* 创建Tree构建器
|
||||
@@ -64,6 +66,9 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
* @return this
|
||||
*/
|
||||
public TreeBuilder<E> append(Map<E, Tree<E>> map) {
|
||||
checkBuilt();
|
||||
|
||||
Assert.isFalse(isBuild, "Current tree has been built.");
|
||||
this.idTreeMap.putAll(map);
|
||||
return this;
|
||||
}
|
||||
@@ -75,6 +80,8 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
* @return this
|
||||
*/
|
||||
public TreeBuilder<E> append(Iterable<Tree<E>> trees) {
|
||||
checkBuilt();
|
||||
|
||||
for (Tree<E> tree : trees) {
|
||||
this.idTreeMap.put(tree.getId(), tree);
|
||||
}
|
||||
@@ -90,6 +97,8 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
* @return this
|
||||
*/
|
||||
public <T> TreeBuilder<E> append(List<T> list, NodeParser<T, E> nodeParser) {
|
||||
checkBuilt();
|
||||
|
||||
final TreeNodeConfig config = this.root.getConfig();
|
||||
final Map<E, Tree<E>> map = new LinkedHashMap<>(list.size(), 1);
|
||||
Tree<E> node;
|
||||
@@ -101,38 +110,69 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
return append(map);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重置Builder,实现复用
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public TreeBuilder<E> reset() {
|
||||
this.idTreeMap.clear();
|
||||
this.root.setChildren(null);
|
||||
this.isBuild = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tree<E> build() {
|
||||
checkBuilt();
|
||||
|
||||
buildFromMap();
|
||||
cutTree();
|
||||
|
||||
this.isBuild = true;
|
||||
this.idTreeMap.clear();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树列表,例如:
|
||||
* 构建树列表,没有顶层节点,例如:
|
||||
*
|
||||
* <pre>
|
||||
* -用户管理
|
||||
* --用户添加
|
||||
* --用户管理
|
||||
* -用户管理
|
||||
* +用户添加
|
||||
* - 部门管理
|
||||
* --部门添加
|
||||
* --部门管理
|
||||
* -部门管理
|
||||
* +部门添加
|
||||
* </pre>
|
||||
*
|
||||
* @return 树列表
|
||||
*/
|
||||
public List<Tree<E>> buildList() {
|
||||
return this.root.getChildren();
|
||||
if (isBuild) {
|
||||
// 已经构建过了
|
||||
return this.root.getChildren();
|
||||
}
|
||||
return build().getChildren();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始构建
|
||||
*/
|
||||
private void buildFromMap() {
|
||||
if (MapUtil.isEmpty(this.idTreeMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Map<E, Tree<E>> eTreeMap = MapUtil.sortByValue(this.idTreeMap, false);
|
||||
List<Tree<E>> rootTreeList = CollUtil.newArrayList();
|
||||
E parentId;
|
||||
for (Tree<E> node : eTreeMap.values()) {
|
||||
if (null == node) {
|
||||
continue;
|
||||
}
|
||||
parentId = node.getParentId();
|
||||
if (ObjectUtil.equals(this.root.getId(), parentId)) {
|
||||
this.root.addChildren(node);
|
||||
@@ -146,4 +186,48 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 树剪枝
|
||||
*/
|
||||
private void cutTree() {
|
||||
final TreeNodeConfig config = this.root.getConfig();
|
||||
final Integer deep = config.getDeep();
|
||||
if (null == deep || deep < 0) {
|
||||
return;
|
||||
}
|
||||
cutTree(this.root, 0, deep);
|
||||
}
|
||||
|
||||
/**
|
||||
* 树剪枝叶
|
||||
*
|
||||
* @param tree 节点
|
||||
* @param currentDepp 当前层级
|
||||
* @param maxDeep 最大层级
|
||||
*/
|
||||
private void cutTree(Tree<E> tree, int currentDepp, int maxDeep) {
|
||||
if (null == tree) {
|
||||
return;
|
||||
}
|
||||
if (currentDepp == maxDeep) {
|
||||
// 剪枝
|
||||
tree.setChildren(null);
|
||||
return;
|
||||
}
|
||||
|
||||
final List<Tree<E>> children = tree.getChildren();
|
||||
if (CollUtil.isNotEmpty(children)) {
|
||||
for (Tree<E> child : children) {
|
||||
cutTree(child, currentDepp + 1, maxDeep);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已经构建
|
||||
*/
|
||||
private void checkBuilt() {
|
||||
Assert.isFalse(isBuild, "Current tree has been built.");
|
||||
}
|
||||
}
|
||||
|
@@ -134,11 +134,11 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
Number value = values[0];
|
||||
BigDecimal result = null == value ? BigDecimal.ZERO : new BigDecimal(value.toString());
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (null != value) {
|
||||
result = result.add(new BigDecimal(value.toString()));
|
||||
result = result.add(toBigDecimal(value));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -158,11 +158,11 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
String value = values[0];
|
||||
BigDecimal result = StrUtil.isBlank(value) ? BigDecimal.ZERO : new BigDecimal(value);
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (StrUtil.isNotBlank(value)) {
|
||||
result = result.add(new BigDecimal(value));
|
||||
result = result.add(toBigDecimal(value));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -182,7 +182,7 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
BigDecimal value = values[0];
|
||||
BigDecimal result = null == value ? BigDecimal.ZERO : value;
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (null != value) {
|
||||
@@ -274,11 +274,11 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
Number value = values[0];
|
||||
BigDecimal result = null == value ? BigDecimal.ZERO : new BigDecimal(value.toString());
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (null != value) {
|
||||
result = result.subtract(new BigDecimal(value.toString()));
|
||||
result = result.subtract(toBigDecimal(value));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -298,11 +298,11 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
String value = values[0];
|
||||
BigDecimal result = StrUtil.isBlank(value) ? BigDecimal.ZERO : new BigDecimal(value);
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (StrUtil.isNotBlank(value)) {
|
||||
result = result.subtract(new BigDecimal(value));
|
||||
result = result.subtract(toBigDecimal(value));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -322,7 +322,7 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
BigDecimal value = values[0];
|
||||
BigDecimal result = null == value ? BigDecimal.ZERO : value;
|
||||
BigDecimal result = toBigDecimal(value);
|
||||
for (int i = 1; i < values.length; i++) {
|
||||
value = values[i];
|
||||
if (null != value) {
|
||||
@@ -729,9 +729,7 @@ public class NumberUtil {
|
||||
* @return 两个参数的商
|
||||
*/
|
||||
public static BigDecimal div(String v1, String v2, int scale, RoundingMode roundingMode) {
|
||||
final BigDecimal bd1 = StrUtil.isBlank(v1) ? BigDecimal.ZERO : new BigDecimal(v1);
|
||||
final BigDecimal bd2 = StrUtil.isBlank(v2) ? BigDecimal.ZERO : new BigDecimal(v2);
|
||||
return div(bd1, bd2, scale, roundingMode);
|
||||
return div(toBigDecimal(v1), toBigDecimal(v2), scale, roundingMode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package cn.hutool.core.lang.tree;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TreeBuilderTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void checkIsBuiltTest(){
|
||||
final TreeBuilder<Integer> of = TreeBuilder.of(0);
|
||||
of.build();
|
||||
of.append(new ArrayList<>());
|
||||
}
|
||||
|
||||
}
|
@@ -50,7 +50,7 @@ public class TreeTest {
|
||||
// 自定义属性名 都要默认值的
|
||||
treeNodeConfig.setWeightKey("order");
|
||||
treeNodeConfig.setIdKey("rid");
|
||||
treeNodeConfig.setDeep(3);
|
||||
treeNodeConfig.setDeep(2);
|
||||
|
||||
//转换器
|
||||
List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
|
||||
@@ -66,4 +66,5 @@ public class TreeTest {
|
||||
|
||||
Assert.assertEquals(treeNodes.size(), 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -48,6 +48,12 @@ public class NumberUtilTest {
|
||||
Assert.assertEquals(new BigDecimal("464"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBlankTest(){
|
||||
BigDecimal result = NumberUtil.add("123", " ");
|
||||
Assert.assertEquals(new BigDecimal("123"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIntegerTest() {
|
||||
Assert.assertTrue(NumberUtil.isInteger("-12"));
|
||||
|
Reference in New Issue
Block a user