Skip to content

Commit c6e2778

Browse files
mhansencopybara-github
authored andcommitted
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
1 parent 396d661 commit c6e2778

File tree

1 file changed

+2
-42
lines changed

1 file changed

+2
-42
lines changed

java/core/src/main/java/com/google/protobuf/SmallSortedMap.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Iterator;
1515
import java.util.List;
1616
import java.util.Map;
17-
import java.util.NoSuchElementException;
1817
import java.util.Set;
1918
import java.util.SortedMap;
2019
import java.util.TreeMap;
@@ -169,13 +168,13 @@ public int getNumOverflowEntries() {
169168
/** @return An iterable over the overflow entries. */
170169
public Iterable<Map.Entry<K, V>> getOverflowEntries() {
171170
return overflowEntries.isEmpty()
172-
? EmptySet.<Map.Entry<K, V>>iterable()
171+
? Collections.emptySet()
173172
: overflowEntries.entrySet();
174173
}
175174

176175
Iterable<Map.Entry<K, V>> getOverflowEntriesDescending() {
177176
return overflowEntriesDescending.isEmpty()
178-
? EmptySet.<Map.Entry<K, V>>iterable()
177+
? Collections.emptySet()
179178
: overflowEntriesDescending.entrySet();
180179
}
181180

@@ -597,45 +596,6 @@ private Iterator<Map.Entry<K, V>> getOverflowIterator() {
597596
}
598597
}
599598

600-
/**
601-
* Helper class that holds immutable instances of an Iterable/Iterator that we return when the
602-
* overflow entries is empty. This eliminates the creation of an Iterator object when there is
603-
* nothing to iterate over.
604-
*/
605-
private static class EmptySet {
606-
607-
private static final Iterator<Object> ITERATOR =
608-
new Iterator<Object>() {
609-
@Override
610-
public boolean hasNext() {
611-
return false;
612-
}
613-
614-
@Override
615-
public Object next() {
616-
throw new NoSuchElementException();
617-
}
618-
619-
@Override
620-
public void remove() {
621-
throw new UnsupportedOperationException();
622-
}
623-
};
624-
625-
private static final Iterable<Object> ITERABLE =
626-
new Iterable<Object>() {
627-
@Override
628-
public Iterator<Object> iterator() {
629-
return ITERATOR;
630-
}
631-
};
632-
633-
@SuppressWarnings("unchecked")
634-
static <T> Iterable<T> iterable() {
635-
return (Iterable<T>) ITERABLE;
636-
}
637-
}
638-
639599
@Override
640600
public boolean equals(Object o) {
641601
if (this == o) {

0 commit comments

Comments
 (0)