Functional Programming in Java, Second Edition: p 125: Going all the way with createCacheAndHeavy()

The example on page 125 comes closest to the mythical “self-modifying code” (a completely useless concept as modifying the data structure that the code works on is much easier, as seen here, but I digress!)

We can go one step further it seems and replace the anonymous class in createAndCacheHeavy() with another lambda (truly a closure that wraps around the Heavy instance) that matches Supplier<Lambda>. As we have no immediate way of testing the actual type of of the synthesized class that underpins a lambda (I think), we need to add a boolean to decide on whether we already did the replacement or not. This boolean does not need to be volatile or an AtomicBoolean as it is accessed from with synchronized code by the few earliest threads only. I hope I’m right about this.

Here we go:

class Holder {

    private Supplier<Heavy> heavy = () -> createAndCacheHeavy();
    private boolean firstCall = true;

    public Holder() {
        System.out.println("Holder created");
    }

    public Heavy getHeavy() {
        return heavy.get();
    }

    // We replace the supplier in the form of a lambda creating the Heavy instance
    // with another lambda that just returns the firstly created instance

    private synchronized Heavy createAndCacheHeavy() {
        if (firstCall) {
            System.out.println("First call to createAndCacheHeavy()");
            Heavy heavyInstance = new Heavy();
            heavy = () -> heavyInstance;
            firstCall = false;
        }
        else {
            System.out.println("Just another call to createAndCacheHeavy()");
            // Only happens if there was another thread that was queued on the
            // initial "Supplier" while the first thread performed the action
            // in the "firstCall" branch above. That initial "Supplier" has
            // already been replaced by the "Supplier" just returning
            // "heavyInstance". As this is a synchronized call, said "Supplier"
            // should be visible to this thread, so heavy.get() will do
            // what we expect.
        }
        return heavy.get();
    }
}