Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Collections to support changes made in Java 9 #223

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions jre/java/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;
import jsinterop.annotations.JsNonNull;
Expand All @@ -33,6 +34,83 @@
@JsType
public interface List<E> extends Collection<E> {

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

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

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

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

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

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

/**
* Internal-only helper to avoid copying the incoming array, and instead just wrap it with an
* immutable List after checking there are no nulls.
*/
@JsIgnore
static <E> List<E> __ofInternal(E[] elements) {
for (int i = 0; i < elements.length; i++) {
checkNotNull(elements[i]);
}
return Collections.unmodifiableList(Arrays.asList(elements));
}

@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 jre/java/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.unmodifiableMap(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 = checkNotNull(entries[i]);
checkArgument(map.put(checkNotNull(entry.getKey()), checkNotNull(entry.getValue())) == null,
"Can't add multiple entries with the same key");
}

return Collections.unmodifiableMap(map);
}

/**
* Represents an individual map entry.
*/
Expand Down
67 changes: 67 additions & 0 deletions jre/java/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 @@ -31,6 +34,70 @@ public interface Set<E> extends Collection<E> {
@Override
Iterator<E> iterator();

@JsIgnore
static <E> Set<E> of() {
return Collections.unmodifiableSet(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])), "Can't add the same item multiple times");
}
return Collections.unmodifiableSet(set);
}

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