let's play TDD, tackles closures in java

Artur Biesiadowski abies at adres.pl
Sat Dec 18 14:17:58 PST 2010

On 16/12/2010 08:30, Florian Weimer wrote:
> We can turn this into:
> final BooleanReference fired = new BooleanReference(false);
> model.addTableModelListener(#{e ->  fired.setValue(true)});
> model.doThing();
> assertTrue(fired.getValue());

You can as well use AtomicBoolean, which has an extra benefit of having 
defined behaviour in multithreaded case.

I suppose that 95% of the cases of 'ugliness' people are rising could be 
solve by adding kind of autoboxing for Atomic* classes.

AtomicBoolean fired = false;

model.addTableModelListener(#{e ->  fired = true;});


assertTrue(fired.get()); // or assertTrue((boolean)fired); or even assertTrue(fired) as assertTrue is expecting boolean only, not object ref

With mapping of AtomicInteger.addAndGet to +=, ++,-- to 
increment/decrementAndGet, you will even get multithreaded sum thing 
working out of the box.

While I'm not big fan of another set of magic dependencies (after String 
contatenation and java.lang.Integer/etc autoboxing), I still find it a 
lot cleaner that most of the solutions for transparent shared variable 

Artur Biesiadowski

More information about the lambda-dev mailing list