public final class CyclicSerializableLinkedHashSet<T>
extends com.google.common.collect.ForwardingSet<T>
implements java.io.Serializable
LinkedHashSet
that can be safely (de)serialized when involved in a reference cycle.
The motivating usecase of this class is the afformentioned cycles. Consider the following example:
class Foo implements Serializable {
private LinkedHashSet<Foo> children = new LinkedHashSet<>();
private Object hashSource = new Object();
public void addChild(Foo child) {
children.addChild(child);
}
{@literal @}Override
public int hashCode() {
return hashSouce.hashCode();
}
}
****
Foo f = new Foo();
f.addChild(f);
****
If f
were serialized and reserialized, there is a possiblity of exceptions via the
following steps. This class is designed to eliminate such issue.
f
begins deserialization
f.children
begins deserialization
f.children
attempts to add f
to itself
f.hashCode()
is called
f.hashSource
is null
, throwing a NullPointerException
Constructor and Description |
---|
CyclicSerializableLinkedHashSet() |
Modifier and Type | Method and Description |
---|---|
protected java.util.Set<T> |
delegate() |
equals, hashCode, standardEquals, standardHashCode, standardRemoveAll
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, standardAddAll, standardClear, standardContains, standardContainsAll, standardIsEmpty, standardRemove, standardRetainAll, standardToArray, standardToArray, standardToString, toArray, toArray
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Copyright © 2009-2018 Google. All Rights Reserved.