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 import jalview.log.JLoggerLog4j;
29 public class DatastoreRegistry implements AutoCloseable
31 protected static JLoggerLog4j log = JLoggerLog4j
32 .getLogger(DatastoreRegistry.class);
35 * map between Datastore objects and the objects they are handling- used to
36 * prevent cycles in the synchronization pattern. Keys are both vamsas objects
37 * and jalview objects, values are datastore objects.
39 IdentityHashMap dsObjReg;
42 * all datastore items - value is [jvObject,vObject]
44 IdentityHashMap dsItemReg;
46 public DatastoreRegistry()
48 dsObjReg = new IdentityHashMap();
49 dsItemReg = new IdentityHashMap();
55 * @return true if obj is in the registry
57 public boolean isInvolvedInDsitem(Object obj)
59 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
60 : dsObjReg.containsKey(obj);
66 * @return DatastoreItem associated with obj - or null
68 public DatastoreItem getDatastoreItemFor(Object obj)
70 if (obj instanceof DatastoreItem)
72 log.debug("Returning DatastoreItem self reference.");// TODO: we could
77 return (DatastoreItem) obj;
79 return (DatastoreItem) dsObjReg.get(obj);
82 synchronized void registerDsObj(DatastoreItem dsitem)
84 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
85 if (dsregitem == null)
87 // create a new item entry
88 dsregitem = new Object[] { dsitem.jvobj, dsitem.vobj };
89 dsItemReg.put(dsitem, dsregitem);
90 dsObjReg.put(dsitem.jvobj, dsitem);
91 dsObjReg.put(dsitem.vobj, dsitem);
95 // update registry for any changed references
97 if (dsitem.jvobj != dsregitem[0])
99 // overwrite existing involved entries.
100 if (dsregitem[0] != null)
102 dsObjReg.remove(dsregitem[0]);
104 if ((dsregitem[0] = dsitem.jvobj) != null)
106 dsObjReg.put(dsregitem[0], dsitem);
109 // and for the vobject
110 if (dsitem.vobj != dsregitem[1])
112 // overwrite existing involved entries.
113 if (dsregitem[1] != null)
115 dsObjReg.remove(dsregitem[1]);
117 if ((dsregitem[1] = dsitem.vobj) != null)
119 dsObjReg.put(dsregitem[1], dsitem);
126 * Remove dsitem from the registry
129 * @return null or last known Object[] { jvobject, vobject } references for
132 public synchronized Object[] removeDsObj(DatastoreItem dsitem)
134 Object[] dsregitem = null;
135 // synchronized (dsItemReg)
137 // synchronized (dsObjReg)
139 dsregitem = (Object[]) dsItemReg.remove(dsitem);
140 if (dsregitem != null)
142 // eliminate object refs too
143 if (dsregitem[0] != null)
145 dsObjReg.remove(dsregitem[0]);
147 if (dsregitem[1] != null)
149 dsObjReg.remove(dsregitem[1]);
160 if (dsObjReg != null)
162 Iterator items = dsObjReg.entrySet().iterator();
163 // avoid the nested reference memory leak.
164 while (items.hasNext())
166 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
173 if (dsItemReg != null)