/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.generic;

import java.io.Serializable;
import java.util.Arrays;
import net.ranides.assira.collection.arrays.ArrayUtils;

/**
 * This class represents immutable tuple, which can be used as key in HashMap.
 * It uses "equals" to compare values:
 * tuples are equal only if each corresponding object inside two tuples are equal.
 *
 * Because of that, it is preffered to store immutable objects inside tuple.
 */
public final class Tuple implements Comparable<Tuple>, Serializable {

    private static final long serialVersionUID = 1L;

    private final Object[] values;

    /**
     * Creates new tuple.
     *
     * @param values values
     */
    public Tuple(Object... values) {
        this.values = ArrayUtils.copy(values);
    }

    @Override
    public boolean equals(Object value) {
        return this == value || ((value!=null && Tuple.class.equals(value.getClass())) && Arrays.equals(values, ((Tuple)value).values));
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(values);
    }

    @Override
    public int compareTo(Tuple that) {
        return ArrayUtils.compare(this.values, that.values);
    }

}
