-
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.
Merge branch 'aaronpoweruser-master'
- Loading branch information
Showing
8 changed files
with
435 additions
and
3 deletions.
There are no files selected for viewing
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
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
70 changes: 70 additions & 0 deletions
70
src/main/java/de/javakaffee/kryoserializers/dexx/ListSerializer.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,70 @@ | ||
package de.javakaffee.kryoserializers.dexx; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
import com.github.andrewoma.dexx.collection.IndexedLists; | ||
import com.github.andrewoma.dexx.collection.List; | ||
|
||
/** | ||
* A kryo {@link Serializer} for dexx {@link List} | ||
*/ | ||
public class ListSerializer extends Serializer<List> { | ||
|
||
private static final boolean DOES_NOT_ACCEPT_NULL = true; | ||
private static final boolean IMMUTABLE = true; | ||
|
||
public ListSerializer() { | ||
super(DOES_NOT_ACCEPT_NULL, IMMUTABLE); | ||
} | ||
|
||
@Override | ||
public void write(Kryo kryo, Output output, List object) { | ||
output.writeInt(object.size(), true); | ||
for (Object elm : object) { | ||
kryo.writeClassAndObject(output, elm); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Object> read(Kryo kryo, Input input, Class<List> aClass) { | ||
final int size = input.readInt(true); | ||
final Object[] list = new Object[size]; | ||
for (int i = 0; i < size; ++i) { | ||
list[i] = kryo.readClassAndObject(input); | ||
} | ||
return IndexedLists.copyOf(list); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ImmutableListSerializer} and registers its serializer | ||
* | ||
* @param kryo the {@link Kryo} instance to set the serializer on | ||
*/ | ||
public static void registerSerializers(final Kryo kryo) { | ||
|
||
final ListSerializer serializer = new ListSerializer(); | ||
|
||
kryo.register(List.class, serializer); | ||
|
||
// Note: | ||
// Only registering above is good enough for serializing/deserializing. | ||
// but if using Kryo#copy, following is required. | ||
|
||
kryo.register(IndexedLists.of().getClass(), serializer); | ||
kryo.register(IndexedLists.of(1).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6,7).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6,7,8).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6,7,8,9).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6,7,8,10).getClass(), serializer); | ||
kryo.register(IndexedLists.of(1,2,3,4,5,6,7,8,10,11).getClass(), serializer); | ||
|
||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/de/javakaffee/kryoserializers/dexx/MapSerializer.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,73 @@ | ||
package de.javakaffee.kryoserializers.dexx; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
import com.github.andrewoma.dexx.collection.Map; | ||
import com.github.andrewoma.dexx.collection.Maps; | ||
import com.github.andrewoma.dexx.collection.Pair; | ||
|
||
import java.util.HashMap; | ||
import java.util.ArrayList; | ||
import java.util.Map.Entry; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* A kryo {@link Serializer} for dexx {@link Map} | ||
*/ | ||
public class MapSerializer extends Serializer<Map<Object, ? extends Object>> { | ||
|
||
private static final boolean DOES_NOT_ACCEPT_NULL = true; | ||
private static final boolean IMMUTABLE = true; | ||
|
||
public MapSerializer() { | ||
super(DOES_NOT_ACCEPT_NULL, IMMUTABLE); | ||
} | ||
|
||
@Override | ||
public void write(Kryo kryo, Output output, Map<Object, ? extends Object> immutableMap) { | ||
kryo.writeObject(output, immutableMap.asMap()); | ||
} | ||
|
||
@Override | ||
public Map<Object, Object> read(Kryo kryo, Input input, Class<Map<Object, ? extends Object>> type) { | ||
HashMap<Object, Object> map = kryo.readObject(input, HashMap.class); | ||
ArrayList<Pair<Object, Object>> listOfPairs = new ArrayList(); | ||
|
||
for (Entry<Object, Object> entry : map.entrySet()) { | ||
Pair pair = new Pair(entry.getKey(), entry.getValue()); | ||
listOfPairs.add(pair); | ||
} | ||
|
||
return Maps.copyOf(listOfPairs); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ImmutableMapSerializer} 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 MapSerializer serializer = new MapSerializer(); | ||
|
||
kryo.register(Map.class, serializer); | ||
kryo.register(Maps.of().getClass(), serializer); | ||
|
||
Object o1 = new Object(); | ||
Object o2 = new Object(); | ||
Object o3 = new Object(); | ||
Object o4 = new Object(); | ||
Object o5 = new Object(); | ||
|
||
kryo.register(Maps.of(o1, o1).getClass(), serializer); | ||
kryo.register(Maps.of(o1, o1, o2, o2).getClass(), serializer); | ||
kryo.register(Maps.of(o1, o1, o2, o2, o3, o3).getClass(), serializer); | ||
kryo.register(Maps.of(o1, o1, o2, o2, o3, o3, o4, o4).getClass(), serializer); | ||
kryo.register(Maps.of(o1, o1, o2, o2, o3, o3, o4, o4, o5, o5).getClass(), serializer); | ||
|
||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/de/javakaffee/kryoserializers/dexx/SetSerializer.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,71 @@ | ||
package de.javakaffee.kryoserializers.dexx; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
import com.github.andrewoma.dexx.collection.Builder; | ||
import com.github.andrewoma.dexx.collection.Set; | ||
import com.github.andrewoma.dexx.collection.Sets; | ||
|
||
/** | ||
* A kryo {@link Serializer} for dexx {@link Set} | ||
*/ | ||
public class SetSerializer extends Serializer<Set<Object>> { | ||
|
||
private static final boolean DOES_NOT_ACCEPT_NULL = false; | ||
private static final boolean IMMUTABLE = true; | ||
|
||
public SetSerializer() { | ||
super(DOES_NOT_ACCEPT_NULL, IMMUTABLE); | ||
} | ||
|
||
@Override | ||
public void write(Kryo kryo, Output output, Set<Object> object) { | ||
output.writeInt(object.size(), true); | ||
for (Object elm : object) { | ||
kryo.writeClassAndObject(output, elm); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<Object> read(Kryo kryo, Input input, Class<Set<Object>> type) { | ||
final int size = input.readInt(true); | ||
Builder<Object, Set<Object>> builder = Sets.builder(); | ||
for (int i = 0; i < size; ++i) { | ||
builder.add(kryo.readClassAndObject(input)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ImmutableSetSerializer} and registers its serializer | ||
* for the several ImmutableSet related classes. | ||
* | ||
* @param kryo the {@link Kryo} instance to set the serializer on | ||
*/ | ||
public static void registerSerializers(final Kryo kryo) { | ||
|
||
final SetSerializer serializer = new SetSerializer(); | ||
|
||
kryo.register(Set.class, serializer); | ||
|
||
// Note: | ||
// Only registering above is good enough for serializing/deserializing. | ||
// but if using Kryo#copy, following is required. | ||
|
||
kryo.register(Sets.of().getClass(), serializer); | ||
kryo.register(Sets.of(1).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6,7).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6,7,8).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6,7,8,9).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6,7,8,9,10).getClass(), serializer); | ||
kryo.register(Sets.of(1,2,3,4,5,6,7,8,9,10,11).getClass(), serializer); | ||
|
||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/java/de/javakaffee/kryoserializers/dexx/ListSerializerTest.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,84 @@ | ||
package de.javakaffee.kryoserializers.dexx; | ||
|
||
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.assertSame; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
|
||
import com.github.andrewoma.dexx.collection.ArrayList; | ||
import com.github.andrewoma.dexx.collection.IndexedLists; | ||
import com.github.andrewoma.dexx.collection.List; | ||
|
||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Test for {@link ListSerializer} | ||
*/ | ||
public class ListSerializerTest { | ||
|
||
private Kryo _kryo; | ||
|
||
@BeforeTest | ||
public void setUp() throws Exception { | ||
_kryo = new Kryo(); | ||
|
||
ListSerializer.registerSerializers(_kryo); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testEmpty() { | ||
final List<?> obj = IndexedLists.of(); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final List<?> deserialized = deserialize(_kryo, serialized, List.class); | ||
assertTrue(deserialized.isEmpty()); | ||
assertEquals(deserialized.size(), obj.size()); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testRegular() { | ||
final List<?> obj = IndexedLists.of(3,4,5,6,7); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final List<?> deserialized = deserialize(_kryo, serialized, List.class); | ||
assertEquals(deserialized, obj); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testCopyOfIterable() { | ||
final ArrayList<Object> iterable = new ArrayList<Object>(); | ||
iterable.append(new Object()); | ||
final List<?> obj = IndexedLists.copyOf(iterable.asList()); | ||
final byte[] serialized = serialize(_kryo, obj); | ||
final List<?> deserialized = deserialize(_kryo, serialized, List.class); | ||
assertEquals(deserialized, obj); | ||
} | ||
|
||
// Kryo#copy tests | ||
|
||
@Test(enabled = true) | ||
public void testCopyEmpty() { | ||
final List<?> obj = IndexedLists.of(); | ||
final List<?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testCopyRegular() { | ||
final List<?> obj = IndexedLists.of(2,3,4,5); | ||
final List<?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testCopyCopyOfIterable() { | ||
final ArrayList<Object> iterable = new ArrayList<Object>(); | ||
iterable.append(new Object()); | ||
final List<?> obj = IndexedLists.copyOf(iterable.asList()); | ||
List<?> copied = _kryo.copy(obj); | ||
assertSame(copied, obj); | ||
} | ||
} |
Oops, something went wrong.