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

#923 Add helper to register serializers for java.util.ImmutableCollections #933

Merged
merged 1 commit into from
Dec 21, 2022
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 @@ -41,6 +41,15 @@ public static void addDefaultSerializers (Kryo kryo) {
}
}

/** Creates new serializers for all types of {@link java.util.ImmutableCollections}s and registers them.
*
* @param kryo the {@link Kryo} instance to register the serializers on. */
public static void registerSerializers (Kryo kryo) {
JdkImmutableListSerializer.registerSerializers(kryo);
JdkImmutableMapSerializer.registerSerializers(kryo);
JdkImmutableSetSerializer.registerSerializers(kryo);
}

static class JdkImmutableListSerializer extends CollectionSerializer<List<Object>> {

private JdkImmutableListSerializer () {
Expand Down Expand Up @@ -79,6 +88,14 @@ static void addDefaultSerializers (Kryo kryo) {
kryo.addDefaultSerializer(List.of(1, 2, 3, 4).getClass(), serializer);
kryo.addDefaultSerializer(List.of(1, 2, 3, 4).subList(0, 2).getClass(), serializer);
}

static void registerSerializers (Kryo kryo) {
final JdkImmutableListSerializer serializer = new JdkImmutableListSerializer();
kryo.register(List.of().getClass(), serializer);
kryo.register(List.of(1).getClass(), serializer);
kryo.register(List.of(1, 2, 3, 4).getClass(), serializer);
kryo.register(List.of(1, 2, 3, 4).subList(0, 2).getClass(), serializer);
}
}

static class JdkImmutableMapSerializer extends MapSerializer<Map<Object, Object>> {
Expand Down Expand Up @@ -119,6 +136,13 @@ static void addDefaultSerializers (Kryo kryo) {
kryo.addDefaultSerializer(Map.of(1, 2).getClass(), serializer);
kryo.addDefaultSerializer(Map.of(1, 2, 3, 4).getClass(), serializer);
}

static void registerSerializers (Kryo kryo) {
final JdkImmutableMapSerializer serializer = new JdkImmutableMapSerializer();
kryo.register(Map.of().getClass(), serializer);
kryo.register(Map.of(1, 2).getClass(), serializer);
kryo.register(Map.of(1, 2, 3, 4).getClass(), serializer);
}
}

static class JdkImmutableSetSerializer extends CollectionSerializer<Set<Object>> {
Expand Down Expand Up @@ -158,6 +182,13 @@ static void addDefaultSerializers (Kryo kryo) {
kryo.addDefaultSerializer(Set.of(1).getClass(), serializer);
kryo.addDefaultSerializer(Set.of(1, 2, 3, 4).getClass(), serializer);
}

static void registerSerializers (Kryo kryo) {
final JdkImmutableSetSerializer serializer = new JdkImmutableSetSerializer();
kryo.register(Set.of().getClass(), serializer);
kryo.register(Set.of(1).getClass(), serializer);
kryo.register(Set.of(1, 2, 3, 4).getClass(), serializer);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ class ImmutableCollectionsSerializersTest extends KryoTestCase {
@BeforeEach
public void setUp () throws Exception {
super.setUp();
kryo.register(Class.forName("java.util.ImmutableCollections$List12"));
kryo.register(Class.forName("java.util.ImmutableCollections$ListN"));
kryo.register(Class.forName("java.util.ImmutableCollections$SubList"));
kryo.register(Class.forName("java.util.ImmutableCollections$Map1"));
kryo.register(Class.forName("java.util.ImmutableCollections$MapN"));
kryo.register(Class.forName("java.util.ImmutableCollections$Set12"));
kryo.register(Class.forName("java.util.ImmutableCollections$SetN"));

ImmutableCollectionsSerializers.registerSerializers(kryo);
kryo.register(HashMap.class);
kryo.register(TestClass.class);
}
Expand Down