All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.rouz.task.Singleton Maven / Gradle / Ivy

The newest version!
package io.rouz.task;

import java.util.Objects;

import io.rouz.task.dsl.TaskBuilder.F0;

/**
 * A singleton supplier decorator.
 *
 * Ensures that the {@link #get()} method of the wrapped {@link F0} only is called once.
 */
class Singleton implements F0 {

  private final F0 supplier;
  private volatile T value;

  private Singleton(F0 supplier) {
    this.supplier = Objects.requireNonNull(supplier);
  }

  static  F0 create(F0 fn) {
    return new Singleton<>(fn);
  }

  @Override
  public T get() {
    if (value == null) {
      synchronized (this) {
        if (value == null) {
          value = supplier.get();
        }
      }
    }
    return value;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy