2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2021-05-01 15:53:30 +00:00
|
|
|
package edu.wpi.first.math;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2024-05-06 23:52:59 -05:00
|
|
|
import java.util.Objects;
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
/**
|
|
|
|
|
* Represents a pair of two objects.
|
|
|
|
|
*
|
|
|
|
|
* @param <A> The first object's type.
|
|
|
|
|
* @param <B> The second object's type.
|
|
|
|
|
*/
|
2020-08-14 23:40:33 -07:00
|
|
|
public class Pair<A, B> {
|
|
|
|
|
private final A m_first;
|
|
|
|
|
private final B m_second;
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
/**
|
|
|
|
|
* Constructs a pair.
|
|
|
|
|
*
|
|
|
|
|
* @param first The first object.
|
|
|
|
|
* @param second The second object.
|
|
|
|
|
*/
|
2020-08-14 23:40:33 -07:00
|
|
|
public Pair(A first, B second) {
|
|
|
|
|
m_first = first;
|
|
|
|
|
m_second = second;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the first object.
|
|
|
|
|
*
|
|
|
|
|
* @return The first object.
|
|
|
|
|
*/
|
2020-08-14 23:40:33 -07:00
|
|
|
public A getFirst() {
|
|
|
|
|
return m_first;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the second object.
|
|
|
|
|
*
|
|
|
|
|
* @return The second object.
|
|
|
|
|
*/
|
2020-08-14 23:40:33 -07:00
|
|
|
public B getSecond() {
|
|
|
|
|
return m_second;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
/**
|
|
|
|
|
* Returns a pair comprised of the two given objects.
|
|
|
|
|
*
|
|
|
|
|
* @param <A> The first object's type.
|
|
|
|
|
* @param <B> The second object's type.
|
|
|
|
|
* @param a The first object.
|
|
|
|
|
* @param b The second object.
|
|
|
|
|
* @return A pair comprised of the two given objects.
|
|
|
|
|
*/
|
2020-08-14 23:40:33 -07:00
|
|
|
public static <A, B> Pair<A, B> of(A a, B b) {
|
|
|
|
|
return new Pair<>(a, b);
|
|
|
|
|
}
|
2024-03-25 01:51:08 -05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return String.format("Pair(%s, %s)", m_first, m_second);
|
|
|
|
|
}
|
2024-05-06 23:52:59 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
2020-08-14 23:40:33 -07:00
|
|
|
}
|