Skip to content

Commit

Permalink
Updated collections to support changes made in Java 9
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed May 17, 2023
1 parent 186894c commit fdee314
Show file tree
Hide file tree
Showing 7 changed files with 849 additions and 0 deletions.
64 changes: 64 additions & 0 deletions user/super/com/google/gwt/emul/java/util/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.util.function.UnaryOperator;
import javaemul.internal.ArrayHelper;

import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsMethod;
Expand All @@ -34,6 +35,69 @@
@JsType
public interface List<E> extends Collection<E> {

@JsIgnore
static <E> List<E> of() {
return Collections.emptyList();
}

@JsIgnore
static <E> List<E> of(E e1) {
return of((E[]) new Object[] {e1});
}

@JsIgnore
static <E> List<E> of(E e1, E e2) {
return of((E[]) new Object[] {e1, e2});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3) {
return of((E[]) new Object[] {e1, e2, e3});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4) {
return of((E[]) new Object[] {e1, e2, e3, e4});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9});
}

@JsIgnore
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9, e10});
}

@JsIgnore
static <E> List<E> of(E... elements) {
for (int i = 0; i < elements.length; i++) {
checkNotNull(elements[i]);
}
return Collections.unmodifiableList(Arrays.asList((E[]) ArrayHelper.unsafeClone(elements, 0, elements.length)));
}

@JsMethod(name = "addAtIndex")
void add(int index, E element);

Expand Down
206 changes: 206 additions & 0 deletions user/super/com/google/gwt/emul/java/util/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkArgument;
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.io.Serializable;
Expand All @@ -35,6 +36,211 @@
@JsType
public interface Map<K, V> {

@JsIgnore
static <K, V> Map<K, V> of() {
return Collections.emptyMap();
}

@JsIgnore
static <K, V> Map<K, V> of(K key, V value) {
return ofEntries(
entry(key, value)
);
}

@JsIgnore
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
return ofEntries(
entry(k1, v1),
entry(k2, v2)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8,
K k9, V v9
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8),
entry(k9, v9)
);
}

@JsIgnore
static <K, V> Map<K, V> of(
K k1, V v1,
K k2, V v2,
K k3, V v3,
K k4, V v4,
K k5, V v5,
K k6, V v6,
K k7, V v7,
K k8, V v8,
K k9, V v9,
K k10, V v10
) {
return ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
entry(k4, v4),
entry(k5, v5),
entry(k6, v6),
entry(k7, v7),
entry(k8, v8),
entry(k9, v9),
entry(k10, v10)
);
}

@JsIgnore
static <K, V> Entry<K, V> entry(K key, V value) {
// This isn't quite consistent with the javadoc, since this is serializable, while entry()
// need not be serializable.
return new AbstractMap.SimpleImmutableEntry(
checkNotNull(key),
checkNotNull(value)
);
}

@JsIgnore
static <K, V> Map<K, V> ofEntries(Entry<? extends K,? extends V>... entries) {
Map<K, V> map = new HashMap<>();

for (int i = 0; i < entries.length; i++) {
// TODO this perhaps can be optimized if we know the entry is an instance of
// AbstractMap.SimpleImmutableEntry, or something more specialized?
Entry<? extends K, ? extends V> entry = entries[i];
checkArgument(map.put(checkNotNull(entry.getKey()), checkNotNull(entry.getValue())) == null,
"Duplicate key " + entry.getKey());
}

return Collections.unmodifiableMap(map);
}

/**
* Represents an individual map entry.
*/
Expand Down
67 changes: 67 additions & 0 deletions user/super/com/google/gwt/emul/java/util/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkArgument;
import static javaemul.internal.InternalPreconditions.checkNotNull;

import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsType;

Expand All @@ -26,6 +29,70 @@
*/
@JsType
public interface Set<E> extends Collection<E> {
@JsIgnore
static <E> Set<E> of() {
return Collections.emptySet();
}

@JsIgnore
static <E> Set<E> of(E e1) {
return of((E[]) new Object[] {e1});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2) {
return of((E[]) new Object[] {e1, e2});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3) {
return of((E[]) new Object[] {e1, e2, e3});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4) {
return of((E[]) new Object[] {e1, e2, e3, e4});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9});
}

@JsIgnore
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return of((E[]) new Object[] {e1, e2, e3, e4, e5, e6, e7, e8, e9, e10});
}

@JsIgnore
static <E> Set<E> of(E... elements) {
HashSet<E> set = new HashSet<>();
for (int i = 0; i < elements.length; i++) {
checkArgument(set.add(checkNotNull(elements[i])));
}
return Collections.unmodifiableSet(set);
}

@JsIgnore
@Override
default Spliterator<E> spliterator() {
Expand Down
Loading

0 comments on commit fdee314

Please sign in to comment.