io.contek.zeus.StringKey Maven / Gradle / Ivy
package io.contek.zeus;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import static java.nio.charset.StandardCharsets.UTF_8;
@Immutable
public abstract class StringKey {
private static final HashFunction HASH_FUNCTION = Hashing.murmur3_128();
private final String stringValue;
private int hashCode;
protected StringKey(String stringValue) {
this.stringValue = stringValue;
}
public final String getStringValue() {
return stringValue;
}
@Override
public final boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StringKey that = (StringKey) o;
return stringValue.equals(that.stringValue);
}
@Override
public final int hashCode() {
if (hashCode == 0) {
hashCode = HASH_FUNCTION.hashString(stringValue, UTF_8).asInt();
}
return hashCode;
}
@Override
public final String toString() {
return stringValue;
}
}