12

From the javadocs:

public interface Cache<K,V> extends Function<K,V> {
    //...
    void invalidate(Object key);
    //...
}

Why is this not rendered as a generic method:

    void invalidate(K key);

Is there a technical reason, a historical reason, or some other reason?

Matt Mills
  • 8,692
  • 6
  • 40
  • 64

1 Answers1

13

For the same reason that Map.remove takes an Object argument, which is explained here and here.

This reason is neither technical nor historical: it's just...an objectively sensible reason.

Community
  • 1
  • 1
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • But why doesn't Cache have a `get(Object key)` method? :) – John Vint Feb 27 '12 at 04:15
  • 5
    Because `Cache.get` and `LoadingCache.getUnchecked` can each cause entries to be _added_ to the cache, so it has to be of type `K` for the same reason that `Map.put` has to take a key of type `K`. Admittedly, `getIfPresent` doesn't take an `Object` -- it forces its argument to be of type `K` -- and that was probably a judgement call. (But `asMap().get` still takes an `Object`, and it's functionally equivalent to `getIfPresent`.) – Louis Wasserman Feb 27 '12 at 04:17
  • Yea it does seem odd they conformed to the `Map` interface in some spots and not others though. – John Vint Feb 27 '12 at 04:18
  • 1
    I'm not positive about the justification for `getIfPresent`, but I do know for a fact that these APIs were discussed and debated for _days_. – Louis Wasserman Feb 27 '12 at 04:20
  • 2
    getIfPresent(K) may well have been an accident for all I remember. – Kevin Bourrillion Feb 27 '12 at 07:55