2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.io.vamsas;
20 import java.util.IdentityHashMap;
21 import java.util.Iterator;
24 public class DatastoreRegistry
26 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
27 .getLogger(DatastoreRegistry.class);
30 * map between Datastore objects and the objects they are handling- used to
31 * prevent cycles in the synchronization pattern. Keys are both vamsas objects
32 * and jalview objects, values are datastore objects.
34 IdentityHashMap dsObjReg;
37 * all datastore items - value is [jvObject,vObject]
39 IdentityHashMap dsItemReg;
41 public DatastoreRegistry()
43 dsObjReg = new IdentityHashMap();
44 dsItemReg = new IdentityHashMap();
50 * @return true if obj is in the registry
52 public boolean isInvolvedInDsitem(Object obj)
54 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
55 : dsObjReg.containsKey(obj);
61 * @return DatastoreItem associated with obj - or null
63 public DatastoreItem getDatastoreItemFor(Object obj)
65 if (obj instanceof DatastoreItem)
67 log.debug("Returning DatastoreItem self reference.");// TODO: we could
72 return (DatastoreItem) obj;
74 return (DatastoreItem) dsObjReg.get(obj);
77 synchronized void registerDsObj(DatastoreItem dsitem)
79 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
80 if (dsregitem == null)
82 // create a new item entry
83 dsregitem = new Object[]
84 { dsitem.jvobj, dsitem.vobj };
85 dsItemReg.put(dsitem, dsregitem);
86 dsObjReg.put(dsitem.jvobj, dsitem);
87 dsObjReg.put(dsitem.vobj, dsitem);
91 // update registry for any changed references
93 if (dsitem.jvobj != dsregitem[0])
95 // overwrite existing involved entries.
96 if (dsregitem[0] != null)
98 dsObjReg.remove(dsregitem[0]);
100 if ((dsregitem[0] = dsitem.jvobj) != null)
102 dsObjReg.put(dsregitem[0], dsitem);
105 // and for the vobject
106 if (dsitem.vobj != dsregitem[1])
108 // overwrite existing involved entries.
109 if (dsregitem[1] != null)
111 dsObjReg.remove(dsregitem[1]);
113 if ((dsregitem[1] = dsitem.vobj) != null)
115 dsObjReg.put(dsregitem[1], dsitem);
122 * Remove dsitem from the registry
125 * @return null or last known Object[] { jvobject, vobject } references for
128 public synchronized Object[] removeDsObj(DatastoreItem dsitem)
130 Object[] dsregitem = null;
131 // synchronized (dsItemReg)
133 // synchronized (dsObjReg)
135 dsregitem = (Object[]) dsItemReg.remove(dsitem);
136 if (dsregitem != null)
138 // eliminate object refs too
139 if (dsregitem[0] != null)
141 dsObjReg.remove(dsregitem[0]);
143 if (dsregitem[1] != null)
145 dsObjReg.remove(dsregitem[1]);
153 protected void finalize()
155 if (dsObjReg != null)
157 Iterator items = dsObjReg.entrySet().iterator();
158 // avoid the nested reference memory leak.
159 while (items.hasNext())
161 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
168 if (dsItemReg != null)