public class AnnotationSorter
{
+ public enum SortOrder
+ {
+ SEQUENCE_AND_TYPE, TYPE_AND_SEQUENCE
+ }
+
private final AlignmentI alignment;
public AnnotationSorter(AlignmentI alignmentI)
}
};
+ 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());
}
}