Skip to content

Commit

Permalink
Update ImmutableLinkedSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 6, 2024
1 parent e59f818 commit 08af664
Showing 1 changed file with 7 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

@SuppressWarnings("unchecked")
public final class ImmutableLinkedSeq<E> extends AbstractImmutableSeq<E> implements Serializable {
@Serial
private static final long serialVersionUID = -2711437988200648694L;

private static final Factory<?> FACTORY = new Factory<>();

private static final Node<?> NIL_NODE = new Node<>();
Expand Down Expand Up @@ -396,57 +393,13 @@ public E getLast() {

@Serial
private Object writeReplace() {
return new SerializationReplaced<>(this);
return new SerializationWrapper<>(factory(), this);
}

private static final class SerializationReplaced<E> implements Serializable, Externalizable {
private static final class Factory<E> implements CollectionFactory<E, MutableSinglyLinkedList<E>, ImmutableLinkedSeq<E>>, Serializable {
@Serial
private static final long serialVersionUID = 0L;

private ImmutableLinkedSeq<E> value;

public SerializationReplaced() {
}

public SerializationReplaced(ImmutableLinkedSeq<E> value) {
this.value = value;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(value.size());
for (E v : value) {
out.writeObject(v);
}
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
int size = in.readInt();
if (size == 0) {
value = empty();
return;
}

Node<E> node = new Node<>((E) in.readObject());
Node<E> tail = node;
int c = 1;
for (int i = 1; i < size; i++) {
tail = (tail.tail = new Node<>((E) in.readObject()));
c++;
}
tail.tail = nilNode();
this.value = new ImmutableLinkedSeq<>(node, c);
}

@Serial
private Object readResolve() {
return value;
}
}

private static final class Factory<E> implements CollectionFactory<E, MutableSinglyLinkedList<E>, ImmutableLinkedSeq<E>> {

@Override
public MutableSinglyLinkedList<E> newBuilder() {
return new MutableSinglyLinkedList<>();
Expand All @@ -466,6 +419,11 @@ public void addToBuilder(@NotNull MutableSinglyLinkedList<E> builder, E value) {
public MutableSinglyLinkedList<E> mergeBuilder(@NotNull MutableSinglyLinkedList<E> builder1, @NotNull MutableSinglyLinkedList<E> builder2) {
return (MutableSinglyLinkedList<E>) Builder.merge(builder1, builder2);
}

@Serial
private Object readResolve() {
return factory();
}
}

private static final class Node<@Covariant E> {
Expand Down

0 comments on commit 08af664

Please sign in to comment.