io.deephaven.hash.KeyedObjectKey Maven / Gradle / Ivy
The newest version!
/*
Copyright (C) 2021 Deephaven Data Labs (https://deephaven.io).
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU Lesser General Public License as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
package io.deephaven.hash;
import java.util.Objects;
import java.util.function.Function;
/** This interface adapts a class for use with a KeyedObjectHash. */
@SuppressWarnings("unused")
public interface KeyedObjectKey {
/**
* Returns the key of an object.
*
* @param v the object
* @return its key
*/
K getKey(V v);
/**
* Returns the hash code of a key.
*
* @param k the key
* @return the hash code of the object's key
*/
int hashKey(K k);
/**
* Compare a key against the key of an object.
*
* @param k the key
* @param v the object
* @return true, if the given key is equal to the object's key
*/
boolean equalKey(K k, V v);
abstract class Basic implements KeyedObjectKey {
@Override
public boolean equalKey(final K k, final V v) {
return k.equals(getKey(v));
}
@Override
public int hashKey(final K k) {
return k.hashCode();
}
}
class BasicAdapter extends Basic {
private final Function keyFunction;
public BasicAdapter(final Function keyFunction) {
this.keyFunction = Objects.requireNonNull(keyFunction, "keyFunction");
}
@Override
public K getKey(final V v) {
return keyFunction.apply(v);
}
}
abstract class NullSafeBasic implements KeyedObjectKey {
@Override
public boolean equalKey(final K k, final V v) {
return k == null ? getKey(v) == null : k.equals(getKey(v));
}
@Override
public int hashKey(final K k) {
return k == null ? 0 : k.hashCode();
}
}
class NullSafeBasicAdapter extends NullSafeBasic {
private final Function keyFunction;
public NullSafeBasicAdapter(final Function keyFunction) {
this.keyFunction = Objects.requireNonNull(keyFunction, "keyFunction");
}
@Override
public K getKey(final V v) {
return keyFunction.apply(v);
}
}
abstract class Exact implements KeyedObjectKey {
@Override
public boolean equalKey(final K k, final V v) {
return k == getKey(v);
}
@Override
public int hashKey(final K k) {
return k.hashCode();
}
}
class ExactAdapter extends Exact {
private final Function keyFunction;
public ExactAdapter(final Function keyFunction) {
this.keyFunction = Objects.requireNonNull(keyFunction, "keyFunction");
}
@Override
public K getKey(final V v) {
return keyFunction.apply(v);
}
}
abstract class NullSafeExact implements KeyedObjectKey {
@Override
public boolean equalKey(final K k, final V v) {
return k == getKey(v);
}
@Override
public int hashKey(final K k) {
return k == null ? 0 : k.hashCode();
}
}
class NullSafeExactAdapter extends NullSafeExact {
private final Function keyFunction;
public NullSafeExactAdapter(final Function keyFunction) {
this.keyFunction = Objects.requireNonNull(keyFunction, "keyFunction");
}
@Override
public K getKey(final V v) {
return keyFunction.apply(v);
}
}
}