Optimize empty collections

This commit is contained in:
Alexey Andreev 2020-02-10 17:11:50 +03:00
parent a414a7979c
commit f1d83ad9d3

View File

@ -21,34 +21,88 @@ import org.teavm.classlib.java.util.TMap.Entry;
public class TCollections extends TObject {
@SuppressWarnings("rawtypes")
public static final TSet EMPTY_SET = emptySet();
public static final TSet EMPTY_SET = new TAbstractSet<Object>() {
@Override public int size() {
return 0;
}
@Override public TIterator<Object> iterator() {
return emptyIterator();
}
@Override public boolean isEmpty() {
return true;
}
@Override public boolean contains(Object o) {
return false;
}
@Override public boolean containsAll(TCollection<?> c) {
return false;
}
};
@SuppressWarnings("rawtypes")
public static final TMap EMPTY_MAP = emptyMap();
public static final TMap EMPTY_MAP = new TAbstractMap<Object, Object>() {
@Override public TSet<Entry<Object, Object>> entrySet() {
return emptySet();
}
@Override
public int size() {
return 0;
}
@Override public Object get(Object key) {
return null;
}
@Override public boolean isEmpty() {
return true;
}
@Override public boolean containsKey(Object key) {
return false;
}
@Override public boolean containsValue(Object value) {
return false;
}
};
@SuppressWarnings("rawtypes")
public static final TList EMPTY_LIST = emptyList();
public static final TList EMPTY_LIST = new TAbstractList<Object>() {
@Override public Object get(int index) {
throw new TIndexOutOfBoundsException();
}
@Override public int size() {
return 0;
}
@Override
public TIterator<Object> iterator() {
return emptyIterator();
}
@Override
public TListIterator<Object> listIterator(int index) {
if (index != 0) {
throw new IndexOutOfBoundsException();
}
return emptyListIterator();
}
@Override public boolean isEmpty() {
return true;
}
};
public static <T> TIterator<T> emptyIterator() {
return new TIterator<T>() {
private static final TIterator<?> EMPTY_ITERATOR = new TIterator<Object>() {
@Override public boolean hasNext() {
return false;
}
@Override public T next() {
@Override public Object next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
throw new TIllegalStateException();
}
};
}
public static <T> TListIterator<T> emptyListIterator() {
return new TListIterator<T>() {
private static final TListIterator<?> EMPTY_LIST_ITERATOR = new TListIterator<Object>() {
@Override public boolean hasNext() {
return false;
}
@Override public T next() {
@Override public Object next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
@ -57,7 +111,7 @@ public class TCollections extends TObject {
@Override public boolean hasPrevious() {
return false;
}
@Override public T previous() {
@Override public Object previous() {
throw new TNoSuchElementException();
}
@Override public int nextIndex() {
@ -66,24 +120,27 @@ public class TCollections extends TObject {
@Override public int previousIndex() {
return -1;
}
@Override public void set(T e) {
@Override public void set(Object e) {
throw new TUnsupportedOperationException();
}
@Override public void add(T e) {
@Override public void add(Object e) {
throw new TUnsupportedOperationException();
}
};
@SuppressWarnings("unchecked")
public static <T> TIterator<T> emptyIterator() {
return (TIterator<T>) EMPTY_ITERATOR;
}
public static final <T> TList<T> emptyList() {
return new TAbstractList<T>() {
@Override public T get(int index) {
throw new TIndexOutOfBoundsException();
@SuppressWarnings("unchecked")
public static <T> TListIterator<T> emptyListIterator() {
return (TListIterator<T>) EMPTY_LIST_ITERATOR;
}
@Override public int size() {
return 0;
}
};
@SuppressWarnings("unchecked")
public static <T> TList<T> emptyList() {
return (TList<T>) EMPTY_LIST;
}
public static <T> TEnumeration<T> emptyEnumeration() {
@ -97,23 +154,14 @@ public class TCollections extends TObject {
};
}
public static final <T> TSet<T> emptySet() {
return new TAbstractSet<T>() {
@Override public int size() {
return 0;
}
@Override public TIterator<T> iterator() {
return emptyIterator();
}
};
@SuppressWarnings("unchecked")
public static <T> TSet<T> emptySet() {
return (TSet<T>) EMPTY_SET;
}
public static final <K, V> TMap<K, V> emptyMap() {
return new TAbstractMap<K, V>() {
@Override public TSet<Entry<K, V>> entrySet() {
return emptySet();
}
};
@SuppressWarnings("unchecked")
public static <K, V> TMap<K, V> emptyMap() {
return (TMap<K, V>) EMPTY_MAP;
}
public static <T> TList<T> singletonList(final T o) {