Skip to content

Commit

Permalink
chore(java): clean code for Fury ArrayAsList (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang authored Apr 24, 2024
1 parent 2fde90d commit ca2f873
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.fury.resolver.ClassInfoHolder;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.resolver.RefResolver;
import org.apache.fury.serializer.collection.ArrayAsList;
import org.apache.fury.serializer.collection.CollectionFlags;
import org.apache.fury.serializer.collection.FuryArrayAsListSerializer;
import org.apache.fury.type.Type;
Expand Down Expand Up @@ -545,14 +544,14 @@ public double[] read(MemoryBuffer buffer) {
public static final class StringArraySerializer extends Serializer<String[]> {
private final StringSerializer stringSerializer;
private final FuryArrayAsListSerializer collectionSerializer;
private final ArrayAsList<String> list;
private final FuryArrayAsListSerializer.ArrayAsList<String> list;

public StringArraySerializer(Fury fury) {
super(fury, String[].class);
stringSerializer = new StringSerializer(fury);
collectionSerializer = new FuryArrayAsListSerializer(fury);
collectionSerializer.setElementSerializer(stringSerializer);
list = new ArrayAsList<>(0);
list = new FuryArrayAsListSerializer.ArrayAsList<>(0);
}

@Override
Expand All @@ -571,7 +570,7 @@ public void write(MemoryBuffer buffer, String[] value) {
// TODO reference support
// this method won't throw exception.
int flags = collectionSerializer.writeNullabilityHeader(buffer, list);
list.setArray(null); // clear for gc
list.clearArray(); // clear for gc
StringSerializer stringSerializer = this.stringSerializer;
if ((flags & CollectionFlags.HAS_NULL) != CollectionFlags.HAS_NULL) {
for (String elem : value) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package org.apache.fury.serializer.collection;

import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.RandomAccess;
import org.apache.fury.Fury;
import org.apache.fury.annotation.Internal;
import org.apache.fury.memory.MemoryBuffer;
Expand All @@ -28,7 +31,8 @@
/** Serializer for {@link ArrayAsList}. Helper for serialization of other classes. */
@Internal
@SuppressWarnings("rawtypes")
public final class FuryArrayAsListSerializer extends CollectionSerializer<ArrayAsList> {
public final class FuryArrayAsListSerializer
extends CollectionSerializer<FuryArrayAsListSerializer.ArrayAsList> {
public FuryArrayAsListSerializer(Fury fury) {
super(fury, ArrayAsList.class, true);
}
Expand All @@ -43,4 +47,91 @@ public Collection newCollection(MemoryBuffer buffer) {
setNumElements(numElements);
return new ArrayAsList(numElements);
}

/**
* A List which wrap a Java array like `java.util.Arrays.ArrayList`, but allow to replace wrapped
* array. Used for serialization only, do not use it in other scenarios.
*/
@Internal
public static class ArrayAsList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable {
private static final Object[] EMPTY = new Object[0];

private E[] array;
private int size;

@SuppressWarnings("unchecked")
public ArrayAsList(int size) {
array = (E[]) new Object[size];
}

@Override
public int size() {
return size;
}

@Override
public boolean add(E e) {
array[size++] = e;
return true;
}

@Override
public E get(int index) {
return array[index];
}

@SuppressWarnings("unchecked")
public void clearArray() {
size = 0;
array = (E[]) EMPTY;
}

public void setArray(E[] a) {
array = a;
size = a.length;
}

public E[] getArray() {
return array;
}

@Override
public E set(int index, E element) {
throw new UnsupportedOperationException();
}

@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException();
}

@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException();
}

/** Returns original array without copy. */
@Override
public Object[] toArray() {
return array;
}

@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private int index;

@Override
public boolean hasNext() {
return index < array.length;
}

@Override
public E next() {
return array[index++];
}
};
}
}
}

0 comments on commit ca2f873

Please sign in to comment.