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
pull/16843/head
Mark Hansen 7 months ago committed by Copybara-Service
parent 396d661767
commit c6e2778f91
  1. 44
      java/core/src/main/java/com/google/protobuf/SmallSortedMap.java

@ -14,7 +14,6 @@ import java.util.Collections;
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 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> {
/** @return An iterable over the overflow entries. */
public Iterable<Map.Entry<K, V>> getOverflowEntries() {
return overflowEntries.isEmpty()
? EmptySet.<Map.Entry<K, V>>iterable()
? Collections.emptySet()
: overflowEntries.entrySet();
}
Iterable<Map.Entry<K, V>> getOverflowEntriesDescending() {
return overflowEntriesDescending.isEmpty()
? EmptySet.<Map.Entry<K, V>>iterable()
? Collections.emptySet()
: overflowEntriesDescending.entrySet();
}
@ -597,45 +596,6 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> {
}
}
/**
* 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<Object> ITERATOR =
new Iterator<Object>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
private static final Iterable<Object> ITERABLE =
new Iterable<Object>() {
@Override
public Iterator<Object> iterator() {
return ITERATOR;
}
};
@SuppressWarnings("unchecked")
static <T> Iterable<T> iterable() {
return (Iterable<T>) ITERABLE;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {

Loading…
Cancel
Save