Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io.vamsas

File DatastoreRegistry.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

30
42
6
1
177
108
21
0.5
7
6
3.5

Classes

Class Line # Actions
DatastoreRegistry 27 42 21 78
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.io.vamsas;
22   
23    import java.util.IdentityHashMap;
24    import java.util.Iterator;
25    import java.util.Map;
26   
 
27    public class DatastoreRegistry
28    {
29    protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
30    .getLogger(DatastoreRegistry.class);
31   
32    /**
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.
36    */
37    IdentityHashMap dsObjReg;
38   
39    /**
40    * all datastore items - value is [jvObject,vObject]
41    */
42    IdentityHashMap dsItemReg;
43   
 
44  0 toggle public DatastoreRegistry()
45    {
46  0 dsObjReg = new IdentityHashMap();
47  0 dsItemReg = new IdentityHashMap();
48    }
49   
50    /**
51    *
52    * @param obj
53    * @return true if obj is in the registry
54    */
 
55  0 toggle public boolean isInvolvedInDsitem(Object obj)
56    {
57  0 return (obj instanceof DatastoreItem) ? dsItemReg.containsKey(obj)
58    : dsObjReg.containsKey(obj);
59    }
60   
61    /**
62    *
63    * @param obj
64    * @return DatastoreItem associated with obj - or null
65    */
 
66  0 toggle public DatastoreItem getDatastoreItemFor(Object obj)
67    {
68  0 if (obj instanceof DatastoreItem)
69    {
70  0 log.debug("Returning DatastoreItem self reference.");// TODO: we could
71    // store the update
72    // hierarchy - so
73    // retrieve parent
74    // for obj.
75  0 return (DatastoreItem) obj;
76    }
77  0 return (DatastoreItem) dsObjReg.get(obj);
78    }
79   
 
80  0 toggle synchronized void registerDsObj(DatastoreItem dsitem)
81    {
82  0 Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
83  0 if (dsregitem == null)
84    {
85    // create a new item entry
86  0 dsregitem = new Object[] { dsitem.jvobj, dsitem.vobj };
87  0 dsItemReg.put(dsitem, dsregitem);
88  0 dsObjReg.put(dsitem.jvobj, dsitem);
89  0 dsObjReg.put(dsitem.vobj, dsitem);
90    }
91    else
92    {
93    // update registry for any changed references
94    // for the jvobject
95  0 if (dsitem.jvobj != dsregitem[0])
96    {
97    // overwrite existing involved entries.
98  0 if (dsregitem[0] != null)
99    {
100  0 dsObjReg.remove(dsregitem[0]);
101    }
102  0 if ((dsregitem[0] = dsitem.jvobj) != null)
103    {
104  0 dsObjReg.put(dsregitem[0], dsitem);
105    }
106    }
107    // and for the vobject
108  0 if (dsitem.vobj != dsregitem[1])
109    {
110    // overwrite existing involved entries.
111  0 if (dsregitem[1] != null)
112    {
113  0 dsObjReg.remove(dsregitem[1]);
114    }
115  0 if ((dsregitem[1] = dsitem.vobj) != null)
116    {
117  0 dsObjReg.put(dsregitem[1], dsitem);
118    }
119    }
120    }
121    }
122   
123    /**
124    * Remove dsitem from the registry
125    *
126    * @param dsitem
127    * @return null or last known Object[] { jvobject, vobject } references for
128    * this dsitem
129    */
 
130  0 toggle public synchronized Object[] removeDsObj(DatastoreItem dsitem)
131    {
132  0 Object[] dsregitem = null;
133    // synchronized (dsItemReg)
134    {
135    // synchronized (dsObjReg)
136    {
137  0 dsregitem = (Object[]) dsItemReg.remove(dsitem);
138  0 if (dsregitem != null)
139    {
140    // eliminate object refs too
141  0 if (dsregitem[0] != null)
142    {
143  0 dsObjReg.remove(dsregitem[0]);
144    }
145  0 if (dsregitem[1] != null)
146    {
147  0 dsObjReg.remove(dsregitem[1]);
148    }
149    }
150    }
151    }
152  0 return dsregitem;
153    }
154   
 
155  0 toggle @Override
156    protected void finalize() throws Throwable
157    {
158  0 if (dsObjReg != null)
159    {
160  0 Iterator items = dsObjReg.entrySet().iterator();
161    // avoid the nested reference memory leak.
162  0 while (items.hasNext())
163    {
164  0 Object[] vals = (Object[]) ((Map.Entry) items.next()).getValue();
165  0 vals[0] = null;
166  0 vals[1] = null;
167    }
168  0 items = null;
169  0 dsObjReg.clear();
170    }
171  0 if (dsItemReg != null)
172    {
173  0 dsItemReg.clear();
174    }
175  0 super.finalize();
176    }
177    }