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

com.rapleaf.jack.LazyLoadPersistence Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package com.rapleaf.jack;

import java.io.Serializable;

public class LazyLoadPersistence implements Serializable {
  @FunctionalInterface
  public interface Factory {
    T create(BaseDatabaseConnection conn, D databases);
  }

  private final BaseDatabaseConnection conn;
  private final D databases;
  private final Factory factory;

  private volatile T persistence;

  private volatile boolean disableCaching;

  public LazyLoadPersistence(BaseDatabaseConnection conn, D databases, Factory factory) {
    this.conn = conn;
    this.databases = databases;

    this.persistence = null;
    this.disableCaching = false;
    this.factory = factory;
  }

  public T get() {
    if (persistence == null) {
      synchronized (this) {
        if (persistence == null) {
          this.persistence = factory.create(conn, databases);

          if (disableCaching) {
            persistence.disableCaching();
          }
        }
      }
    }

    return persistence;
  }

  public void disableCaching() {
    disableCaching = true;

    if (persistence != null) {
      persistence.disableCaching();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy