From 236ebba3a878739fd6421103d96e4df612b19f3d Mon Sep 17 00:00:00 2001 From: achao Date: Tue, 2 Aug 2022 09:33:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:trollface:=20unshift=20=E7=81=B5=E6=84=9F?= =?UTF-8?q?=E6=9D=A5=E6=BA=90:https://developer.mozilla.org/zh-CN/docs/Web?= =?UTF-8?q?/JavaScript/Reference/Global=5FObjects/Array/unshift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/stream/FastStream.java | 4 ++-- .../java/cn/hutool/core/stream/FastStreamTest.java | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java index 72d06776f..42031f756 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java @@ -1114,7 +1114,7 @@ public class FastStream implements Stream, Iterable { * @param obj 元素 * @return 流 */ - public FastStream addFirst(T obj) { + public FastStream unshift(T obj) { return FastStream.concat(Stream.of(obj), this.stream); } @@ -1125,7 +1125,7 @@ public class FastStream implements Stream, Iterable { * @return 流 */ @SafeVarargs - public final FastStream addFirst(T... obj) { + public final FastStream unshift(T... obj) { return FastStream.concat(ofStream(obj), this.stream); } diff --git a/hutool-core/src/test/java/cn/hutool/core/stream/FastStreamTest.java b/hutool-core/src/test/java/cn/hutool/core/stream/FastStreamTest.java index 26d985094..9091ec6a0 100644 --- a/hutool-core/src/test/java/cn/hutool/core/stream/FastStreamTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/stream/FastStreamTest.java @@ -100,7 +100,8 @@ public class FastStreamTest { put("1", 1); put("2", 2); put("3", 3); - }}, identityMap); + } + }, identityMap); } @Test @@ -115,7 +116,8 @@ public class FastStreamTest { put("1", singletonList(1)); put("2", singletonList(2)); put("3", singletonList(3)); - }}, group); + } + }, group); } @Test @@ -230,9 +232,9 @@ public class FastStreamTest { } @Test - public void testAddFirst() { + public void testUnshift() { List list = Arrays.asList(2, 3); - List unshift = FastStream.of(list).addFirst(1).toList(); + List unshift = FastStream.of(list).unshift(1).toList(); Assert.assertEquals(Arrays.asList(1, 2, 3), unshift); } From 65ea5c206615d0012608f6e9c628daf79f0485b3 Mon Sep 17 00:00:00 2001 From: achao Date: Tue, 2 Aug 2022 09:57:25 +0800 Subject: [PATCH 2/3] :trollface: remove private --- .../cn/hutool/core/stream/FastStream.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java index 42031f756..07a2e6f43 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java @@ -8,6 +8,7 @@ import cn.hutool.core.lang.mutable.MutableInt; import cn.hutool.core.lang.mutable.MutableObj; import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.StrUtil; +import cn.hutool.core.util.ArrayUtil; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -60,10 +61,7 @@ public class FastStream implements Stream, Iterable { this.stream = stream; } - @SafeVarargs - private static Stream ofStream(E... values) { - return Objects.isNull(values) ? Stream.empty() : Arrays.stream(values); - } + // --------------------------------------------------------------- Static method start /** * 返回{@code FastStream}的建造器 @@ -99,7 +97,7 @@ public class FastStream implements Stream, Iterable { } /** - * 返回包含单个元素的串行流, 如果值为null, 则返回空的流, 不抛出异常 + * 返回包含单个元素的串行流 * * @param t 单个元素 * @param 元素类型 @@ -120,7 +118,7 @@ public class FastStream implements Stream, Iterable { @SafeVarargs @SuppressWarnings("varargs") public static FastStream of(T... values) { - return new FastStream<>(ofStream(values)); + return ArrayUtil.isEmpty(values) ? FastStream.empty() : new FastStream<>(Stream.of(values)); } /** @@ -280,6 +278,8 @@ public class FastStream implements Stream, Iterable { return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(FastStream::of).orElseGet(FastStream::empty); } + // --------------------------------------------------------------- Static method end + /** * 过滤元素,返回与指定断言匹配的元素组成的流 * 这是一个无状态中间操作 @@ -718,8 +718,8 @@ public class FastStream implements Stream, Iterable { * @throws ArrayStoreException 如果元素转换失败,例如不是该元素类型及其父类,则抛出该异常 * 例如以下代码编译正常,但运行时会抛出 {@link ArrayStoreException} *
{@code
-	 *                                                         String[] strings = Stream.builder().add(1).build().toArray(String[]::new);
-	 *                                                         }
+ * String[] strings = Stream.builder().add(1).build().toArray(String[]::new); + * } */ @Override public A[] toArray(IntFunction generator) { @@ -1094,7 +1094,7 @@ public class FastStream implements Stream, Iterable { * @return 流 */ public FastStream push(T obj) { - return FastStream.concat(this.stream, Stream.of(obj)); + return FastStream.concat(this.stream, of(obj)); } /** @@ -1105,7 +1105,7 @@ public class FastStream implements Stream, Iterable { */ @SuppressWarnings("unchecked") public FastStream push(T... obj) { - return FastStream.concat(this.stream, ofStream(obj)); + return FastStream.concat(this.stream, of(obj)); } /** @@ -1115,7 +1115,7 @@ public class FastStream implements Stream, Iterable { * @return 流 */ public FastStream unshift(T obj) { - return FastStream.concat(Stream.of(obj), this.stream); + return FastStream.concat(of(obj), this.stream); } /** @@ -1126,7 +1126,7 @@ public class FastStream implements Stream, Iterable { */ @SafeVarargs public final FastStream unshift(T... obj) { - return FastStream.concat(ofStream(obj), this.stream); + return FastStream.concat(of(obj), this.stream); } /** From 040a039d10533de6fb825601b474ccc8038af142 Mon Sep 17 00:00:00 2001 From: achao Date: Tue, 2 Aug 2022 10:01:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:trollface:=20=E4=BF=AE=E5=A4=8D=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/stream/FastStream.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java index 07a2e6f43..7bfd730d8 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/FastStream.java @@ -716,10 +716,10 @@ public class FastStream implements Stream, Iterable { * @param 给定的数组类型 * @return 包含此流元素的指定的数组 * @throws ArrayStoreException 如果元素转换失败,例如不是该元素类型及其父类,则抛出该异常 + *

* 例如以下代码编译正常,但运行时会抛出 {@link ArrayStoreException} - *

{@code
-	 *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 String[] strings = Stream.builder().add(1).build().toArray(String[]::new);
-	 *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 }
+ *
{@code String[] strings = Stream.builder().add(1).build().toArray(String[]::new); }
+ *

*/ @Override public A[] toArray(IntFunction generator) {