Commit e4a5a5672f4b192f50498efca6053a00b8cdfdab
1 parent
711a2763
StrUtils.maxLength会强制给后面加三个点,哪怕长度没有超过,优化长度不超过不加...
Showing
2 changed files
with
15 additions
and
4 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/SetUtils.java
| 1 | package cn.iocoder.yudao.framework.common.util.collection; | 1 | package cn.iocoder.yudao.framework.common.util.collection; |
| 2 | 2 | ||
| 3 | -import java.util.Arrays; | ||
| 4 | -import java.util.HashSet; | 3 | +import cn.hutool.core.collection.CollUtil; |
| 4 | + | ||
| 5 | import java.util.Set; | 5 | import java.util.Set; |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| @@ -13,7 +13,7 @@ public class SetUtils { | @@ -13,7 +13,7 @@ public class SetUtils { | ||
| 13 | 13 | ||
| 14 | @SafeVarargs | 14 | @SafeVarargs |
| 15 | public static <T> Set<T> asSet(T... objs) { | 15 | public static <T> Set<T> asSet(T... objs) { |
| 16 | - return new HashSet<>(Arrays.asList(objs)); | 16 | + return CollUtil.newHashSet(objs); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | } | 19 | } |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java
| 1 | package cn.iocoder.yudao.framework.common.util.string; | 1 | package cn.iocoder.yudao.framework.common.util.string; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 3 | import cn.hutool.core.util.ArrayUtil; | 4 | import cn.hutool.core.util.ArrayUtil; |
| 4 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 5 | 6 | ||
| @@ -16,7 +17,14 @@ import java.util.stream.Collectors; | @@ -16,7 +17,14 @@ import java.util.stream.Collectors; | ||
| 16 | public class StrUtils { | 17 | public class StrUtils { |
| 17 | 18 | ||
| 18 | public static String maxLength(CharSequence str, int maxLength) { | 19 | public static String maxLength(CharSequence str, int maxLength) { |
| 19 | - return StrUtil.maxLength(str, maxLength - 3); // -3 的原因,是该方法会补充 ... 恰好 | 20 | + Assert.isTrue(maxLength > 0); |
| 21 | + if (null == str) { | ||
| 22 | + return null; | ||
| 23 | + } | ||
| 24 | + if (str.length() <= maxLength) { | ||
| 25 | + return str.toString(); | ||
| 26 | + } | ||
| 27 | + return StrUtil.sub(str, 0, maxLength - 3) + "..."; // -3 的原因,是该方法会补充 ... 恰好 | ||
| 20 | } | 28 | } |
| 21 | 29 | ||
| 22 | /** | 30 | /** |
| @@ -45,4 +53,7 @@ public class StrUtils { | @@ -45,4 +53,7 @@ public class StrUtils { | ||
| 45 | return Arrays.stream(longs).boxed().collect(Collectors.toList()); | 53 | return Arrays.stream(longs).boxed().collect(Collectors.toList()); |
| 46 | } | 54 | } |
| 47 | 55 | ||
| 56 | + public static void main(String[] args) { | ||
| 57 | + System.out.println(maxLength("aaaaa", 4)); | ||
| 58 | + } | ||
| 48 | } | 59 | } |