templates.velocity.cache_guava.pertable.cache.manager.vm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-generator Show documentation
Show all versions of sql2java-generator Show documentation
executable jar of sql2java generator
#parse( "table.include.vm" )
#parse( "header.include.vm" )
#if ($table.countPrimaryKeys() == 0)
$codewriter.setSaveCurrentFile(false)
#stop
#end
#set ( $cacheManagerClass = $table.asCacheManagerClass($isGeneral) )
#set ( $cacheClass = ${table.asCacheClass($isGeneral)})
#set ( $managerClass = $table.asManagerClass($isGeneral) )
#set ( $beanClass = $table.asBeanClass($isGeneral) )
$codewriter.setCurrentJavaFilename("${pkg}.manager", "${cacheManagerClass}.java")
package ${pkg}.manager;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.*;
import gu.sql2java.BaseBean;
import gu.sql2java.ICacheManager;
import gu.sql2java.ListenerContainer;
import gu.sql2java.TableCache;
import gu.sql2java.TableListener;
import gu.sql2java.exception.DaoException;
import gu.sql2java.exception.ObjectRetrievalException;
import gu.sql2java.exception.RuntimeDaoException;
import ${pkg}.*;
#macro(instanceOf $extMgr)#if($extMgr != $cacheManagerClass)${extMgr}.getInstance().#end#end
/**
* cache implementation for $managerClass
* @author guyadong
*/
public class $cacheManagerClass extends $managerClass implements ICacheManager
{
/** singleton of $cacheManagerClass */
private static $cacheManagerClass instance;
/**
* @return a instance of $cacheManagerClass
* @throws IllegalStateException while {@link #instance} is null
*/
public static final $cacheManagerClass getInstance(){
checkState(null != instance,"Uninitialized instance of $cacheManagerClass");
return instance;
}
/**
* create a instance of $cacheManagerClass and assign to {@link #instance} if {@code instance} is not initialized.
* otherwise return {@code instance}.
* @see $cacheManagerClass#$cacheManagerClass(UpdateStrategy ,long , long , TimeUnit )
*/
public static synchronized final $cacheManagerClass makeInstance(UpdateStrategy updateStrategy,long maximumSize, long duration, TimeUnit unit){
if(instance == null){
instance = new ${cacheManagerClass}(updateStrategy,maximumSize,duration,unit);
}
return instance;
}
/** @see ${esc.hash}makeInstance(UpdateStrategy,long, long, TimeUnit) */
public static final $cacheManagerClass makeInstance(long maximumSize, long duration, TimeUnit unit){
return makeInstance(DEFAULT_STRATEGY,maximumSize, duration, unit);
}
/** @see ${esc.hash}makeInstance(long, long, TimeUnit) */
public static final $cacheManagerClass makeInstance(long maximumSize, long durationMinutes){
return makeInstance(maximumSize, durationMinutes, DEFAULT_TIME_UNIT);
}
/** @see ${esc.hash}makeInstance(long, long, TimeUnit) */
public static final $cacheManagerClass makeInstance(long maximumSize){
return makeInstance(maximumSize,DEFAULT_DURATION,DEFAULT_TIME_UNIT);
}
/** instance of {@link TableCache} */
private final TableCache<$beanClass> cache;
/** constructor
* @param updateStrategy
* @param maximumSize
* @param duration
* @param unit
* @see TableCache${esc.hash}TableCache(gu.sql2java.RowMetaData, UpdateStrategy, long, long, TimeUnit)
*/
protected $cacheManagerClass(UpdateStrategy updateStrategy,long maximumSize, long duration, TimeUnit unit) {
super();
cache = new TableCache<>(metaData,updateStrategy,maximumSize,duration,unit);
cache.registerListener();
}
@Override
protected $beanClass doLoadByPrimaryKeyChecked(Object ...keys)throws RuntimeDaoException,ObjectRetrievalException{
return cache.getBean(keys);
}
@Override
protected boolean doExistsPrimaryKey(Object ...keys)throws RuntimeDaoException{
return null != cache.getBeanUnchecked(keys);
}
@Override
protected int actionOnResultSet(ResultSet rs, int[] fieldList, int startRow, int numRows, Action<$beanClass> action) throws DaoException{
if(null == fieldList || fieldList.length == 0){
action = cache.wrap(action);
}
return super.actionOnResultSet(rs, fieldList, startRow, numRows, action);
}
@Override
protected $beanClass doLoadUniqueByIndex(String indexName,Object ...indexValues)throws RuntimeDaoException{
return cache.getBeanByIndexUnchecked(indexName, indexValues);
}
@Override
protected $beanClass doLoadUniqueByIndexChecked(String indexName,Object ...indexValues)throws ObjectRetrievalException{
return cache.getBeanByIndex(indexName, indexValues);
}
@Override
public Map> getForeignKeyDeleteListeners(){
return cache.getManager().getForeignKeyDeleteListeners();
}
@Override
public ListenerContainer<$beanClass> getListenerContainer() {
return cache.getManager().getListenerContainer();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("$cacheManagerClass [cache=");
builder.append(cache);
builder.append("]");
return builder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy