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

com.cedarsoft.gdao.DefaultGenericDaoManager Maven / Gradle / Ivy

The newest version!
package com.cedarsoft.gdao;

import com.cedarsoft.utils.Cache;
import com.cedarsoft.utils.HashedCache;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

/**
 * Manages the DAOs
 */
public class DefaultGenericDaoManager implements GenericDaoManager {
  @SuppressWarnings( {"MismatchedQueryAndUpdateOfCollection"} )
  @NotNull
  private final Cache, GenericDao> daoCache;

  /**
   * Creates a new dao manager
   *
   * @param daoFactory the factory
   */
  public DefaultGenericDaoManager( @NotNull Cache.Factory, GenericDao> daoFactory ) {
    daoCache = new HashedCache, GenericDao>( daoFactory );
  }

  /**
   * Returns the dao
   *
   * @param type the type
   * @return the dao for the given type
   */
  @NotNull
  public  GenericDao getDao( @NotNull Class type ) {
    return getDao( type, null );
  }

  @NotNull
  public  GenericDao getDao( @NotNull Class type, @Nullable LockProvider lockProvider ) {
    return ( GenericDao ) daoCache.get( new DaoTypeDescriptor( type, lockProvider ) );
  }

  public void shutdown() {
    for ( Iterator> it = daoCache.values().iterator(); it.hasNext(); ) {
      GenericDao dao = it.next();
      dao.shutdown();
      it.remove();
    }
  }
}