Skip to content

Commit

Permalink
Provide faster defensive copy.
Browse files Browse the repository at this point in the history
Without this AbstractCollection defaults to an iterator-based approach instead of a simple array copy.
  • Loading branch information
JakeWharton committed Jun 14, 2016
1 parent 4d08d05 commit c928453
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.RandomAccess;

final class ImmutableList<T> extends AbstractList<T> implements RandomAccess, Serializable {
final List<T> list;
private final ArrayList<T> list;

ImmutableList(List<T> list) {
this.list = new ArrayList<>(list);
Expand All @@ -38,6 +38,10 @@ final class ImmutableList<T> extends AbstractList<T> implements RandomAccess, Se
return list.get(i);
}

@Override public Object[] toArray() {
return list.toArray(); // Optimizing for mutable copy by MutableOnWriteList.
}

private Object writeReplace() throws ObjectStreamException {
return Collections.unmodifiableList(list);
}
Expand Down

0 comments on commit c928453

Please sign in to comment.