Skip to content

Commit

Permalink
Serializable StringListView (#560)
Browse files Browse the repository at this point in the history
* Serializable StringListView
  • Loading branch information
li-ukumar authored Aug 21, 2024
1 parent 16fa706 commit 18f9cfa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
import com.linkedin.avroutil1.compatibility.RandomRecordGenerator;
import com.linkedin.avroutil1.compatibility.RecordGenerationConfig;
import com.linkedin.avroutil1.compatibility.StringConverterUtil;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -1946,6 +1951,20 @@ public void testCharSeqAccessorForNoUtf8() {
Assert.assertEquals((int) instance.intAr.get(instance.getIntAr().size() - 1), Integer.MAX_VALUE);
}

@Test
public void testIfSerializable() throws IOException {
Path tempFile = Files.createTempFile(null, ".tmp");
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
vs19.TestCollections instance = new RandomRecordGenerator().randomSpecific(vs19.TestCollections.class,
RecordGenerationConfig.newConfig().withAvoidNulls(true));

objectOutputStream.writeObject(instance.getStrAr());
} finally {
Files.delete(tempFile);
}
}

@BeforeClass
public void setup() {
System.setProperty("org.apache.avro.specific.use_custom_coders", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
*/
package com.linkedin.avroutil1.compatibility.collectiontransformer;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.stream.Collectors;
import org.apache.avro.util.Utf8;


/**
* View of Utf8 List to allow get as String while still allowing set to reflect on the original object.
*/
public class StringListView extends AbstractList<String> {
public class StringListView extends AbstractList<String> implements Serializable {
// Not final to allow addition
private java.util.List<Utf8> _utf8List;

Expand Down Expand Up @@ -102,4 +104,12 @@ public String next() {
}
};
}

/**
* Custom serialization to write the list of strings instead of the list of Utf8.
* ObjectOutputStream seeks this method to serialize the object.
*/
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
out.writeObject(_utf8List.stream().map(Utf8::toString).collect(Collectors.toList()));
}
}

0 comments on commit 18f9cfa

Please sign in to comment.