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 org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
30 public class DatastoreRegistry implements AutoCloseable
32 protected static Logger log = LogManager
33 .getLogger(DatastoreRegistry.class);
36 * map between Datastore objects and the objects they are handling- used to
37 * prevent cycles in the synchronization pattern. Keys are both vamsas objects
38 * and jalview objects, values are datastore objects.
40 IdentityHashMap dsObjReg;
43 * all datastore items - value is [jvObject,vObject]
45 IdentityHashMap dsItemReg;
47 public DatastoreRegistry()
49 dsObjReg = new IdentityHashMap();
50 dsItemReg = new IdentityHashMap();
56 * @return true if obj is in the registry
58 public boolean isInvolvedInDsitem(Object obj)
60 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
61 : dsObjReg.containsKey(obj);
67 * @return DatastoreItem associated with obj - or null
69 public DatastoreItem getDatastoreItemFor(Object obj)
71 if (obj instanceof DatastoreItem)
73 log.debug("Returning DatastoreItem self reference.");// TODO: we could
78 return (DatastoreItem) obj;
80 return (DatastoreItem) dsObjReg.get(obj);
83 synchronized void registerDsObj(DatastoreItem dsitem)
85 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
86 if (dsregitem == null)
88 // create a new item entry
89 dsregitem = new Object[] { dsitem.jvobj, dsitem.vobj };
90 dsItemReg.put(dsitem, dsregitem);
91 dsObjReg.put(dsitem.jvobj, dsitem);
92 dsObjReg.put(dsitem.vobj, dsitem);
96 // update registry for any changed references
98 if (dsitem.jvobj != dsregitem[0])
100 // overwrite existing involved entries.
101 if (dsregitem[0] != null)
103 dsObjReg.remove(dsregitem[0]);
105 if ((dsregitem[0] = dsitem.jvobj) != null)
107 dsObjReg.put(dsregitem[0], dsitem);
110 // and for the vobject
111 if (dsitem.vobj != dsregitem[1])
113 // overwrite existing involved entries.
114 if (dsregitem[1] != null)
116 dsObjReg.remove(dsregitem[1]);
118 if ((dsregitem[1] = dsitem.vobj) != null)
120 dsObjReg.put(dsregitem[1], dsitem);
127 * Remove dsitem from the registry
130 * @return null or last known Object[] { jvobject, vobject } references for
133 public synchronized Object[] removeDsObj(DatastoreItem dsitem)
135 Object[] dsregitem = null;
136 // synchronized (dsItemReg)
138 // synchronized (dsObjReg)
140 dsregitem = (Object[]) dsItemReg.remove(dsitem);
141 if (dsregitem != null)
143 // eliminate object refs too
144 if (dsregitem[0] != null)
146 dsObjReg.remove(dsregitem[0]);
148 if (dsregitem[1] != null)
150 dsObjReg.remove(dsregitem[1]);
161 if (dsObjReg != null)
163 Iterator items = dsObjReg.entrySet().iterator();
164 // avoid the nested reference memory leak.
165 while (items.hasNext())
167 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
174 if (dsItemReg != null)