| IdentityMapMixin.java |
/* PAT: Persistent Applications Toolkit (patsystem.sf.net)
* Copyright (C) 2004, 2005 Tomasz Nazar, nthx at irc dot pl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Full version of the license is /docs/LICENSE.txt
*/
package org.nthx.pat;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
/** Mixin for {@link IdentityMap} interface.
*
* @version $Id: IdentityMapMixin.java 3725 2005-06-09 23:57:03Z nthx $
* @author nthx@users.sourceforge.net
*/
public class IdentityMapMixin
implements IdentityMap, Serializable
{
//--- Fields internal -------
private transient static Logger log = Logger.getLogger("pat");
//--- Fields ----------------
private Map map;
private long uniqueOID;
//--- Constructors ----------
public IdentityMapMixin()
{
uniqueOID = 1L;
map = new HashMap();
}
//--- Getters and Setters ---
Map getMap(){ return map; }
public Map getMapForTestsOnly(){ return map; }
public void setMap(Map map) { this.map = map; }
public void setUniqueOID(long uniqueOID) { this.uniqueOID = uniqueOID; }
public long getUniqueOID() { return uniqueOID; }
public int getSize() { return getMap().size(); }
//--- Implementation --------
public Identifiable putObject(Identifiable identifiable)
{
if (null == identifiable) return null;
if (null != identifiable.getOID())
throw new IllegalStateException("Trying to put BO which has OID already set: "
+ "\nOID: " + identifiable.getOID() + " object: " + identifiable
+ "\nThis is a BUG. Please send this output to: nthx at users.sf.net"
);
//if object already exists
Identifiable existing = getObject(identifiable.getOID());
if (null != existing)
{
throw new IllegalStateException(
"Not inserting: " + identifiable
+ "\n.. Because it exists there as: " + existing);
}
Long oid = generateUniqueOID();
identifiable.setOID(oid);
// log.debug("Put: " + oid + " " + identifiable.getClass());
// log.debug("Inserted: " + identifiable);
return (Identifiable) getMap().put(identifiable.getOID(), identifiable);
}
public Identifiable getObject(Long oid)
{
return (Identifiable) getMap().get(oid);
}
/** Designed for removing objects which were created as temporary objects or
* for objects that were in failed transaction.
* xxx: create client's API for that or use WeakHashMap?
*/
public Identifiable removeObject(Identifiable bo)
{
return (Identifiable) getMap().remove(bo.getOID());
}
public Long generateUniqueOID()
{
return new Long(uniqueOID++);
}
public Collection keySet()
{
return getMap().keySet();
}
public Collection values()
{
return getMap().values();
}
final static long serialVersionUID = 1L;
}