Run code only once!

Project hosted on github: https://github.com/claudemartin/LockOnce

It’s just one single utility class to make sure code is run only once. And then there’s another one for easy “Lazy Initialization”.

I once wrote this for Java 7 but now it needs Java 8. It makes much more sense to use this with Lambdas. It’s a bit like the lazy keyword of Scala.

// declaration of lazy field:
final Supplier<Foo> lazy = Lazy.of(() -> ...);
// Access to the data:     
Foo foo = lazy.get();

You can also register a “destructor”. This solves the problem that such fields (they are usually static final) are never GCd and finalizers are utterly useless. In the same way you can have resources (they just have to implement AutoClose) closed on shutdown.

Misnomer: List Interface

Some think “List” is a misnomer. I don’t even mind so much. I think List is ok.

I just put this here because there already is an entry in the FAQ:

Why don’t you rename the List interface to Sequence; doesn’t “list” generally suggest “linked list”? Also, doesn’t it conflict with java.awt.List?

PS: Vector is strange, though. It is synchronized and it really should be SynchronizedList. But it is defacto deprecated. So who cares?