JAL-1152 with sticky annotation sort order that updates as sequences are
[jalview.git] / src / jalview / analysis / AnnotationSorter.java
index 4ee2b86..289544c 100644 (file)
@@ -17,6 +17,11 @@ import java.util.Comparator;
 public class AnnotationSorter
 {
 
+  public enum SortOrder
+  {
+    SEQUENCE_AND_TYPE, TYPE_AND_SEQUENCE
+  }
+  
   private final AlignmentI alignment;
 
   public AnnotationSorter(AlignmentI alignmentI)
@@ -117,39 +122,49 @@ public class AnnotationSorter
     }
   };
 
+  private final Comparator<? super AlignmentAnnotation> DEFAULT_COMPARATOR = bySequenceAndType;
+  
   /**
-   * Sort by annotation type (label), within sequence order.
-   * Non-sequence-related annotations sort to the end.
+   * Sort by the specified order.
    * 
    * @param alignmentAnnotations
+   * @param order
    */
-  public void sortBySequenceAndType(
-          AlignmentAnnotation[] alignmentAnnotations)
+  public void sort(AlignmentAnnotation[] alignmentAnnotations,
+          SortOrder order)
   {
+    Comparator<? super AlignmentAnnotation> comparator = getComparator(order);
+
     if (alignmentAnnotations != null)
     {
       synchronized (alignmentAnnotations)
       {
-        Arrays.sort(alignmentAnnotations, bySequenceAndType);
+        Arrays.sort(alignmentAnnotations, comparator);
       }
     }
   }
 
   /**
-   * Sort by sequence order within annotation type (label). Non-sequence-related
-   * annotations sort to the end.
+   * Get the comparator for the specified sort order.
    * 
-   * @param alignmentAnnotations
+   * @param order
+   * @return
    */
-  public void sortByTypeAndSequence(
-          AlignmentAnnotation[] alignmentAnnotations)
+  private Comparator<? super AlignmentAnnotation> getComparator(
+          SortOrder order)
   {
-    if (alignmentAnnotations != null)
+    if (order == null)
     {
-      synchronized (alignmentAnnotations)
-      {
-        Arrays.sort(alignmentAnnotations, byTypeAndSequence);
-      }
+      return DEFAULT_COMPARATOR;
+    }
+    switch (order)
+    {
+    case SEQUENCE_AND_TYPE:
+      return this.bySequenceAndType;
+    case TYPE_AND_SEQUENCE:
+      return this.byTypeAndSequence;
+    default:
+      throw new UnsupportedOperationException(order.toString());
     }
   }