All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.javakaffee.kryoserializers.guava.ArrayTableSerializer Maven / Gradle / Ivy

Go to download

Additional kryo (http://kryo.googlecode.com) serializers for standard jdk types (e.g. currency, jdk proxies) and some for external libs (e.g. joda time, cglib proxies, wicket).

The newest version!
package de.javakaffee.kryoserializers.guava;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Registration;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.common.collect.ArrayTable;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
 * A kryo {@link Serializer} for guava-libraries {@link ArrayTable}.
 */
public class ArrayTableSerializer extends TableSerializerBase> {

    private static final boolean HANDLES_NULL = false;
    private static final boolean IMMUTABLE = false;

    public ArrayTableSerializer() {
        super(HANDLES_NULL, IMMUTABLE);
    }

    @Override
    public void write(Kryo kryo, Output output, ArrayTable table) {
        List rowKeys = table.rowKeyList();
        List columnKeys = table.columnKeyList();
        kryo.writeClassAndObject(output, rowKeys);
        kryo.writeClassAndObject(output, columnKeys);
        for (R rowKey : rowKeys) {
            for (C columnKey : columnKeys) {
                V val = table.get(rowKey, columnKey);
                kryo.writeClassAndObject(output, val);
            }
        }
    }

    @Override
    public ArrayTable read(Kryo kryo, Input input, Class> type) {
        List rowKeys = (List) kryo.readClassAndObject(input);
        List columnKeys = (List) kryo.readClassAndObject(input);
        ArrayTable table = ArrayTable.create(rowKeys, columnKeys);
        for (R rowKey : rowKeys) {
            for (C columnKey : columnKeys) {
                V val = (V) kryo.readClassAndObject(input);
                table.put(rowKey, columnKey, val);
            }
        }
        return table;
    }

    @Override
    public ArrayTable copy(final Kryo kryo, final ArrayTable original) {
        return ArrayTable.create(original);
    }

    /* kryo.getSerializer (invoking kryo.getRegistration) throws an exception if registration is required,
     * therefore we're getting a potentially existing serializer on another way.
     */
    private static Serializer getSerializer(Kryo kryo, Class type) {
        Registration registration = kryo.getClassResolver().getRegistration(type);
        return registration != null ? registration.getSerializer() : null;
    }

    /**
     * Creates a new {@link ArrayTableSerializer} and registers its serializer.
     *
     * @param kryo the {@link Kryo} instance to set the serializer on
     */
    public static void registerSerializers(final Kryo kryo) {

        // ImmutableList is used by ArrayTable. However,
        // we already have a separate serializer class for ImmutableList,
        // ImmutableListSerializer. If it is not already being used, register it.
        Serializer immutableListSerializer = getSerializer(kryo, ImmutableList.class);
        if (!(immutableListSerializer instanceof ImmutableListSerializer)) {
            ImmutableListSerializer.registerSerializers(kryo);
        }

        final ArrayTableSerializer serializer = new ArrayTableSerializer();
        kryo.register(ArrayTable.class, serializer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy