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

org.broadinstitute.hellbender.tools.spark.utils.HopscotchCollectionSpark Maven / Gradle / Ivy

The newest version!
package org.broadinstitute.hellbender.tools.spark.utils;

import com.esotericsoftware.kryo.DefaultSerializer;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.broadinstitute.hellbender.utils.collections.HopscotchCollection;

import java.util.Collection;

@DefaultSerializer(HopscotchCollectionSpark.Serializer.class)
public class HopscotchCollectionSpark extends HopscotchCollection {
    /** make a small HopscotchCollectionSpark */
    public HopscotchCollectionSpark() {}

    /** make a HopscotchCollectionSpark for a specified capacity (or good guess) */
    public HopscotchCollectionSpark( final int capacity ) { super(capacity); }

    /** make a HopscotchCollectionSpark from a collection */
    public HopscotchCollectionSpark( final Collection collection ) { super(collection); }

    @SuppressWarnings("unchecked")
    protected HopscotchCollectionSpark( final Kryo kryo, final Input input ) {
        super((int)(input.readInt() * HopscotchCollection.LOAD_FACTOR));

        final boolean oldReferences = kryo.getReferences();
        kryo.setReferences(false);

        int nElements = input.readInt();
        while ( nElements-- > 0 ) {
            add((T)kryo.readClassAndObject(input));
        }

        kryo.setReferences(oldReferences);
    }

    @SuppressWarnings("unchecked")
    protected void serialize( final Kryo kryo, final Output output ) {
        final boolean oldReferences = kryo.getReferences();
        kryo.setReferences(false);

        final int capacity = capacity();
        output.writeInt(capacity);
        output.writeInt(size());

        // write the chain heads, and then the squatters
        chainHeads().forEach(t -> kryo.writeClassAndObject(output, t));
        squatters().forEach(t -> kryo.writeClassAndObject(output, t));

        kryo.setReferences(oldReferences);
    }

    public static final class Serializer extends com.esotericsoftware.kryo.Serializer> {
        @Override
        public void write( final Kryo kryo, final Output output, final HopscotchCollectionSpark hopscotchCollection ) {
            hopscotchCollection.serialize(kryo, output);
        }

        @Override
        public HopscotchCollectionSpark read( final Kryo kryo, final Input input,
                                            final Class> klass ) {
            return new HopscotchCollectionSpark<>(kryo, input);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy