This commit is contained in:
Looly 2020-07-12 10:21:53 +08:00
parent 93d91699c1
commit 80ed1c0141
6 changed files with 95 additions and 53 deletions

View File

@ -24,6 +24,8 @@
* 【core 】 修复Dict中putAll大小写问题issue#I1MU5B@Gitee * 【core 】 修复Dict中putAll大小写问题issue#I1MU5B@Gitee
* 【core 】 修复POI中sax读取数字判断错误问题issue#931@Github * 【core 】 修复POI中sax读取数字判断错误问题issue#931@Github
* 【core 】 修复DateUtil.endOfQuarter错误问题issue#I1NGZ7@Gitee * 【core 】 修复DateUtil.endOfQuarter错误问题issue#I1NGZ7@Gitee
* 【core 】 修复URL中有空格转为+问题issue#I1NGW4@Gitee
* 【core 】 修复CollUtil.intersectionDistinct空集合结果错误问题
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
## 5.3.8 (2020-06-16) ## 5.3.8 (2020-06-16)

View File

@ -283,23 +283,26 @@ public class CollUtil {
@SafeVarargs @SafeVarargs
public static <T> Set<T> intersectionDistinct(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) { public static <T> Set<T> intersectionDistinct(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) {
final Set<T> result; final Set<T> result;
if (isEmpty(coll1)) { if (isEmpty(coll1) || isEmpty(coll2)) {
result = new LinkedHashSet<>(); // 有一个空集合就直接返回空
return new LinkedHashSet<>();
} else { } else {
result = new LinkedHashSet<>(coll1); result = new LinkedHashSet<>(coll1);
} }
if (isNotEmpty(coll2)) {
result.retainAll(coll2);
}
if (ArrayUtil.isNotEmpty(otherColls)) { if (ArrayUtil.isNotEmpty(otherColls)) {
for (Collection<T> otherColl : otherColls) { for (Collection<T> otherColl : otherColls) {
if(isNotEmpty(otherColl)){ if(isNotEmpty(otherColl)){
result.retainAll(otherColl); result.retainAll(otherColl);
} else {
// 有一个空集合就直接返回空
return new LinkedHashSet<>();
} }
} }
} }
result.retainAll(coll2);
return result; return result;
} }

View File

@ -19,10 +19,9 @@ import java.util.BitSet;
* 3.将非文本内容转换成"%xy"的形式,xy是两位16进制的数值; * 3.将非文本内容转换成"%xy"的形式,xy是两位16进制的数值;
* </pre> * </pre>
* *
* @author looly, * @author looly
*
*/ */
public class URLEncoder implements Serializable{ public class URLEncoder implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// --------------------------------------------------------------------------------------------- Static method start // --------------------------------------------------------------------------------------------- Static method start
@ -48,11 +47,21 @@ public class URLEncoder implements Serializable{
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' Also '=' and '&amp;' 不编码 * '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' Also '=' and '&amp;' 不编码
* 其它编码为 %nn 形式 * 其它编码为 %nn 形式
* </pre> * </pre>
* * <p>
* 详细见https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm * 详细见https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm
*/ */
public static final URLEncoder QUERY = createQuery(); public static final URLEncoder QUERY = createQuery();
/**
* 全编码的{@link URLEncoder}<br>
* <pre>
* 0x2A, 0x2D, 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A as-is
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' 不编码
* 其它编码为 %nn 形式
* </pre>
*/
public static final URLEncoder ALL = createAll();
/** /**
* 创建默认{@link URLEncoder}<br> * 创建默认{@link URLEncoder}<br>
* 默认的编码器针对URI路径编码定义如下 * 默认的编码器针对URI路径编码定义如下
@ -102,7 +111,7 @@ public class URLEncoder implements Serializable{
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' Also '=' and '&amp;' 不编码 * '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' Also '=' and '&amp;' 不编码
* 其它编码为 %nn 形式 * 其它编码为 %nn 形式
* </pre> * </pre>
* * <p>
* 详细见https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm * 详细见https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm
* *
* @return {@link URLEncoder} * @return {@link URLEncoder}
@ -122,16 +131,44 @@ public class URLEncoder implements Serializable{
return encoder; return encoder;
} }
/**
* 创建{@link URLEncoder}<br>
* 编码器针对URI路径编码定义如下
*
* <pre>
* 0x2A, 0x2D, 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A as-is
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' 不编码
* 其它编码为 %nn 形式
* </pre>
* <p>
* 详细见https://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm
*
* @return {@link URLEncoder}
*/
public static URLEncoder createAll() {
final URLEncoder encoder = new URLEncoder();
encoder.addSafeCharacter('*');
encoder.addSafeCharacter('-');
encoder.addSafeCharacter('.');
encoder.addSafeCharacter('_');
return encoder;
}
// --------------------------------------------------------------------------------------------- Static method end // --------------------------------------------------------------------------------------------- Static method end
/** 存放安全编码 */ /**
* 存放安全编码
*/
private final BitSet safeCharacters; private final BitSet safeCharacters;
/** 是否编码空格为+ */ /**
* 是否编码空格为+
*/
private boolean encodeSpaceAsPlus = false; private boolean encodeSpaceAsPlus = false;
/** /**
* 构造<br> * 构造<br>
* * <p>
* [a-zA-Z0-9]默认不被编码 * [a-zA-Z0-9]默认不被编码
*/ */
public URLEncoder() { public URLEncoder() {
@ -191,7 +228,6 @@ public class URLEncoder implements Serializable{
* *
* @param path 需要编码的字符串 * @param path 需要编码的字符串
* @param charset 编码 * @param charset 编码
*
* @return 编码后的字符串 * @return 编码后的字符串
*/ */
public String encode(String path, Charset charset) { public String encode(String path, Charset charset) {

View File

@ -14,7 +14,6 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -331,11 +330,8 @@ public class URLUtil {
if (null == charset) { if (null == charset) {
return url; return url;
} }
try {
return java.net.URLEncoder.encode(url, charset.toString()); return URLEncoder.ALL.encode(url, charset);
} catch (UnsupportedEncodingException e) {
throw new UtilException(e);
}
} }
/** /**

View File

@ -29,12 +29,11 @@ import java.util.SortedSet;
* 集合工具类单元测试 * 集合工具类单元测试
* *
* @author looly * @author looly
*
*/ */
public class CollUtilTest { public class CollUtilTest {
@Test @Test
public void isNotEmptyTest(){ public void isNotEmptyTest() {
Assert.assertFalse(CollUtil.isNotEmpty((Collection<?>) null)); Assert.assertFalse(CollUtil.isNotEmpty((Collection<?>) null));
} }
@ -144,7 +143,7 @@ public class CollUtilTest {
} }
@Test @Test
public void subtractTest(){ public void subtractTest() {
List<String> list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); List<String> list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
List<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); List<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2");
final Collection<String> subtract = CollUtil.subtract(list1, list2); final Collection<String> subtract = CollUtil.subtract(list1, list2);
@ -632,9 +631,9 @@ public class CollUtilTest {
} }
@Test @Test
public void toMapTest(){ public void toMapTest() {
Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d"); Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d");
final Map<String, String> map = CollUtil.toMap(keys, new HashMap<>(), (value)->"key" + value); final Map<String, String> map = CollUtil.toMap(keys, new HashMap<>(), (value) -> "key" + value);
Assert.assertEquals("a", map.get("keya")); Assert.assertEquals("a", map.get("keya"));
Assert.assertEquals("b", map.get("keyb")); Assert.assertEquals("b", map.get("keyb"));
Assert.assertEquals("c", map.get("keyc")); Assert.assertEquals("c", map.get("keyc"));

View File

@ -198,4 +198,10 @@ public class UrlBuilderTest {
final UrlBuilder builder = UrlBuilder.ofHttp(getWorkDayUrl, CharsetUtil.CHARSET_UTF_8); final UrlBuilder builder = UrlBuilder.ofHttp(getWorkDayUrl, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(getWorkDayUrl, builder.toString()); Assert.assertEquals(getWorkDayUrl, builder.toString());
} }
@Test
public void blankEncodeTest(){
final UrlBuilder urlBuilder = UrlBuilder.ofHttp("http://a.com/aaa bbb.html", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("http://a.com/aaa%20bbb.html", urlBuilder.toString());
}
} }