#2718 ConcurrentMap.getOrAdd not the same signature as Map.getOrAdd

Andrey Zakharov Thu 8 Nov 2018

Map:

V getOrAdd(K key, |K->V| valFunc)

ConcurrentMap

Obj getOrAdd(Obj key, Obj defVal)

without lambda

Is it because specific concurrent nature ConcurrentMap ?

SlimerDude Thu 8 Nov 2018

I'm guessing it's because, as the docs say, Fantom's ConcurrentMap is just a wrapper for Java's ConcurrentHashMap so the Fantom getOrAdd() method will undoubtedly just map to Java's putIfAbsent() method as that Java class (from Java 1.5) doesn't understand lambdas or closures.

If you need it, SynchronizedMap from the afConcurrent library is const, concurrent, and does have a getOrAdd() method that takes a closure.

Note I've created a few Map like objects over the years and often thought it'd be useful if there was Map mixin so you could easily inherit / override the usual Iterator methods such as each() / find() / findAll() / etc....

brian Fri 16 Nov 2018

Correct - its because it routes to the Java method putIfAbsent:

public Object getOrAdd(Object key, Object defVal)
{
  Object val = map.putIfAbsent(key, defVal);
  return val == null ? defVal : val;
}

Login or Signup to reply.