8221981: Simplify Map/List/Set.of() implementation

Reviewed-by: smarks
This commit is contained in:
Claes Redestad 2019-04-04 23:21:24 +02:00
parent 3233a6f944
commit 15d989faa4
4 changed files with 16 additions and 22 deletions

View File

@ -95,11 +95,6 @@ class ImmutableCollections {
}
}
@SuppressWarnings("unchecked")
static <E> List<E> emptyList() {
return (List<E>) ListN.EMPTY_LIST;
}
static abstract class AbstractImmutableList<E> extends AbstractImmutableCollection<E>
implements List<E>, RandomAccess {
@ -556,11 +551,6 @@ class ImmutableCollections {
public abstract int hashCode();
}
@SuppressWarnings("unchecked")
static <E> Set<E> emptySet() {
return (Set<E>) SetN.EMPTY_SET;
}
static final class Set12<E> extends AbstractImmutableSet<E>
implements Serializable {
@ -844,11 +834,6 @@ class ImmutableCollections {
// ---------- Map Implementations ----------
@SuppressWarnings("unchecked")
static <K,V> Map<K,V> emptyMap() {
return (Map<K,V>) MapN.EMPTY_MAP;
}
abstract static class AbstractImmutableMap<K,V> extends AbstractMap<K,V> implements Serializable {
@Override public void clear() { throw uoe(); }
@Override public V compute(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
@ -1248,7 +1233,7 @@ final class CollSer implements Serializable {
return Set.of(array);
case IMM_MAP:
if (array.length == 0) {
return ImmutableCollections.emptyMap();
return ImmutableCollections.MapN.EMPTY_MAP;
} else if (array.length == 2) {
return new ImmutableCollections.Map1<>(array[0], array[1]);
} else {

View File

@ -787,8 +787,9 @@ public interface List<E> extends Collection<E> {
*
* @since 9
*/
@SuppressWarnings("unchecked")
static <E> List<E> of() {
return ImmutableCollections.emptyList();
return (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
}
/**
@ -1031,7 +1032,9 @@ public interface List<E> extends Collection<E> {
static <E> List<E> of(E... elements) {
switch (elements.length) { // implicit null check of elements
case 0:
return ImmutableCollections.emptyList();
@SuppressWarnings("unchecked")
var list = (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
return list;
case 1:
return new ImmutableCollections.List12<>(elements[0]);
case 2:

View File

@ -1286,8 +1286,9 @@ public interface Map<K, V> {
*
* @since 9
*/
@SuppressWarnings("unchecked")
static <K, V> Map<K, V> of() {
return ImmutableCollections.emptyMap();
return (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
}
/**
@ -1604,7 +1605,9 @@ public interface Map<K, V> {
@SuppressWarnings("varargs")
static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
if (entries.length == 0) { // implicit null check of entries array
return ImmutableCollections.emptyMap();
@SuppressWarnings("unchecked")
var map = (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
return map;
} else if (entries.length == 1) {
// implicit null check of the array slot
return new ImmutableCollections.Map1<>(entries[0].getKey(),

View File

@ -448,8 +448,9 @@ public interface Set<E> extends Collection<E> {
*
* @since 9
*/
@SuppressWarnings("unchecked")
static <E> Set<E> of() {
return ImmutableCollections.emptySet();
return (Set<E>) ImmutableCollections.SetN.EMPTY_SET;
}
/**
@ -692,7 +693,9 @@ public interface Set<E> extends Collection<E> {
static <E> Set<E> of(E... elements) {
switch (elements.length) { // implicit null check of elements
case 0:
return ImmutableCollections.emptySet();
@SuppressWarnings("unchecked")
var set = (Set<E>) ImmutableCollections.SetN.EMPTY_SET;
return set;
case 1:
return new ImmutableCollections.Set12<>(elements[0]);
case 2: