Interface Singleton<T>

Type Parameters:
T - the type of the returned value
All Superinterfaces:
Supplier<T>
All Known Implementing Classes:
Lazy
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Singleton<T> extends Supplier<T>
A functional interface with an operation that always returns the same value.

If a function object that implements this interface is used sequentially, the returned values must be identical according to ==. If computation of that value has observable side effects, they should occur only on the first call.

Implementations of this interface are not required to be inherently thread-safe. An object that is used concurrently by multiple threads may return different values, even in the same thread. However, these values must be equivalent according to Objects.equals(Object,Object).

The purpose of this interface is to give an abstract type for reified instances of the singleton design pattern.

Since:
1.4
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Singleton<T>
    constant(@Opt T value)
    Returns a function object that always returns the given value.
    get()
    Returns a fixed value.
  • Method Details

    • get

      @Opt T get()
      Returns a fixed value.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the fixed value
    • constant

      static <T> Singleton<T> constant(@Opt T value)
      Returns a function object that always returns the given value.
      Type Parameters:
      T - the type of the returned value
      Parameters:
      value - the value
      Returns:
      always value