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,69 +21,126 @@ import org.teavm.classlib.java.util.TMap.Entry;
public class TCollections extends TObject { public class TCollections extends TObject {
@SuppressWarnings("rawtypes") @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") @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") @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;
}
};
private static final TIterator<?> EMPTY_ITERATOR = new TIterator<Object>() {
@Override public boolean hasNext() {
return false;
}
@Override public Object next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
throw new TIllegalStateException();
}
};
private static final TListIterator<?> EMPTY_LIST_ITERATOR = new TListIterator<Object>() {
@Override public boolean hasNext() {
return false;
}
@Override public Object next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
throw new TIllegalStateException();
}
@Override public boolean hasPrevious() {
return false;
}
@Override public Object previous() {
throw new TNoSuchElementException();
}
@Override public int nextIndex() {
return 0;
}
@Override public int previousIndex() {
return -1;
}
@Override public void set(Object e) {
throw new TUnsupportedOperationException();
}
@Override public void add(Object e) {
throw new TUnsupportedOperationException();
}
};
@SuppressWarnings("unchecked")
public static <T> TIterator<T> emptyIterator() { public static <T> TIterator<T> emptyIterator() {
return new TIterator<T>() { return (TIterator<T>) EMPTY_ITERATOR;
@Override public boolean hasNext() {
return false;
}
@Override public T next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
throw new TIllegalStateException();
}
};
} }
@SuppressWarnings("unchecked")
public static <T> TListIterator<T> emptyListIterator() { public static <T> TListIterator<T> emptyListIterator() {
return new TListIterator<T>() { return (TListIterator<T>) EMPTY_LIST_ITERATOR;
@Override public boolean hasNext() {
return false;
}
@Override public T next() {
throw new TNoSuchElementException();
}
@Override public void remove() {
throw new TIllegalStateException();
}
@Override public boolean hasPrevious() {
return false;
}
@Override public T previous() {
throw new TNoSuchElementException();
}
@Override public int nextIndex() {
return 0;
}
@Override public int previousIndex() {
return -1;
}
@Override public void set(T e) {
throw new TUnsupportedOperationException();
}
@Override public void add(T e) {
throw new TUnsupportedOperationException();
}
};
} }
public static final <T> TList<T> emptyList() { @SuppressWarnings("unchecked")
return new TAbstractList<T>() { public static <T> TList<T> emptyList() {
@Override public T get(int index) { return (TList<T>) EMPTY_LIST;
throw new TIndexOutOfBoundsException();
}
@Override public int size() {
return 0;
}
};
} }
public static <T> TEnumeration<T> emptyEnumeration() { public static <T> TEnumeration<T> emptyEnumeration() {
@ -97,23 +154,14 @@ public class TCollections extends TObject {
}; };
} }
public static final <T> TSet<T> emptySet() { @SuppressWarnings("unchecked")
return new TAbstractSet<T>() { public static <T> TSet<T> emptySet() {
@Override public int size() { return (TSet<T>) EMPTY_SET;
return 0;
}
@Override public TIterator<T> iterator() {
return emptyIterator();
}
};
} }
public static final <K, V> TMap<K, V> emptyMap() { @SuppressWarnings("unchecked")
return new TAbstractMap<K, V>() { public static <K, V> TMap<K, V> emptyMap() {
@Override public TSet<Entry<K, V>> entrySet() { return (TMap<K, V>) EMPTY_MAP;
return emptySet();
}
};
} }
public static <T> TList<T> singletonList(final T o) { public static <T> TList<T> singletonList(final T o) {