/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.sets;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import net.ranides.assira.collection.utils.HashCollection;
import net.ranides.assira.collection.HashFunction;
import net.ranides.assira.collection.arrays.ArrayAllocator;

/**
 * A generic set map implementation whose
 * {@link HashFunction hashing strategy} isspecified at creation time.
 */
public final class CustomSet<K> extends AHashSet<K> {

    private static final long serialVersionUID = 3L;

    protected HashFunction<K> strategy;

    /**
     * Creates a new hash set.
     *
     * <p>The actual table size will be the least power of two greater than
     * <code>expected</code>/
     * <code>f</code>.
     *
     * @param expected the expected number of elements in the hash set.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    @SuppressWarnings("unchecked")
    public CustomSet(int expected, float factor, HashFunction<K> strategy) {
        super(expected, factor);
        this.strategy = strategy;
    }

    /**
     * Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor.
     *
     * @param expected the expected number of elements in the hash set.
     * @param strategy the strategy.
     */
    public CustomSet(int expected, HashFunction<K> strategy) {
        this(expected, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash set with initial expected
     * {@link Hash#DEFAULT_INITIAL_SIZE} elements and
     * {@link Hash#DEFAULT_LOAD_FACTOR} as load factor.
     *
     * @param strategy the strategy.
     */
    public CustomSet(HashFunction<K> strategy) {
        this(HashCollection.INITIAL_SIZE, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash set copying a given collection.
     *
     * @param values a {@link Collection} to be copied into the new hash set.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomSet(Collection<? extends K> values, float factor, HashFunction<K> strategy) {
        this(values.size(), factor, strategy);
        addAll(values);
    }

    /**
     * Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor copying a given collection.
     *
     * @param values a {@link Collection} to be copied into the new hash set.
     * @param strategy the strategy.
     */
    public CustomSet(Collection<? extends K> values, HashFunction<K> strategy) {
        this(values, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash set using elements provided by a type-specific
     * iterator.
     *
     * @param values a type-specific iterator whose elements will fill the set.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomSet(Iterator<K> values, float factor, HashFunction<K> strategy) {
        this(HashCollection.INITIAL_SIZE, factor, strategy);
        while (values.hasNext()) {
            add(values.next());
        }
    }

    /**
     * Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor using elements provided by a type-specific iterator.
     *
     * @param values a type-specific iterator whose elements will fill the set.
     * @param strategy the strategy.
     */
    public CustomSet(Iterator<K> values, HashFunction<K> strategy) {
        this(values, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash set and fills it with the elements of a given array.
     *
     * @param values an array whose elements will be used to fill the set.
     * @param offset the first element to use.
     * @param length the number of elements to use.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomSet(K[] values, int offset, int length, float factor, HashFunction<K> strategy) {
        this(length < 0 ? 0 : length, factor, strategy);
        ArrayAllocator.ensureOffsetLength(values, offset, length);
        for (int i = 0; i < length; i++) {
            add(values[ offset + i]);
        }
    }

    /**
     * Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor and fills it with the elements of a given array.
     *
     * @param values an array whose elements will be used to fill the set.
     * @param offset the first element to use.
     * @param length the number of elements to use.
     * @param strategy the strategy.
     */
    public CustomSet(K[] values, int offset, int length, HashFunction<K> strategy) {
        this(values, offset, length, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash set copying the elements of an array.
     *
     * @param values an array to be copied into the new hash set.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomSet(K[] values, float factor, HashFunction<K> strategy) {
        this(values, 0, values.length, factor, strategy);
    }

    /**
     * Creates a new hash set with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor copying the elements of an array.
     *
     * @param values an array to be copied into the new hash set.
     * @param strategy the strategy.
     */
    public CustomSet(K[] values, HashFunction<K> strategy) {
        this(values, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Returns the hashing strategy.
     *
     * @return the hashing strategy of this custom hash set.
     */
    public HashFunction<K> strategy() {
        return strategy;
    }

    @Override
    protected int hash(K key) {
        return strategy.hashCode(key);
    }

    @Override
    protected boolean compare(K key1, K key2) {
        return strategy.equals(key1, key2);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void readObjectDetails(java.io.ObjectInputStream istream) throws IOException, ClassNotFoundException {
        this.strategy = (HashFunction<K>)istream.readObject();
    }
    
    @Override
    protected void writeObjectDetails(java.io.ObjectOutputStream ostream) throws IOException {
        ostream.writeObject(this.strategy);
    }
}
