Lazy initialization helper.
dependencies {
implementation "dev.rikka.rikkax.lazy:lazy:<version>"
}
androidx.annotation:annotation
The purpose library is to use Kotlin-like Lazy in Java only projects/libraries. The design and internal implementation is close to Kotlin Lazy.
If your are already using Kotlin, you don't need to use this library.
This library has a rikka.lazy.Lazy
interface and all implementations are its subclass.
There two implementation, rikka.lazy.SynchronizedLazy
(thread-safe version using locks inside) and rikka.lazy.UnsafeLazy
(no thread-safe version).
// Create a UnsafeLazy instance
Lazy<Object> lazy = new UnsafeLazy<>(() -> new Object());
// Get the value
Object value = lazy.get();
// Check if the value is initialized
lazy.isInitialized();
// Create a SynchronizedLazy instance
Lazy<Object> lazy = new SynchronizedLazy<>(() -> new Object());
// Create a SynchronizedLazy instance with a custom lock
Object lock = new Object();
Lazy<Object> lazy = new SynchronizedLazy<>(() -> new Object(), lock);