2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.io.vamsas;
21 import java.util.IdentityHashMap;
22 import java.util.Iterator;
25 public class DatastoreRegistry
27 protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
28 .getLogger(DatastoreRegistry.class);
31 * map between Datastore objects and the objects they are handling- used to
32 * prevent cycles in the synchronization pattern. Keys are both vamsas objects
33 * and jalview objects, values are datastore objects.
35 IdentityHashMap dsObjReg;
38 * all datastore items - value is [jvObject,vObject]
40 IdentityHashMap dsItemReg;
42 public DatastoreRegistry()
44 dsObjReg = new IdentityHashMap();
45 dsItemReg = new IdentityHashMap();
51 * @return true if obj is in the registry
53 public boolean isInvolvedInDsitem(Object obj)
55 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
56 : dsObjReg.containsKey(obj);
62 * @return DatastoreItem associated with obj - or null
64 public DatastoreItem getDatastoreItemFor(Object obj)
66 if (obj instanceof DatastoreItem)
68 log.debug("Returning DatastoreItem self reference.");// TODO: we could
73 return (DatastoreItem) obj;
75 return (DatastoreItem) dsObjReg.get(obj);
78 synchronized void registerDsObj(DatastoreItem dsitem)
80 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
81 if (dsregitem == null)
83 // create a new item entry
84 dsregitem = new Object[]
85 { dsitem.jvobj, dsitem.vobj };
86 dsItemReg.put(dsitem, dsregitem);
87 dsObjReg.put(dsitem.jvobj, dsitem);
88 dsObjReg.put(dsitem.vobj, dsitem);
92 // update registry for any changed references
94 if (dsitem.jvobj != dsregitem[0])
96 // overwrite existing involved entries.
97 if (dsregitem[0] != null)
99 dsObjReg.remove(dsregitem[0]);
101 if ((dsregitem[0] = dsitem.jvobj) != null)
103 dsObjReg.put(dsregitem[0], dsitem);
106 // and for the vobject
107 if (dsitem.vobj != dsregitem[1])
109 // overwrite existing involved entries.
110 if (dsregitem[1] != null)
112 dsObjReg.remove(dsregitem[1]);
114 if ((dsregitem[1] = dsitem.vobj) != null)
116 dsObjReg.put(dsregitem[1], dsitem);
123 * Remove dsitem from the registry
126 * @return null or last known Object[] { jvobject, vobject } references for
129 public synchronized Object[] removeDsObj(DatastoreItem dsitem)
131 Object[] dsregitem = null;
132 // synchronized (dsItemReg)
134 // synchronized (dsObjReg)
136 dsregitem = (Object[]) dsItemReg.remove(dsitem);
137 if (dsregitem != null)
139 // eliminate object refs too
140 if (dsregitem[0] != null)
142 dsObjReg.remove(dsregitem[0]);
144 if (dsregitem[1] != null)
146 dsObjReg.remove(dsregitem[1]);
154 protected void finalize()
156 if (dsObjReg != null)
158 Iterator items = dsObjReg.entrySet().iterator();
159 // avoid the nested reference memory leak.
160 while (items.hasNext())
162 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
169 if (dsItemReg != null)