title: Inheritance-aware class to value map to replace series of `instanceof`
body: <p>I am looking for some magical <code>Map</code>-like utility that, given a type, return the value associated with the type or its closest super-type. This is to replace statements like</p>
  
  <pre><code>if (... instanceof A) return valueA;
  else if (... instanceof B) return valueB;
  ...
  </code></pre>
  
  <p>I have read the answers to <a href="https://stackoverflow.com/q/2790144/499922">Avoiding instanceof in Java</a>, 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.</p>
  
  <p>Unfortunately, the new JDK class <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/ClassValue.html" rel="nofollow noreferrer"><code>ClassValue</code></a> also don't qualify as it does not check super-types.</p>
  
  <p>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.</p>