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

org.voltcore.utils.Pair Maven / Gradle / Ivy

There is a newer version: 10.1.1
Show newest version
/* This file is part of VoltDB.
 * Copyright (C) 2008-2020 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see .
 */

package org.voltcore.utils;

import java.util.Objects;

/**
 * Class representing a pair of generic-ized types. Supports equality, hashing
 * and all that other nice Java stuff. Based on STL's pair class in C++.
 *
 */
public class Pair implements java.io.Serializable {
    private static final long serialVersionUID = 2643511682081674630L;

    protected final T m_first;
    protected final U m_second;
    protected transient final int m_hash;

    public Pair(T first, U second, final boolean hash) {
        m_first = first;
        m_second = second;
        if (hash) {
            m_hash = (first == null ? 0 : first.hashCode() * 31)
                    + (second == null ? 0 : second.hashCode());
        } else {
            m_hash = 0;
        }
    }

    public Pair(T first, U second) {
        this(first, second, true);
    }

    @Override
    public String toString() {
        return "<" + m_first + ", " + m_second + ">";
    }

    @Override
    public int hashCode() {
        return m_hash;
    }

    public Object get(int idx) {
        if (idx == 0) {
            return m_first;
        } else if (idx == 1) {
            return m_second;
        }
        return null;
    }

    /**
     * @param o Object to compare to.
     * @return Is the object equal to a value in the pair.
     */
    public boolean contains(Object o) {
        return Objects.equals(o, m_first) || Objects.equals(o, m_second);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || !(getClass().isInstance(o))) {
            return false;
        }

        @SuppressWarnings("unchecked")
        Pair other = (Pair) o;

        return (m_first == null ? other.m_first == null : m_first.equals(other.m_first)) &&
               (m_second == null ? other.m_second == null : m_second.equals(other.m_second));
    }

    /**
     * @return the first
     */
    public T getFirst() {
        return m_first;
    }

    /**
     * @return the second
     */
    public U getSecond() {
        return m_second;
    }

    /**
     * Convenience class method for constructing pairs using Java's generic type
     * inference.
     */
    public static  Pair of(T x, U y) {
        return new Pair(x, y);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy