2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.vamsas;
23 import java.util.IdentityHashMap;
24 import java.util.Iterator;
27 public class DatastoreRegistry
29 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
30 .getLogger(DatastoreRegistry.class);
33 * map between Datastore objects and the objects they are handling- used to
34 * prevent cycles in the synchronization pattern. Keys are both vamsas objects
35 * and jalview objects, values are datastore objects.
37 IdentityHashMap dsObjReg;
40 * all datastore items - value is [jvObject,vObject]
42 IdentityHashMap dsItemReg;
44 public DatastoreRegistry()
46 dsObjReg = new IdentityHashMap();
47 dsItemReg = new IdentityHashMap();
53 * @return true if obj is in the registry
55 public boolean isInvolvedInDsitem(Object obj)
57 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
58 : dsObjReg.containsKey(obj);
64 * @return DatastoreItem associated with obj - or null
66 public DatastoreItem getDatastoreItemFor(Object obj)
68 if (obj instanceof DatastoreItem)
70 log.debug("Returning DatastoreItem self reference.");// TODO: we could
75 return (DatastoreItem) obj;
77 return (DatastoreItem) dsObjReg.get(obj);
80 synchronized void registerDsObj(DatastoreItem dsitem)
82 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
83 if (dsregitem == null)
85 // create a new item entry
86 dsregitem = new Object[]
87 { dsitem.jvobj, dsitem.vobj };
88 dsItemReg.put(dsitem, dsregitem);
89 dsObjReg.put(dsitem.jvobj, dsitem);
90 dsObjReg.put(dsitem.vobj, dsitem);
94 // update registry for any changed references
96 if (dsitem.jvobj != dsregitem[0])
98 // overwrite existing involved entries.
99 if (dsregitem[0] != null)
101 dsObjReg.remove(dsregitem[0]);
103 if ((dsregitem[0] = dsitem.jvobj) != null)
105 dsObjReg.put(dsregitem[0], dsitem);
108 // and for the vobject
109 if (dsitem.vobj != dsregitem[1])
111 // overwrite existing involved entries.
112 if (dsregitem[1] != null)
114 dsObjReg.remove(dsregitem[1]);
116 if ((dsregitem[1] = dsitem.vobj) != null)
118 dsObjReg.put(dsregitem[1], dsitem);
125 * Remove dsitem from the registry
128 * @return null or last known Object[] { jvobject, vobject } references for
131 public synchronized Object[] removeDsObj(DatastoreItem dsitem)
133 Object[] dsregitem = null;
134 // synchronized (dsItemReg)
136 // synchronized (dsObjReg)
138 dsregitem = (Object[]) dsItemReg.remove(dsitem);
139 if (dsregitem != null)
141 // eliminate object refs too
142 if (dsregitem[0] != null)
144 dsObjReg.remove(dsregitem[0]);
146 if (dsregitem[1] != null)
148 dsObjReg.remove(dsregitem[1]);
156 protected void finalize()
158 if (dsObjReg != null)
160 Iterator items = dsObjReg.entrySet().iterator();
161 // avoid the nested reference memory leak.
162 while (items.hasNext())
164 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
171 if (dsItemReg != null)