JAL-2778 Revert back to old synchronisation
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 65ff9a5..c0a43ee 100644 (file)
@@ -33,7 +33,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 public class HiddenColumns
 {
   private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
-  
+
   /*
    * list of hidden column [start, end] ranges; the list is maintained in
    * ascending start column order
@@ -56,8 +56,7 @@ public class HiddenColumns
   {
     try
     {
-
-      LOCK.readLock().lock();
+      LOCK.writeLock().lock();
       if (copy != null)
       {
         if (copy.hiddenColumns != null)
@@ -67,7 +66,7 @@ public class HiddenColumns
       }
     } finally
     {
-      LOCK.readLock().unlock();
+      LOCK.writeLock().unlock();
     }
   }
 
@@ -128,7 +127,7 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
       int size = 0;
-      if (hasHidden())
+      if (hasHiddenColumns())
       {
         for (int[] range : hiddenColumns)
         {
@@ -136,29 +135,10 @@ public class HiddenColumns
         }
       }
       return size;
-    }
-    finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
-  /**
-   * Answers if there are any hidden columns
-   * 
-   * @return true if there are hidden columns
-   */
-  public boolean hasHidden()
-  {
-    try
-    {
-      LOCK.readLock().lock();
-      return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
     } finally
     {
       LOCK.readLock().unlock();
     }
-
   }
 
   @Override
@@ -309,47 +289,47 @@ public class HiddenColumns
     {
 
       LOCK.readLock().lock();
-    int distance = visibleDistance;
+      int distance = visibleDistance;
 
-    // in case startColumn is in a hidden region, move it to the left
-    int start = adjustForHiddenColumns(findColumnPosition(startColumn));
+      // in case startColumn is in a hidden region, move it to the left
+      int start = adjustForHiddenColumns(findColumnPosition(startColumn));
 
-    // get index of hidden region to left of start
-    int index = getHiddenIndexLeft(start);
-    if (index == -1)
-    {
-      // no hidden regions to left of startColumn
-      return start - distance;
-    }
+      // get index of hidden region to left of start
+      int index = getHiddenIndexLeft(start);
+      if (index == -1)
+      {
+        // no hidden regions to left of startColumn
+        return start - distance;
+      }
 
-    // walk backwards through the alignment subtracting the counts of visible
-    // columns from distance
-    int[] region;
-    int gap = 0;
-    int nextstart = start;
+      // walk backwards through the alignment subtracting the counts of visible
+      // columns from distance
+      int[] region;
+      int gap = 0;
+      int nextstart = start;
 
-    while ((index > -1) && (distance - gap > 0))
-    {
-      // subtract the gap to right of region from distance
-      distance -= gap;
-      start = nextstart;
+      while ((index > -1) && (distance - gap > 0))
+      {
+        // subtract the gap to right of region from distance
+        distance -= gap;
+        start = nextstart;
 
-      // calculate the next gap
-      region = hiddenColumns.get(index);
-      gap = start - region[1];
+        // calculate the next gap
+        region = hiddenColumns.get(index);
+        gap = start - region[1];
 
-      // set start to just to left of current region
-      nextstart = region[0] - 1;
-      index--;
-    }
+        // set start to just to left of current region
+        nextstart = region[0] - 1;
+        index--;
+      }
 
-    if (distance - gap > 0)
-    {
-      // fell out of loop because there are no more hidden regions
-      distance -= gap;
-      return nextstart - distance;
-    }
-    return start - distance;
+      if (distance - gap > 0)
+      {
+        // fell out of loop because there are no more hidden regions
+        distance -= gap;
+        return nextstart - distance;
+      }
+      return start - distance;
     } finally
     {
       LOCK.readLock().unlock();
@@ -401,8 +381,7 @@ public class HiddenColumns
       }
 
       return positions;
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -456,22 +435,22 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
 
-    if (hiddenColumns != null)
-    {
-      int index = hiddenColumns.size() - 1;
-      do
+      if (hiddenColumns != null)
       {
-          int[] region = hiddenColumns.get(index);
-        if (alPos > region[1])
+        int index = hiddenColumns.size() - 1;
+        do
         {
-          return region[1];
-        }
+          int[] region = hiddenColumns.get(index);
+          if (alPos > region[1])
+          {
+            return region[1];
+          }
 
-        index--;
-      } while (index > -1);
-    }
+          index--;
+        } while (index > -1);
+      }
 
-    return alPos;
+      return alPos;
     } finally
     {
       LOCK.readLock().unlock();
@@ -492,22 +471,22 @@ public class HiddenColumns
     {
 
       LOCK.readLock().lock();
-    if (hiddenColumns != null)
-    {
-      int index = hiddenColumns.size() - 1;
-      do
+      if (hiddenColumns != null)
       {
-          int[] region = hiddenColumns.get(index);
-        if (pos > region[1])
+        int index = hiddenColumns.size() - 1;
+        do
         {
-          return index;
-        }
+          int[] region = hiddenColumns.get(index);
+          if (pos > region[1])
+          {
+            return index;
+          }
 
-        index--;
-      } while (index > -1);
-    }
+          index--;
+        } while (index > -1);
+      }
 
