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;
public class IdentityMapMixin
implements IdentityMap, Serializable
{
private transient static Logger log = Logger.getLogger("pat");
private Map map;
private long uniqueOID;
public IdentityMapMixin()
{
uniqueOID = 1L;
map = new HashMap();
}
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(); }
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"
);
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);
return (Identifiable) getMap().put(identifiable.getOID(), identifiable);
}
public Identifiable getObject(Long oid)
{
return (Identifiable) getMap().get(oid);
}
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;
}