common-utils/src/main/java/org/warp/commonutils/type/ScoredValue.java

54 lines
1.1 KiB
Java

package org.warp.commonutils.type;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
public final class ScoredValue<T> implements Comparable<ScoredValue<T>> {
private final float score;
private final T value;
private ScoredValue(float score, T value) {
this.score = score;
this.value = value;
}
public static <T> ScoredValue<T> of(float score, T value) {
return new ScoredValue<T>(score, value);
}
@Override
public int compareTo(@NotNull ScoredValue<T> o) {
// This is reversed
return Float.compare(o.score, this.score);
}
public float getScore() {
return this.score;
}
public T getValue() {
return this.value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ScoredValue<?> that = (ScoredValue<?>) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
public String toString() {
return "ScoredValue(score=" + this.getScore() + ", value=" + this.getValue() + ")";
}
}