![JAR search and dependency download from the Maven repository](/logo.png)
net.dongliu.dbutils.Pair Maven / Gradle / Ivy
package net.dongliu.dbutils;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
/**
* Immutable pair, the key and value cannot be null
*
* @author Liu Dong
*/
public class Pair implements Map.Entry, Serializable {
private static final long serialVersionUID = -6525353427059094141L;
private final A first;
private final B second;
private Pair(A first, B second) {
this.first = Objects.requireNonNull(first);
this.second = Objects.requireNonNull(second);
}
public static Pair of(K key, V value) {
return new Pair<>(key, value);
}
public A first() {
return first;
}
public B second() {
return second;
}
public A getKey() {
return first;
}
/**
* Create a new pair with new key
*/
public Pair withKey(A newKey) {
return new Pair<>(newKey, second);
}
public A getName() {
return first;
}
/**
* Create a new pair with new key
*/
public Pair withName(A newKey) {
return new Pair<>(newKey, second);
}
public B getValue() {
return second;
}
@Override
public B setValue(B value) {
throw new UnsupportedOperationException("Pair is read only");
}
/**
* Create a new pair with new value
*/
public Pair withValue(B newValue) {
return new Pair<>(first, newValue);
}
/**
* To String pair
*/
@SuppressWarnings("unchecked")
public Pair toStringPair() {
if ((first instanceof String) && (second instanceof String)) {
return (Pair) this;
}
return Pair.of(first.toString(), second.toString());
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair, ?> pair = (Pair, ?>) o;
if (!first.equals(pair.first)) return false;
return second.equals(pair.second);
}
@Override
public int hashCode() {
int result = first.hashCode();
result = 31 * result + second.hashCode();
return result;
}
@Override
public String toString() {
return "(" + first + "," + second + ")";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy