-
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.
- Loading branch information
Showing
2 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/de/javakaffee/kryoserializers/guava/ReverseListSerializer.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,78 @@ | ||
package de.javakaffee.kryoserializers.guava; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import com.esotericsoftware.kryo.serializers.CollectionSerializer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* A {@link Lists.ReverseList} Serializer. | ||
* Treat as a {@link List} by reversing before write and after read. | ||
*/ | ||
public abstract class ReverseListSerializer extends Serializer<Object> { | ||
|
||
private static final CollectionSerializer serializer = new CollectionSerializer(); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void write(Kryo kryo, Output output, Object object) { | ||
// reverse the ReverseList to get the "forward" list, and treat as regular List. | ||
List forwardList = Lists.reverse((List<Object>) object); | ||
serializer.write(kryo, output, forwardList); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Object copy(Kryo kryo, Object original) { | ||
List forwardList = Lists.reverse((List) original); | ||
return Lists.reverse((List<Object>) serializer.copy(kryo, forwardList)); | ||
} | ||
|
||
public static void registerSerializers(final Kryo kryo) { | ||
kryo.register(Lists.reverse(Lists.newLinkedList()).getClass(), forReverseList()); | ||
kryo.register(Lists.reverse(Lists.newArrayList()).getClass(), forRandomAccessReverseList()); | ||
} | ||
|
||
public static ReverseListSerializer forReverseList() { | ||
return new ReverseListSerializer.ReverseList(); | ||
} | ||
|
||
public static ReverseListSerializer forRandomAccessReverseList() { | ||
return new ReverseListSerializer.RandomAccessReverseList(); | ||
} | ||
|
||
/** | ||
* A {@link Lists.ReverseList} implementation based on a {@link LinkedList}. | ||
*/ | ||
private static class ReverseList extends ReverseListSerializer { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Object read(Kryo kryo, Input input, Class<Object> type) { | ||
// reading a "forward" list as a LinkedList and returning the reversed list. | ||
List forwardList = (List) serializer.read(kryo, input, (Class) LinkedList.class); | ||
return Lists.reverse(forwardList); | ||
} | ||
} | ||
|
||
/** | ||
* A {@link Lists.ReverseList} implementation based on an {@link ArrayList}. | ||
*/ | ||
private static class RandomAccessReverseList extends ReverseListSerializer { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Object read(Kryo kryo, Input input, Class<Object> type) { | ||
// reading a "forward" list as a ArrayList and returning the reversed list. | ||
List forwardList = (List) serializer.read(kryo, input, (Class) ArrayList.class); | ||
return Lists.reverse(forwardList); | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/test/java/de/javakaffee/kryoserializers/guava/ReverseListSerializerTest.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,123 @@ | ||
package de.javakaffee.kryoserializers.guava; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
|
||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static de.javakaffee.kryoserializers.KryoTest.deserialize; | ||
import static de.javakaffee.kryoserializers.KryoTest.serialize; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
|
||
/** | ||
* Test for {@link ReverseListSerializer}. | ||
*/ | ||
public class ReverseListSerializerTest { | ||
|
||
private Kryo _kryo; | ||
|
||
@BeforeTest | ||
public void setUp() throws Exception { | ||
_kryo = new Kryo(); | ||
|
||
ReverseListSerializer.registerSerializers(_kryo); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testEmptyReverseList() { | ||
testEmpty(Lists.reverse(Lists.newLinkedList())); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testEmptyRandomAccessReverseList() { | ||
testEmpty(Lists.reverse(Lists.newArrayList())); | ||
} | ||
|
||
private void testEmpty(final List<?> reverseList) { | ||
final byte[] serialized = serialize( _kryo, reverseList ); | ||
final List<?> deserialized = deserialize(_kryo, serialized, reverseList.getClass()); | ||
assertEquals(deserialized.getClass(), reverseList.getClass()); | ||
assertTrue(deserialized.isEmpty()); | ||
assertEquals(deserialized.size(), reverseList.size()); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testSingletonReverseList() { | ||
testSingleton(Lists.reverse(Lists.newLinkedList(Collections.singleton(3)))); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testSingletonRandomAccessReverseList() { | ||
testSingleton(Lists.reverse(Lists.newArrayList(Collections.singleton(3)))); | ||
} | ||
|
||
private void testSingleton(final List<?> reverseList) { | ||
final byte[] serialized = serialize( _kryo, reverseList ); | ||
final List<?> deserialized = deserialize(_kryo, serialized, reverseList.getClass()); | ||
assertEquals(deserialized.getClass(), reverseList.getClass()); | ||
assertEquals(deserialized, reverseList); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testRegularReverseList() { | ||
testRegular(Lists.reverse(Lists.newLinkedList(Arrays.asList(3, 4, 5, 6)))); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testRegularRandomAccessReverseList() { | ||
testRegular(Lists.reverse(Lists.newArrayList(3, 4, 5, 6))); | ||
} | ||
|
||
private void testRegular(final List<?> reverseList) { | ||
final byte[] serialized = serialize( _kryo, reverseList ); | ||
final List<?> deserialized = deserialize(_kryo, serialized, reverseList.getClass()); | ||
assertEquals(deserialized.getClass(), reverseList.getClass()); | ||
assertEquals(deserialized, reverseList); | ||
} | ||
|
||
// Kryo#copy tests | ||
|
||
@Test( enabled = true ) | ||
public void testCopyEmptyReverseList() { | ||
testCopy(Lists.reverse(Lists.newLinkedList())); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testCopyEmptyRandomAccessReverseList() { | ||
testCopy(Lists.reverse(Lists.newArrayList())); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testCopySingletonReverseList() { | ||
testCopy(Lists.reverse(Lists.newLinkedList(Collections.singleton(3)))); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testCopySingletonRandomAccessReverseList() { | ||
testCopy(Lists.reverse(Lists.newArrayList(Collections.singleton(3)))); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testCopyRegularReverseList() { | ||
testCopy(Lists.reverse(Lists.newLinkedList(Arrays.asList(3, 4, 5, 6)))); | ||
} | ||
|
||
@Test( enabled = true ) | ||
public void testCopyRegularRandomAccessReverseList() { | ||
testCopy(Lists.reverse(Lists.newArrayList(3, 4, 5, 6))); | ||
} | ||
|
||
private void testCopy(List<?> obj) { | ||
final List<?> copied = _kryo.copy(obj); | ||
assertEquals(copied.getClass(), obj.getClass()); | ||
assertEquals(copied, obj); | ||
} | ||
} |