private static <KEY, VALUE, C extends Collection<VALUE>> void addToMapValueCollection(
      ConcurrentMap<KEY, C> map, KEY key, VALUE value,
      CollectionFactory<VALUE, C> factory) {

    while (true) {

      C values = map.get(key);

      if (values == null) {
        C newKeys = factory.create(value);
        values = map.putIfAbsent(key, newKeys);
        if (values == null)
          return;
      }

      C origCopy = factory.copy(values);

      if (origCopy.contains(value))
        return;

      C extendedCopy = factory.copy(origCopy);
      extendedCopy.add(value);

      if (map.replace(key, origCopy, extendedCopy))
        return;
    }
  }