-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serializer for Guava's ImmutableSortedMap
- Loading branch information
Oleksii Voronin
committed
Aug 24, 2017
1 parent
e334698
commit 2151947
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/de/javakaffee/kryoserializers/guava/ImmutableSortedMapSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package de.javakaffee.kryoserializers.guava; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import com.google.common.collect.ImmutableSortedMap; | ||
import com.google.common.collect.Maps; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
|
||
/** | ||
* A kryo {@link Serializer} for guava-libraries {@link ImmutableSortedMap}. | ||
*/ | ||
public class ImmutableSortedMapSerializer extends Serializer<ImmutableSortedMap<Object, ? extends Object>> { | ||
|
||
private static final boolean DOES_NOT_ACCEPT_NULL = true; | ||
private static final boolean IMMUTABLE = true; | ||
|
||
public ImmutableSortedMapSerializer() { | ||
super(DOES_NOT_ACCEPT_NULL, IMMUTABLE); | ||
} | ||
|
||
@Override | ||
public void write(Kryo kryo, Output output, ImmutableSortedMap<Object, ? extends Object> immutableMap) { | ||
kryo.writeObject(output, Maps.newTreeMap(immutableMap)); | ||
} | ||
|
||
@Override | ||
public ImmutableSortedMap<Object, Object> read(Kryo kryo, Input input, Class<ImmutableSortedMap<Object, ? extends Object>> type) { | ||
Map map = kryo.readObject(input, TreeMap.class); | ||
return ImmutableSortedMap.copyOf(map); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ImmutableSortedMapSerializer} and registers its serializer | ||
* for the several ImmutableMap related classes. | ||
* | ||
* @param kryo the {@link Kryo} instance to set the serializer on | ||
*/ | ||
public static void registerSerializers(final Kryo kryo) { | ||
|
||
final ImmutableSortedMapSerializer serializer = new ImmutableSortedMapSerializer(); | ||
|
||
kryo.register(ImmutableSortedMap.class, serializer); | ||
kryo.register(ImmutableSortedMap.of().getClass(), serializer); | ||
|
||
final Comparable<Object> k1 = new Comparable<Object>() { | ||
@Override | ||
public int compareTo(Object o) { | ||
return o == this ? 0 : -1; | ||
} | ||
}; | ||
final Comparable<Object> k2 = new Comparable<Object>() { | ||
@Override | ||
public int compareTo(Object o) { | ||
return o == this ? 0 : 1; | ||
} | ||
}; | ||
final Object v1 = new Object(); | ||
final Object v2 = new Object(); | ||
|
||
kryo.register(ImmutableSortedMap.of(k1, v1).getClass(), serializer); | ||
kryo.register(ImmutableSortedMap.of(k1, v1, k2, v2).getClass(), serializer); | ||
|
||
Map<DummyEnum,Object> enumMap = new EnumMap<DummyEnum, Object>(DummyEnum.class); | ||
for (DummyEnum e : DummyEnum.values()) { | ||
enumMap.put(e, v1); | ||
} | ||
|
||
kryo.register(ImmutableSortedMap.copyOf(enumMap).getClass(), serializer); | ||
} | ||
|
||
private enum DummyEnum { | ||
VALUE1, | ||
VALUE2 | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/test/java/de/javakaffee/kryoserializers/guava/ImmutableSortedMapSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package de.javakaffee.kryoserializers.guava; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.google.common.collect.ImmutableSortedMap; | ||
import com.google.common.collect.ImmutableSortedMap; | ||
import com.google.common.collect.ImmutableSortedSet; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.EnumMap; | ||
|
||
import static de.javakaffee.kryoserializers.KryoTest.deserialize; | ||
import static de.javakaffee.kryoserializers.KryoTest.serialize; | ||
import static org.testng.Assert.*; | ||
|
||
public class ImmutableSortedMapSerializerTest { | ||
|
||
private enum Planet { | ||
MERCURY, VENUS, EARTH, MARS; | ||
} | ||
|
||
private Kryo _kryo; | ||
|
||
@BeforeTest | ||
public void setUp() throws Exception { | ||
_kryo = new Kryo(); | ||
ImmutableSortedMapSerializer.registerSerializers(_kryo); | ||
} | ||
|
||
@Test | ||
public void testEmpty() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final ImmutableSortedMap<?, ?> deserialized = deserialize(_kryo, serialized, ImmutableSortedMap.class); | ||
assertTrue(deserialized.isEmpty()); | ||
assertEquals(deserialized.size(), obj.size()); | ||
} | ||
|
||
@Test | ||
public void testSingleton() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(3, "k"); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final ImmutableSortedMap<?, ?> deserialized = deserialize(_kryo, serialized, ImmutableSortedMap.class); | ||
assertEquals(deserialized, obj); | ||
} | ||
|
||
@Test | ||
public void testRegular() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(5, "r", 3, "k", 6, "y"); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final ImmutableSortedMap<?, ?> deserialized = deserialize(_kryo, serialized, ImmutableSortedMap.class); | ||
assertEquals(deserialized, obj); | ||
} | ||
|
||
|
||
@Test | ||
public void testDescending() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(1, "a", 5, "e", 3, "c").descendingMap(); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final ImmutableSortedMap<?, ?> deserialized = deserialize(_kryo, serialized, ImmutableSortedMap.class); | ||
assertEquals(deserialized, obj); | ||
} | ||
|
||
@Test | ||
public void testEnum() { | ||
final EnumMap<Planet, String> obj = new EnumMap<Planet, String>(Planet.class); | ||
for (Planet p : Planet.values()) { | ||
obj.put(p, p.name()); | ||
} | ||
|
||
final ImmutableSortedMap<?, ?> immutableObj = ImmutableSortedMap.copyOf(obj); | ||
final byte[] serialized = serialize(_kryo, immutableObj); | ||
final ImmutableSortedMap<?, ?> deserialized = deserialize(_kryo, serialized, ImmutableSortedMap.class); | ||
assertEquals(deserialized, immutableObj); | ||
} | ||
|
||
// Kryo#copy tests | ||
|
||
@Test | ||
public void testCopyEmpty() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(); | ||
final ImmutableSortedMap<?, ?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
|
||
@Test | ||
public void testCopySingleton() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(1, 1); | ||
final ImmutableSortedMap<?, ?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
|
||
@Test | ||
public void testCopyRegular() { | ||
final ImmutableSortedMap<?, ?> obj = ImmutableSortedMap.of(1, 2, 3, 4); | ||
final ImmutableSortedMap<?, ?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
|
||
} |