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

edu.stanford.nlp.util.Lazy Maven / Gradle / Ivy

package edu.stanford.nlp.util;

import java.util.function.Supplier;

/**
 * An instantiation of a lazy object.
 *
 * @author Gabor Angeli
 */
public abstract class Lazy {
  private E implOrNull = null;

  public synchronized E get() {
    if (implOrNull == null) {
      implOrNull = compute();
    }
    return implOrNull;
  }

  protected abstract E compute();

  public E getIfDefined() {
    return implOrNull;
  }

  public static  Lazy from(final E definedElement) {
    return new Lazy() {
      @Override
      protected E compute() {
        return definedElement;
      }
    };
  }

  public static  Lazy of(Supplier fn) {
    return new Lazy() {
      @Override
      protected E compute() {
        return fn.get();
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy