51 lines
1.5 KiB
Java
51 lines
1.5 KiB
Java
package com.displaynone.acss.utils;
|
|
|
|
import lombok.Getter;
|
|
import org.springframework.lang.NonNull;
|
|
import org.springframework.lang.Nullable;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
import java.util.Map;
|
|
import java.util.stream.Collector;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Getter
|
|
public final class Pair<S, T> {
|
|
private final S first;
|
|
private final T second;
|
|
|
|
private Pair(@NonNull S first, @NonNull T second) {
|
|
Assert.notNull(first, "First must not be null");
|
|
Assert.notNull(second, "Second must not be null");
|
|
|
|
this.first = first;
|
|
this.second = second;
|
|
}
|
|
|
|
public static <S, T> Pair<S, T> of(@NonNull S first, @NonNull T second) {
|
|
return new Pair<>(first, second);
|
|
}
|
|
|
|
public static <S, T> Collector<Pair<S, T>, ?, Map<S, T>> toMap() {
|
|
return Collectors.toMap(Pair::getFirst, Pair::getSecond);
|
|
}
|
|
|
|
public boolean equals(@Nullable Object object) {
|
|
if (this == object) return true;
|
|
if (!(object instanceof Pair<?, ?> pair)) return false;
|
|
|
|
return ObjectUtils.nullSafeEquals(this.first, pair.first) && ObjectUtils.nullSafeEquals(this.second, pair.second);
|
|
}
|
|
|
|
public int hashCode() {
|
|
int result = ObjectUtils.nullSafeHashCode(this.first);
|
|
result = 31 * result + ObjectUtils.nullSafeHashCode(this.second);
|
|
return result;
|
|
}
|
|
|
|
public String toString() {
|
|
return String.format("%s->%s", this.first, this.second);
|
|
}
|
|
}
|