Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
de.javakaffee.kryoserializers.dexx.SetSerializer Maven / Gradle / Ivy
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> {
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) {
output.writeInt(object.size(), true);
for (Object elm : object) {
kryo.writeClassAndObject(output, elm);
}
}
@Override
public Set read(Kryo kryo, Input input, Class> type) {
final int size = input.readInt(true);
Builder> 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);
}
}