himanshudixit / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Allow null values in MapMaker #166

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is not a defect but the "template" options did not have any way to
report an enhancement request.

It would be nice if the MapMaker allowed null values.  We use the
MapMaker#computingMap as a cheap way to cache expensive lookups.  Sometimes
that lookup will return null and the Map throws a NullPointerException

Thanks

Original issue reported on code.google.com by kevin.a....@gmail.com on 13 May 2009 at 6:00

GoogleCodeExporter commented 9 years ago
ConcurrentMaps generally forbid null so that callers can distinguish between 
"key not
present" and "key maps to null value" atomically.  This is true of 
ConcurrentHashMap,
for example, which MapMaker uses for simple maps.

You're probably best served by a trivial wrapper class along these lines:

class Maybe<T> {
  boolean hasValue();
  T getValue();
}

Then you can have a MapMaker<K, Maybe<V>>.  It should perform as well as an 
internal
implementation would at the cost of a more complex interface.  You can probably
implement a Map<K, V> wrapper to fix the latter problem.

Original comment by cpov...@google.com on 13 May 2009 at 6:20

GoogleCodeExporter commented 9 years ago
Thanks for the tip.

Original comment by kevin.a....@gmail.com on 13 May 2009 at 6:25