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

import java.util.Collection;
import net.ranides.assira.collection.IntComparator;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;

/**
 * A type-specific red-black tree map implementation for primitive int keys.
 * 
 * <P>The iterators can be safely cast end a {@linkplain java.util.ListIterator list iterator}.
 */
@SuppressWarnings({
    "PMD.AvoidReassigningParameters"
})
public final class IntRBTreeMultiMap<V> extends AIntRBTreeMap<V> implements IntMultiMap<V>  {

    private static final long serialVersionUID = 2L;
    
    /**
     * Creates a new empty tree map.
     */
    public IntRBTreeMultiMap() {
        this((IntComparator)null);
    }

    /**
     * Creates a new empty tree map with the given comparator.
     *
     * @param comparator a (possibly type-specific) comparator.
     */
    public IntRBTreeMultiMap(Comparator<? super Integer> comparator) {
        super(comparator, false);
    }

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a {@link Map} to be copied into the new tree map.
     */
    public IntRBTreeMultiMap(Map<? extends Integer, ? extends V> values) {
        this();
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given sorted map (and its
     * {@link Comparator}).
     *
     * @param values a {@link SortedMap} to be copied into the new tree map.
     */
    public IntRBTreeMultiMap(SortedMap<Integer, V> values) {
        this(values.comparator());
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given map.
     *
     * @param values a type-specific map to be copied into the new tree map.
     */
    public IntRBTreeMultiMap(IntMap<? extends V> values) {
        this();
        putAll(values);
    }

    /**
     * Creates a new tree map copying a given sorted map (and its
     * {@link Comparator}).
     *
     * @param values a type-specific sorted map to be copied into the new tree map.
     */
    public IntRBTreeMultiMap(IntSortedMap<V> values) {
        this(values.comparator());
        putAll(values);
    }

    /**
     * Creates a new tree map using the elements of two parallel arrays and the
     * given comparator.
     *
     * @param keyd the array of mkeys of the new tree map.
     * @param values the array of corresponding mvalues in the new tree map.
     * @param comparator a (possibly type-specific) comparator.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public IntRBTreeMultiMap(int[] keyd, V values[], Comparator<? super Integer> comparator) {
        this(comparator);
        if (keyd.length != values.length) {
            throw new IllegalArgumentException("The key array and the value array have different lengths (" + keyd.length + " and " + values.length + ")");
        }
        for (int i = 0; i < keyd.length; i++) {
            this.put(keyd[ i], values[ i]);
        }
    }

    /**
     * Creates a new tree map using the elements of two parallel arrays.
     *
     * @param keys the array of mkeys of the new tree map.
     * @param values the array of corresponding mvalues in the new tree map.
     * @throws IllegalArgumentException if <code>k</code> and <code>v</code>
     * have different lengths.
     */
    public IntRBTreeMultiMap(int[] keys, V[] values) {
        this(keys, values, null);
    }

    @Override
    public Collection<V> getAll(int key) {
        return subMap(key, key+1).values();
    }

}
