JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / io / vamsas / DatastoreRegistry.java
index 21083dd..9ecd36c 100644 (file)
@@ -1,12 +1,29 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.io.vamsas;
 
-import java.util.Enumeration;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import uk.ac.vamsas.client.Vobject;
-
 public class DatastoreRegistry
 {
   protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
@@ -51,55 +68,53 @@ public class DatastoreRegistry
     if (obj instanceof DatastoreItem)
     {
       log.debug("Returning DatastoreItem self reference.");// TODO: we could
-                                                           // store the update
-                                                           // hierarchy - so
-                                                           // retrieve parent
-                                                           // for obj.
+      // store the update
+      // hierarchy - so
+      // retrieve parent
+      // for obj.
       return (DatastoreItem) obj;
     }
     return (DatastoreItem) dsObjReg.get(obj);
   }
 
-  public void registerDsObj(DatastoreItem dsitem)
+  synchronized void registerDsObj(DatastoreItem dsitem)
   {
-    synchronized (dsItemReg)
+    Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
+    if (dsregitem == null)
+    {
+      // create a new item entry
+      dsregitem = new Object[] { dsitem.jvobj, dsitem.vobj };
+      dsItemReg.put(dsitem, dsregitem);
+      dsObjReg.put(dsitem.jvobj, dsitem);
+      dsObjReg.put(dsitem.vobj, dsitem);
+    }
+    else
     {
-      Object[] dsregitem = (Object[]) dsItemReg.get(dsitem);
-      if (dsregitem == null)
+      // update registry for any changed references
+      // for the jvobject
+      if (dsitem.jvobj != dsregitem[0])
       {
-        // create a new item entry
-        dsregitem = new Object[]
-        { dsitem.jvobj, dsitem.vobj };
-        dsItemReg.put(dsitem, dsregitem);
+        // overwrite existing involved entries.
+        if (dsregitem[0] != null)
+        {
+          dsObjReg.remove(dsregitem[0]);
+        }
+        if ((dsregitem[0] = dsitem.jvobj) != null)
+        {
+          dsObjReg.put(dsregitem[0], dsitem);
+        }
       }
-      // atomic update of the jv and vobject references in the dsObjReg
-      synchronized (dsObjReg)
+      // and for the vobject
+      if (dsitem.vobj != dsregitem[1])
       {
-        // for the jvobject
-        if (dsitem.jvobj != dsregitem[0])
+        // overwrite existing involved entries.
+        if (dsregitem[1] != null)
         {
-          // overwrite existing involved entries.
-          if (dsregitem[0] != null)
-          {
-            dsObjReg.remove(dsregitem[0]);
-          }
-          if ((dsregitem[0] = dsitem.jvobj) != null)
-          {
-            dsObjReg.put(dsregitem[0], dsitem);
-          }
+          dsObjReg.remove(dsregitem[1]);
         }
-        // and for the vobject
-        if (dsitem.vobj != dsregitem[1])
+        if ((dsregitem[1] = dsitem.vobj) != null)
         {
-          // overwrite existing involved entries.
-          if (dsregitem[1] != null)
-          {
-            dsObjReg.remove(dsregitem[1]);
-          }
-          if ((dsregitem[1] = dsitem.vobj) != null)
-          {
-            dsObjReg.put(dsregitem[1], dsitem);
-          }
+          dsObjReg.put(dsregitem[1], dsitem);
         }
       }
     }
@@ -112,12 +127,12 @@ public class DatastoreRegistry
    * @return null or last known Object[] { jvobject, vobject } references for
    *         this dsitem
    */
-  public Object[] removeDsObj(DatastoreItem dsitem)
+  public synchronized Object[] removeDsObj(DatastoreItem dsitem)
   {
     Object[] dsregitem = null;
-    synchronized (dsItemReg)
+    // synchronized (dsItemReg)
     {
-      synchronized (dsObjReg)
+      // synchronized (dsObjReg)
       {
         dsregitem = (Object[]) dsItemReg.remove(dsitem);
         if (dsregitem != null)
@@ -152,7 +167,7 @@ public class DatastoreRegistry
       items = null;
       dsObjReg.clear();
     }
-    if (dsItemReg!=null)
+    if (dsItemReg != null)
     {
       dsItemReg.clear();
     }