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

import java.io.IOException;
import net.ranides.assira.generic.HashUtils;
import java.util.Map;
import net.ranides.assira.collection.utils.HashCollection;
import net.ranides.assira.collection.HashFunction;

/**
 * A type-specific hash map with primitive int values, whose
 * {@link HashFunction hashing strategy} is specified at creation time.
 */
public final class CustomLookup<K> extends AHashLookup<K> {

    private static final long serialVersionUID = 2L;
    
    protected transient HashFunction<K> strategy;

    /**
     * Creates a new hash map.
     *
     * <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 CustomLookup(int expected, float factor, HashFunction<K> strategy) {
        super(expected, factor);
        this.strategy = strategy;
    }

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

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

    /**
     * Creates a new hash map copying a given one.
     *
     * @param values a {@link Map} to be copied into the new hash map.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomLookup(Map<? extends K, ? extends Integer> values, float factor, HashFunction<K> strategy) {
        this(values.size(), factor, strategy);
        putAll(values);
    }

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

    /**
     * Creates a new hash map copying a given type-specific one.
     *
     * @param values a type-specific map to be copied into the new hash map.
     * @param factor the load factor.
     * @param strategy the strategy.
     */
    public CustomLookup(ALookup<K> values, float factor, HashFunction<K> strategy) {
        this(values.size(), factor, strategy);
        putAll(values);
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor copying a given type-specific one.
     *
     * @param values a type-specific map to be copied into the new hash map.
     * @param strategy the strategy.
     */
    public CustomLookup(ALookup<K> values, HashFunction<K> strategy) {
        this(values, HashCollection.LOAD_FACTOR, strategy);
    }

    /**
     * Creates a new hash map using the elements of two parallel arrays.
     *
     * @param keys the array of keys of the new hash map.
     * @param values the array of corresponding values in the new hash map.
     * @param factor the load factor.
     * @param strategy the strategy.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public CustomLookup(K[] keys, int values[], float factor, HashFunction<K> strategy) {
        this(keys.length, factor, strategy);
        if (keys.length != values.length) {
            throw new IllegalArgumentException("The key array and the value array have different lengths (" + keys.length + " and " + values.length + ")");
        }
        for (int i = 0; i < keys.length; i++) {
            this.put(keys[ i], values[ i]);
        }
    }

    /**
     * Creates a new hash map with {@link Hash#DEFAULT_LOAD_FACTOR} as load
     * factor using the elements of two parallel arrays.
     *
     * @param keys the array of keys of the new hash map.
     * @param values the array of corresponding values in the new hash map.
     * @param strategy the strategy.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public CustomLookup(K[] keys, int values[], HashFunction<K> strategy) {
        this(keys, values, HashCollection.LOAD_FACTOR, strategy);
    }

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

    @Override
    protected int hash(K key) {
        try {
            return HashUtils.murmurHash3(strategy.hashCode(key));
        } catch(ClassCastException ex) {
            // "unchecked" generic methods could pass something strange here
            return 0;
        }
    }

    @Override
    protected boolean compare(K key1, K key2) {
        try {
            return strategy.equals(key1, key2);
        } catch(ClassCastException ex) {
            // "unchecked" generic methods could pass something strange here
            return false;
        }
    }
    
    @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);
    }

}
