Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializable StringListView #560

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
}
}
Loading