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

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

There is a newer version: 4.6.0.0
Show 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 java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;

/**
 * Implements Set by imposing a unique-element constraint on HopscotchCollection.
 * Also implements equals and hashCode to be consistent with the documented requirements of the java Set interface.
 */
@DefaultSerializer(HopscotchSet.Serializer.class)
public class HopscotchSet extends HopscotchCollection implements Set {
    public HopscotchSet() {}
    public HopscotchSet( final int capacity ) { super(capacity); }
    public HopscotchSet( final Collection collection ) { super(collection); }
    protected HopscotchSet( final Kryo kryo, final Input input ) { super(kryo, input); }

    @Override
    public final boolean equals( final Object obj ) {
        if ( this == obj ) return true;
        if ( !(obj instanceof Set) ) return false;
        @SuppressWarnings("rawtypes")
        final Set that = (Set)obj;
        return this.size() == that.size() && this.containsAll(that);
    }

    @Override
    public final int hashCode() { return stream().mapToInt(Objects::hashCode).sum(); }

    /** in a set, uniqueness is on the entry, so we just compare entries to see if we already have an identical one */
    @Override
    protected BiPredicate entryCollides() { return Object::equals; }

    @SuppressWarnings("rawtypes")
    public static final class Serializer extends com.esotericsoftware.kryo.Serializer {
        @Override
        public void write( final Kryo kryo, final Output output, final HopscotchSet hopscotchSet ) {
            hopscotchSet.serialize(kryo, output);
        }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy