From c6e2778f9173ed925746197310f4d11e5f46df96 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Tue, 14 May 2024 12:17:28 -0700 Subject: [PATCH] Replace SmallSortedMap.EmptySet with equivalent Collections.emptySet() This reduces our code weight by a little (3 classes). Collections.emptySet also has a singleton empty iterator, so it doesn't allocate. PiperOrigin-RevId: 633667264 --- .../com/google/protobuf/SmallSortedMap.java | 44 +------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java index 17e8d77dba0a..55934d21875f 100644 --- a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java +++ b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java @@ -14,7 +14,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; @@ -169,13 +168,13 @@ public int getNumOverflowEntries() { /** @return An iterable over the overflow entries. */ public Iterable> getOverflowEntries() { return overflowEntries.isEmpty() - ? EmptySet.>iterable() + ? Collections.emptySet() : overflowEntries.entrySet(); } Iterable> getOverflowEntriesDescending() { return overflowEntriesDescending.isEmpty() - ? EmptySet.>iterable() + ? Collections.emptySet() : overflowEntriesDescending.entrySet(); } @@ -597,45 +596,6 @@ private Iterator> getOverflowIterator() { } } - /** - * Helper class that holds immutable instances of an Iterable/Iterator that we return when the - * overflow entries is empty. This eliminates the creation of an Iterator object when there is - * nothing to iterate over. - */ - private static class EmptySet { - - private static final Iterator ITERATOR = - new Iterator() { - @Override - public boolean hasNext() { - return false; - } - - @Override - public Object next() { - throw new NoSuchElementException(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - - private static final Iterable ITERABLE = - new Iterable() { - @Override - public Iterator iterator() { - return ITERATOR; - } - }; - - @SuppressWarnings("unchecked") - static Iterable iterable() { - return (Iterable) ITERABLE; - } - } - @Override public boolean equals(Object o) { if (this == o) {