-    return -1;
+      return -1;
     } finally
     {
       LOCK.readLock().unlock();
@@ -523,24 +502,19 @@ public class HiddenColumns
    */
   public void hideColumns(int start, int end)
   {
-    hideColumns(start, end, false);
-  }
-
-  /**
-   * Adds the specified column range to the hidden columns
-   * 
-   * @param start
-   * @param end
-   */
-  private void hideColumns(int start, int end, boolean alreadyLocked)
-  {
+    boolean wasAlreadyLocked = false;
     try
     {
-
-      if (!alreadyLocked)
+      // check if the write lock was already locked by this thread,
+      // as this method can be called internally in loops within HiddenColumns
+      if (!LOCK.isWriteLockedByCurrentThread())
       {
         LOCK.writeLock().lock();
       }
+      else
+      {
+        wasAlreadyLocked = true;
+      }
 
       if (hiddenColumns == null)
       {
@@ -602,15 +576,15 @@ public class HiddenColumns
           }
           return;
         }
-    }
+      }
 
-    /*
-     * remaining case is that the new range follows everything else
-     */
+      /*
+       * remaining case is that the new range follows everything else
+       */
       hiddenColumns.add(new int[] { start, end });
     } finally
     {
-      if (!alreadyLocked)
+      if (!wasAlreadyLocked)
       {
         LOCK.writeLock().unlock();
       }
@@ -623,18 +597,18 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
 
-    if (hiddenColumns != null)
-    {
-      for (int[] region : hiddenColumns)
+      if (hiddenColumns != null)
       {
-        if (column >= region[0] && column <= region[1])
+        for (int[] region : hiddenColumns)
         {
-          return false;
+          if (column >= region[0] && column <= region[1])
+          {
+            return false;
+          }
         }
       }
-    }
 
-    return true;
+      return true;
     } finally
     {
       LOCK.readLock().unlock();
@@ -665,7 +639,7 @@ public class HiddenColumns
 
     return copy;
   }
-  
+
   /**
    * Returns a copy of the vector of hidden regions, as an ArrayList. Before
    * using this method please consider if you really need access to the hidden
@@ -784,8 +758,7 @@ public class HiddenColumns
           }
         }
       }
-    }
-    finally
+    } finally
     {
       LOCK.writeLock().unlock();
     }
@@ -852,8 +825,7 @@ public class HiddenColumns
       {
         return new int[] { start, end - 1 };
       }
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -922,8 +894,7 @@ public class HiddenColumns
       }
 
       return selections;
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -1016,8 +987,7 @@ public class HiddenColumns
       }
       // otherwise, sequence was completely hidden
       return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -1138,8 +1108,7 @@ public class HiddenColumns
       {
         alignmentAnnotation.restrict(start, end);
       }
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -1191,7 +1160,7 @@ public class HiddenColumns
       List<int[]> inserts = sr.getInsertions();
       for (int[] r : inserts)
       {
-        hideColumns(r[0], r[1], true);
+        hideColumns(r[0], r[1]);
       }
     } finally
     {
@@ -1220,8 +1189,7 @@ public class HiddenColumns
       }
 
       hiddenColumns = null;
-    }
-    finally
+    } finally
     {
       LOCK.writeLock().unlock();
     }
@@ -1256,8 +1224,7 @@ public class HiddenColumns
       {
         hiddenColumns = null;
       }
-    }
-    finally
+    } finally
     {
       LOCK.writeLock().unlock();
     }
@@ -1381,8 +1348,7 @@ public class HiddenColumns
           hiddenColumns = null;
         }
       }
-    }
-    finally
+    } finally
     {
       LOCK.writeLock().unlock();
     }
@@ -1431,8 +1397,7 @@ public class HiddenColumns
     // recover mapping between sequence's non-gap positions and positions
     // mapping to view.
     pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
-    int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
-            profileseq.getLength());
+    int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
     int spos = 0;
     int offset = 0;
 
@@ -1481,9 +1446,8 @@ public class HiddenColumns
             }
             else
             {
-              al.getSequenceAt(s).setSequence(
-                      sq.substring(0, spos + offset) + sb.toString()
-                              + sq.substring(spos + offset));
+              al.getSequenceAt(s).setSequence(sq.substring(0, spos + offset)
+                      + sb.toString() + sq.substring(spos + offset));
             }
           }
         }
@@ -1495,7 +1459,8 @@ public class HiddenColumns
     {
       // pad the final region with gaps.
       StringBuffer sb = new StringBuffer();
-      for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
+      for (int s = 0, ns = profileseq.getLength() - spos
+              - offset; s < ns; s++)
       {
         sb.append(gc);
       }
@@ -1597,8 +1562,7 @@ public class HiddenColumns
         }
       }
       return hashCode;
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }
@@ -1620,7 +1584,7 @@ public class HiddenColumns
                       .nextSetBit(lastSet))
       {
         lastSet = inserts.nextClearBit(firstSet);
-        hideColumns(firstSet, lastSet - 1, true);
+        hideColumns(firstSet, lastSet - 1);
       }
     } finally
     {
@@ -1646,8 +1610,7 @@ public class HiddenColumns
       {
         inserts.set(range[0], range[1] + 1);
       }
-    }
-    finally
+    } finally
     {
       LOCK.readLock().unlock();
     }