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

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;
import net.ranides.assira.generic.ValueUtils;

/**
 * Mutable implementation of {@link Entry} interface.
 * @author ranides
 * @param <K>
 * @param <V>
 */
public class Pair<K,V> implements Entry<K, V>, Serializable {

    private static final long serialVersionUID = 2L;

    private final K key;
	private final V value;

    /**
     * Create new pair with passed values.
     * @param key
     * @param value
     */
    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    /**
     * Static factory method - create new pair with passed values.
     * @param <SK>
     * @param <SV>
     * @param key
     * @param value
     * @return
     */
    public static <SK,SV> Pair<SK,SV> make(SK key, SV value) {
        return new Pair<>(key, value);
    }

    @Override
    public K getKey() {
        return key;
    }

    @Override
    public V getValue() {
        return value;
    }

    @Override
    public V setValue(V value) {
        throw new UnsupportedOperationException("Immutable pair.");
    }

    @Override
    public int hashCode() {
         return (key   == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
    }

    @Override
    public boolean equals(Object object) {
        if (object instanceof Entry) {
            Map.Entry other = (Map.Entry)object;
            return ValueUtils.equals(other.getKey(), key) && ValueUtils.equals(other.getValue(), value);
        }
	    return false;
    }

    @Override
    public String toString() {
        return key + "=" + value;
    }



}
