📄 abstractstaticdomain.java
字号:
/*
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is jRelationalFramework.
*
* The Initial Developer of the Original Code is is.com.
* Portions created by is.com are Copyright (C) 2000 is.com.
* All Rights Reserved.
*
* Contributor(s): Jonathan Carlson (joncrlsn@users.sf.net)
* Contributor(s): ____________________________________
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License (the "GPL") or the GNU Lesser General
* Public license (the "LGPL"), in which case the provisions of the GPL or
* LGPL are applicable instead of those above. If you wish to allow use of
* your version of this file only under the terms of either the GPL or LGPL
* and not to allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by either the GPL or LGPL
* License. If you do not delete the provisions above, a recipient may use
* your version of this file under either the MPL or GPL or LGPL License.
*
*/
package com.is.jrf;
import com.is.util.sql.JDBCHelper;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This abstract superclass caches the PersistentObjects for it's
* subclasses.
*
* Subclasses of this abstract class should represent relatively small
* lookup tables that rarely or never change. i.e. a Gender table with two
* rows. One row for male, and one for female. The resulting
* PersistentObjects are cached so that calls to find() or findAll() return
* the cached instances instead.
*
* The cache uses a ThreadLocal instance so this should be able to be used in a
* multi-threaded environment where the threads are pooled. If the threads
* are not pooled then using this superclass may not benefit the
* performance of the application much.
*
* In a single-threaded environment this will work fine too.
*/
public abstract class AbstractStaticDomain
extends AbstractDomain
{
/**
* This is a ThreadLocal so it can be used in an EJB environment.
*/
private static ThreadLocal s_cache = null;
/**
* This is a static initializer that is executed when this class is loaded
* by the JVM. I'm commenting it because I haven't seen this feature used
* all that much.
*/
static
{
s_cache = new ThreadLocal()
{
protected Object initialValue()
{
return new HashMap();
}
};
} // static
public AbstractStaticDomain()
{
super();
}
public AbstractStaticDomain(int option)
{
super(option);
}
/**
* Return a Map is unique to this thread and subclass. If not populated,
* the map will be populated with the results of findAll() before being
* returned to the caller.
*
* @return a value of type 'Map'
*/
private Map getInstanceCache()
{
Map classMap = (Map) s_cache.get();
Map instanceCache = (Map) classMap.get(this.getClass());
if (instanceCache == null)
{
instanceCache = new HashMap();
// We must pass in the default JDBCHelper and call super below to
// avoid using the overridden findAll(aJDBCHelper) in this
// subclass.
List allPOs = super.findAll(this.getJDBCHelper());
String key = null;
PersistentObject po = null;
Iterator iterator = allPOs.iterator();
while (iterator.hasNext())
{
po = (PersistentObject) iterator.next();
key = this.encodePrimaryKey(po);
instanceCache.put(key,po);
}
classMap.put(this.getClass(), instanceCache);
}
return instanceCache;
}
/**
* This is a method override that gets its objects from the cache.
*
* @param pkOrPO a value of type 'Object'
* @param aJDBCHelper a value of type 'JDBCHelper' - not used but needed
* to match the signature of the overridden method.
* @return a value of type 'PersistentObject'
*/
public PersistentObject find(Object pkOrPO, JDBCHelper aJDBCHelper)
{
String key = null;
if (pkOrPO instanceof PersistentObject)
{
PersistentObject aPO = (PersistentObject) pkOrPO;
key = this.encodePrimaryKey(aPO);
}
else
{
key = (pkOrPO==null ? "null" : pkOrPO.toString());
}
return (PersistentObject) this.getInstanceCache().get(key);
} // find(pkOrPO, aJDBCHelper)
/**
* Return all of the instances that are in the cache.
*
* @param aJDBCHelper a value of type 'JDBCHelper'
* @return a value of type 'List'
*/
public List findAll(JDBCHelper aJDBCHelper)
{
return new ArrayList(this.getInstanceCache().values());
}
} // AbstractStaticDomain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -