Pair--代替多键值对的尴尬局面

来源:互联网 发布:旺材 知乎 编辑:程序博客网 时间:2024/05/04 01:01
/** * Pair of values. * 这段代码可以用来代替多键值对的尴尬局面,比如Pair<L, M, S, R> */public class Pair <L, R>    implements Comparable<Pair<L, R>>, Map.Entry<L, R>{    public L left;    public R right;    /**     * Creates a pair.     *     * @param left Left value     * @param right Right value     */    public Pair(L left, R right) {        this.left = left;        this.right = right;    }    /**     * Creates a pair representing the same mapping as the     * specified entry.     *     * @param entry the entry to copy     */    public Pair(Map.Entry<? extends L, ? extends R> entry) {        this.left = entry.getKey();        this.right = entry.getValue();    }    /**     * Creates a Pair.     *     * @param left Left value     * @param right Right value     * @return a new Pair     */    public static <L, R> Pair<L, R> of(L left, R right) {        return new Pair<L, R>(left, right);    }    public boolean equals(Object obj) {        if (obj instanceof Pair) {            //noinspection unchecked            Pair<L, R> pair = (Pair) obj;            return Util.equals(this.left, pair.left)                && Util.equals(this.right, pair.right);        }        return false;    }    public int hashCode() {        int k = (left == null) ? 0 : left.hashCode();        int k1 = (right == null) ? 0 : right.hashCode();        return ((k << 4) | k) ^ k1;    }    public int compareTo(Pair<L, R> that) {        int c = compare((Comparable) this.left, (Comparable)that.left);        if (c == 0) {            c = compare((Comparable) this.right, (Comparable)that.right);        }        return c;    }    public String toString() {        return "<" + left + ", " + right + ">";    }    // implement Map.Entry    public L getKey() {        return left;    }    // implement Map.Entry    public R getValue() {        return right;    }    // implement Map.Entry    public R setValue(R value) {        R previous = right;        right = value;        return previous;    }    /**     * Compares a pair of comparable values of the same type. Null collates     * less than everything else, but equal to itself.     *     * @param c1 First value     * @param c2 Second value     * @return  a negative integer, zero, or a positive integer if c1     *          is less than, equal to, or greater than c2.     */    private static <C extends Comparable<C>> int compare(C c1, C c2) {        if (c1 == null) {            if (c2 == null) {                return 0;            } else {                return -1;            }        } else if (c2 == null) {            return 1;        } else {            return c1.compareTo(c2);        }    }}

原创粉丝点击