From 8bbc5b5265ef5e80becb6d34c0a33e79ff95e11f Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 9 Aug 2024 21:15:16 +0800 Subject: [PATCH] Simplify ImmutableArray --- .../collection/immutable/ImmutableArray.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableArray.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableArray.java index 1dc930a9..72c3dae4 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableArray.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableArray.java @@ -124,26 +124,20 @@ public static ImmutableArray narrow(ImmutableArray array) { } public static @NotNull ImmutableArray from(@NotNull java.util.Collection values) { - if (values.size() == 0) { + if (values.isEmpty()) { // implicit null check of values return empty(); } return new ImmutableArray<>(values.toArray()); } public static @NotNull ImmutableArray from(@NotNull Iterable values) { - Objects.requireNonNull(values); + return switch (values) { // implicit null check of values + case ImmutableArray immutableArray -> ((ImmutableArray) values); + case Traversable traversable -> from((Traversable) values); + case java.util.Collection collection -> from(((java.util.Collection) values)); + default -> MutableArrayList.from(values).toImmutableArray(); + }; - if (values instanceof ImmutableArray) { - return ((ImmutableArray) values); - } - if (values instanceof Traversable) { - return from((Traversable) values); - } - if (values instanceof java.util.Collection) { - return from(((java.util.Collection) values)); - } - - return MutableArrayList.from(values).toImmutableArray(); } public static @NotNull ImmutableArray from(@NotNull Iterator it) {