Skip to content

Commit

Permalink
Simplify ImmutableArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Aug 9, 2024
1 parent 9d904a2 commit 8bbc5b5
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,20 @@ public static <E> ImmutableArray<E> narrow(ImmutableArray<? extends E> array) {
}

public static <E> @NotNull ImmutableArray<E> from(@NotNull java.util.Collection<? extends E> values) {
if (values.size() == 0) {
if (values.isEmpty()) { // implicit null check of values
return empty();
}
return new ImmutableArray<>(values.toArray());
}

public static <E> @NotNull ImmutableArray<E> from(@NotNull Iterable<? extends E> values) {
Objects.requireNonNull(values);
return switch (values) { // implicit null check of values
case ImmutableArray<?> immutableArray -> ((ImmutableArray<E>) values);
case Traversable<?> traversable -> from((Traversable<E>) values);
case java.util.Collection<?> collection -> from(((java.util.Collection<E>) values));
default -> MutableArrayList.<E>from(values).toImmutableArray();
};

if (values instanceof ImmutableArray) {
return ((ImmutableArray<E>) values);
}
if (values instanceof Traversable) {
return from((Traversable<E>) values);
}
if (values instanceof java.util.Collection) {
return from(((java.util.Collection<E>) values));
}

return MutableArrayList.<E>from(values).toImmutableArray();
}

public static <E> @NotNull ImmutableArray<E> from(@NotNull Iterator<? extends E> it) {
Expand Down

0 comments on commit 8bbc5b5

Please sign in to comment.