Class Singleton<T>

java.lang.Object
dev.enola.common.context.Singleton<T>
All Implemented Interfaces:
Supplier<T>

public abstract class Singleton<T> extends Object implements Supplier<T>
Singleton.

Singletons statically hold a value for the lifetime of the JVM.

This is great for "global" services; some initialization code called at the start of an application sets its value, which can then be easily looked up from anywhere in an application. It is not possible to later change or reset that value - this is an intentional design decision.

For things which are e.g. user- or request-dependent, just use TLC instead.

In tests, use this together with the JUnit SingletonRule. See also SingleTest for how to use this; TL;DR is: class MyService { public static final Singleton<MyService> SINGLETON = new Singleton<>() {};. For configurable services used in several tests, it's convenient to define something like: public static Singleton<MyService> set(...) { return SINGLETON.set(new MyService(...));

This class is intentionally not thread safe, for performance reasons.