Skip to content

Commit

Permalink
#923 Add helper method to register serializers for `java.util.Immutab…
Browse files Browse the repository at this point in the history
…leCollections` (#933)
  • Loading branch information
theigl authored Dec 21, 2022
1 parent bb10a0b commit 4785d75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
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

0 comments on commit 4785d75

Please sign in to comment.