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 { 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;
}
};
public static <T> TIterator<T> emptyIterator() { private static final TIterator<?> EMPTY_ITERATOR = new TIterator<Object>() {
return new TIterator<T>() {
@Override public boolean hasNext() { @Override public boolean hasNext() {
return false; return false;
} }
@Override public T next() { @Override public Object next() {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
@Override public void remove() { @Override public void remove() {
throw new TIllegalStateException(); throw new TIllegalStateException();
} }
}; };
}
public static <T> TListIterator<T> emptyListIterator() { private static final TListIterator<?> EMPTY_LIST_ITERATOR = new TListIterator<Object>() {
return new TListIterator<T>() {
@Override public boolean hasNext() { @Override public boolean hasNext() {
return false; return false;
} }
@Override public T next() { @Override public Object next() {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
@Override public void remove() { @Override public void remove() {
@ -57,7 +111,7 @@ public class TCollections extends TObject {
@Override public boolean hasPrevious() { @Override public boolean hasPrevious() {
return false; return false;
} }
@Override public T previous() { @Override public Object previous() {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
@Override public int nextIndex() { @Override public int nextIndex() {
@ -66,24 +120,27 @@ public class TCollections extends TObject {
@Override public int previousIndex() { @Override public int previousIndex() {
return -1; return -1;
} }
@Override public void set(T e) { @Override public void set(Object e) {
throw new TUnsupportedOperationException(); throw new TUnsupportedOperationException();
} }
@Override public void add(T e) { @Override public void add(Object e) {
throw new TUnsupportedOperationException(); throw new TUnsupportedOperationException();
} }
}; };
@SuppressWarnings("unchecked")
public static <T> TIterator<T> emptyIterator() {
return (TIterator<T>) EMPTY_ITERATOR;
} }
public static final <T> TList<T> emptyList() { @SuppressWarnings("unchecked")
return new TAbstractList<T>() { public static <T> TListIterator<T> emptyListIterator() {
@Override public T get(int index) { return (TListIterator<T>) EMPTY_LIST_ITERATOR;
throw new TIndexOutOfBoundsException();
} }
@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() { 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) {