All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.ots.utils.Pair Maven / Gradle / Ivy

Go to download

Aliyun Open Services SDK for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

There is a newer version: 2.2.4
Show newest version
package com.aliyun.openservices.ots.utils;

import java.io.Serializable;

/**
 * A generic class for pairs.
 * @param 
 * @param 
 */
public class Pair implements Serializable
{
    protected T1 first = null;
    protected T2 second = null;

    /**
     * Default constructor.
     */
    public Pair()
    {
    }

    /**
     * Constructor
     * @param a operand
     * @param b operand
     */
    public Pair(T1 a, T2 b)
    {
        this.first = a;
        this.second = b;
    }

    /**
     * Constructs a new pair, inferring the type via the passed arguments
     * @param  type for first
     * @param  type for second
     * @param a first element
     * @param b second element
     * @return a new pair containing the passed arguments
     */
    public static  Pair newPair(T1 a, T2 b) {
        return new Pair(a, b);
    }

    /**
     * Replace the first element of the pair.
     * @param a operand
     */
    public void setFirst(T1 a)
    {
        this.first = a;
    }

    /**
     * Replace the second element of the pair.
     * @param b operand
     */
    public void setSecond(T2 b)
    {
        this.second = b;
    }

    /**
     * Return the first element stored in the pair.
     * @return T1
     */
    public T1 getFirst()
    {
        return first;
    }

    /**
     * Return the second element stored in the pair.
     * @return T2
     */
    public T2 getSecond()
    {
        return second;
    }

    private static boolean equals(Object x, Object y)
    {
        return (x == null && y == null) || (x != null && x.equals(y));
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(Object other)
    {
        return other instanceof Pair && equals(first, ((Pair)other).first) &&
                equals(second, ((Pair)other).second);
    }

    @Override
    public int hashCode()
    {
        if (first == null)
            return (second == null) ? 0 : second.hashCode() + 1;
        else if (second == null)
            return first.hashCode() + 2;
        else
            return first.hashCode() * 17 + second.hashCode();
    }

    @Override
    public String toString()
    {
        return "{" + getFirst() + "," + getSecond() + "}";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy