// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. package edu.wpi.first.math; import java.util.Objects; /** * Represents a pair of two objects. * * @param The first object's type. * @param The second object's type. */ public class Pair { private final A m_first; private final B m_second; /** * Constructs a pair. * * @param first The first object. * @param second The second object. */ public Pair(A first, B second) { m_first = first; m_second = second; } /** * Returns the first object. * * @return The first object. */ public A getFirst() { return m_first; } /** * Returns the second object. * * @return The second object. */ public B getSecond() { return m_second; } /** * Returns a pair comprised of the two given objects. * * @param The first object's type. * @param The second object's type. * @param a The first object. * @param b The second object. * @return A pair comprised of the two given objects. */ public static Pair of(A a, B b) { return new Pair<>(a, b); } @Override public String toString() { return String.format("Pair(%s, %s)", m_first, m_second); } /** * Checks equality between this Pair and another object. * * @param obj The other object. * @return Whether the two objects are equal or not. */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Pair) { return Objects.equals(m_first, ((Pair) obj).getFirst()) && Objects.equals(m_second, ((Pair) obj).getSecond()); } return false; } @Override public int hashCode() { return Objects.hash(m_first, m_second); } }