I am looking for some magical Map
-like utility that, given a type, return the value associated with the type or its closest super-type. This is to replace statements like
if (... instanceof A) return valueA;
else if (... instanceof B) return valueB;
...
I have read the answers to Avoiding instanceof in Java, which suggested a number of patterns, in particular, the Visitor pattern. However, since the goal is to return a simple value, implementing the visitors seems to be an overkill.
Unfortunately, the new JDK class ClassValue
also don't qualify as it does not check super-types.
I just want to check if such utility exists in any well-known library before I roll my own. The implementation should be thread-safe and hopefully has les than linear cost w.r.t. the number of values inserted.