public V get(K key) {
    final WeakReference<V> currentWeakRef = lookup(key);
    // resolve it, after which if not null, we have a strong reference
    V strongVal = resolve(currentWeakRef);
    if (strongVal != null) {
      // all good.
      return  strongVal;
    }

    // here, either currentWeakRef was null, or its reference was GC'd.
    if (currentWeakRef != null) {
      // garbage collection removed the reference.

      // explicitly remove the weak ref from the map if it has not
      // been updated by this point
      // this is here just for completeness.
      map.remove(key, currentWeakRef);

      // log/report the loss.
      noteLost(key);
    }

    // create a new value and add it to the map
    return create(key);
  }
@Test
  public void testFactoryReturningNull() throws Throwable {
    referenceMap = new WeakReferenceMap<>(
        (k) -> null,
            null);
    intercept(NullPointerException.class, () ->
        referenceMap.get(0));
  }