Skip to content

Commit

Permalink
Under J2CL, expose {ImmutableList,ImmutableSet}.{of,copyOf} for JavaS…
Browse files Browse the repository at this point in the history
…cript usage.

RELNOTES=`collect`: Under J2CL, exposed `ImmutableList` and `ImmutableSet` methods `copyOf` and `of` for JavaScript usage.
PiperOrigin-RevId: 545771930
  • Loading branch information
gkdn authored and Google Java Core Libraries committed Jul 5, 2023
1 parent 31c478f commit b41968f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.RandomAccess;
import java.util.stream.Collector;
import jsinterop.annotations.JsMethod;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -118,6 +119,12 @@ private static void arrayCopy(Object[] dest, int pos, Object... source) {
System.arraycopy(source, 0, dest, pos, source.length);
}

/** ImmutableList.of API that is friendly to use from JavaScript. */
@JsMethod(name = "of")
static <E> ImmutableList<E> jsOf(E... elements) {
return copyOf(elements);
}

public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements) {
checkNotNull(elements); // for GWT
return (elements instanceof Collection)
Expand All @@ -142,6 +149,7 @@ public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) {
return copyFromCollection(elements);
}

@JsMethod
public static <E> ImmutableList<E> copyOf(E[] elements) {
checkNotNull(elements); // eager for GWT
return copyOf(Arrays.asList(elements));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collector;
import jsinterop.annotations.JsMethod;

/**
* GWT emulated version of {@link com.google.common.collect.ImmutableSet}. For the unsorted sets,
Expand Down Expand Up @@ -85,6 +86,13 @@ public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... ot
return copyOf(all.iterator());
}

/** ImmutableSet.of API that is friendly to use from JavaScript. */
@JsMethod(name = "of")
static <E> ImmutableSet<E> jsOf(E... elements) {
return copyOf(elements);
}

@JsMethod
public static <E> ImmutableSet<E> copyOf(E[] elements) {
checkNotNull(elements);
switch (elements.length) {
Expand Down

0 comments on commit b41968f

Please sign in to comment.