Class Lazy<T>

java.lang.Object
eu.bandm.tools.util.java.Lazy<T>
Type Parameters:
T - the type of the supplied value
All Implemented Interfaces:
Supplier<T>

public class Lazy<T> extends Object implements Supplier<T>
A lazy supplier of a single value.

A lazy supplier only supplies a single value, i.e., all subsequent calls to get() return a value identical to the first one. Any side effects of creating the returned value only occur on the first call to get(). Side effects are encapsulated by giving a initialization supplier.

This class is not inherently thread-safe. If an instance is to be shared between threads, and there is a race where multiple threads may call get(), then there is no guarantee how many times the given initialization supplier will be called, or which respective result will be stored. If unique initialization is required, add external synchronization as needed.

  • Constructor Details

    • Lazy

      public Lazy(Supplier<? extends T> init)
      Creates a new instance.

      The given initializer is stored, but not yet evaluated.

      Parameters:
      init - the initializer
      Throws:
      NullPointerException - if init is null
  • Method Details

    • get

      public T get()
      Returns the value, calling the initializer if necessary.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the value
    • isInitialized

      public boolean isInitialized()
      Checks whether the initializer has been called.
      Returns:
      true if the initializer has been called