Is there in Java 8 any analogue for implements
keyword for methods?
Let's say I have a functional interface:
@FunctionalInterface
interface LongHasher {
int hash(long x);
}
And a library of 3 static methods "implementing" this functional interface:
class LongHashes {
static int xorHash(long x) {
return (int)(x ^ (x >>> 32));
}
static int continuingHash(long x) {
return (int)(x + (x >>> 32));
}
static int randomHash(long x) {
return xorHash(x * 0x5DEECE66DL + 0xBL);
}
}
In the future I want to be able to interchangeably use any of references to these 3 methods as a parameter. For example:
static LongHashMap createHashMap(LongHasher hasher) { ... }
...
public static void main(String[] args) {
LongHashMap map = createHashMap(LongHashes::randomHash);
...
}
How can I ensure at compile time that LongHashes::xorHash
, LongHashes::continuingHash
and LongHashes::randomHash
have the same signature as LongHasher.hash(long x)
?