Skip to content

Commit

Permalink
Remove static singleton field from base classes and ensure singletons…
Browse files Browse the repository at this point in the history
… follow the same pattern.

This avoid cycles in clinits that hard to optimize.

RELNOTES=n/a
PiperOrigin-RevId: 590989450
  • Loading branch information
gkdn authored and Google Java Core Libraries committed Dec 14, 2023
1 parent 4fe1df5 commit 5141cb1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Predicate;
Expand All @@ -38,9 +37,6 @@ public abstract class ImmutableCollection<E> extends AbstractCollection<E> imple
static final int SPLITERATOR_CHARACTERISTICS =
Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED;

static final ImmutableCollection<Object> EMPTY_IMMUTABLE_COLLECTION =
new ForwardingImmutableCollection<Object>(Collections.emptyList());

ImmutableCollection() {}

public abstract UnmodifiableIterator<E> iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
@SuppressWarnings("serial") // we're overriding default serialization
public abstract class ImmutableList<E> extends ImmutableCollection<E>
implements List<E>, RandomAccess {
static final ImmutableList<Object> EMPTY =
new RegularImmutableList<Object>(Collections.emptyList());

ImmutableList() {}

Expand All @@ -52,7 +50,7 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E>
// Casting to any type is safe because the list will never hold any elements.
@SuppressWarnings("unchecked")
public static <E> ImmutableList<E> of() {
return (ImmutableList<E>) EMPTY;
return (ImmutableList<E>) RegularImmutableList.EMPTY;
}

public static <E> ImmutableList<E> of(E element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
* @author Hayward Chan
*/
public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
static final ImmutableMap<Object, Object> EMPTY = new RegularImmutableMap<Object, Object>();

abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> {
abstract UnmodifiableIterator<Entry<K, V>> entryIterator();
Expand Down Expand Up @@ -92,7 +91,7 @@ public UnmodifiableIterator<Entry<K, V>> iterator() {
}

public static <K, V> ImmutableMap<K, V> of() {
return (ImmutableMap<K, V>) EMPTY;
return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY;
}

public static <K, V> ImmutableMap<K, V> of(K k1, V v1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

import java.util.List;
Expand All @@ -26,6 +27,9 @@
* @author Hayward Chan
*/
class RegularImmutableList<E> extends ForwardingImmutableList<E> {

static final ImmutableList<Object> EMPTY = new RegularImmutableList<Object>(emptyList());

private final List<E> delegate;
E forSerialization;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
final class RegularImmutableMap<K, V> extends ForwardingImmutableMap<K, V> {

static final ImmutableMap<Object, Object> EMPTY = new RegularImmutableMap<Object, Object>();

RegularImmutableMap(Map<? extends K, ? extends V> delegate) {
super(delegate);
}
Expand Down

0 comments on commit 5141cb1

Please sign in to comment.