From f1d83ad9d34fc21c1360646f4c88fe947d3a1e9f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 10 Feb 2020 17:11:50 +0300 Subject: [PATCH] Optimize empty collections --- .../classlib/java/util/TCollections.java | 182 +++++++++++------- 1 file changed, 115 insertions(+), 67 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java b/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java index dec2cd347..e092e5a48 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java @@ -21,69 +21,126 @@ 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() { + @Override public int size() { + return 0; + } + @Override public TIterator 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() { + @Override public TSet> 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() { + @Override public Object get(int index) { + throw new TIndexOutOfBoundsException(); + } + @Override public int size() { + return 0; + } + @Override + public TIterator iterator() { + return emptyIterator(); + } + @Override + public TListIterator 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() { + @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() { + @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 TIterator emptyIterator() { - return new TIterator() { - @Override public boolean hasNext() { - return false; - } - @Override public T next() { - throw new TNoSuchElementException(); - } - @Override public void remove() { - throw new TIllegalStateException(); - } - }; + return (TIterator) EMPTY_ITERATOR; } + @SuppressWarnings("unchecked") public static TListIterator emptyListIterator() { - return new TListIterator() { - @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(); - } - }; + return (TListIterator) EMPTY_LIST_ITERATOR; } - public static final TList emptyList() { - return new TAbstractList() { - @Override public T get(int index) { - throw new TIndexOutOfBoundsException(); - } - @Override public int size() { - return 0; - } - }; + @SuppressWarnings("unchecked") + public static TList emptyList() { + return (TList) EMPTY_LIST; } public static TEnumeration emptyEnumeration() { @@ -97,23 +154,14 @@ public class TCollections extends TObject { }; } - public static final TSet emptySet() { - return new TAbstractSet() { - @Override public int size() { - return 0; - } - @Override public TIterator iterator() { - return emptyIterator(); - } - }; + @SuppressWarnings("unchecked") + public static TSet emptySet() { + return (TSet) EMPTY_SET; } - public static final TMap emptyMap() { - return new TAbstractMap() { - @Override public TSet> entrySet() { - return emptySet(); - } - }; + @SuppressWarnings("unchecked") + public static TMap emptyMap() { + return (TMap) EMPTY_MAP; } public static TList singletonList(final T o) {