1 package jalview.datamodel.features;
3 import java.util.Comparator;
6 * A comparator that orders ranges by either start position or end position
7 * ascending. If the position matches, ordering is resolved by end position (or
13 public class RangeComparator implements Comparator<ContiguousI>
15 public static final Comparator<ContiguousI> BY_START_POSITION = new RangeComparator(
18 public static final Comparator<ContiguousI> BY_END_POSITION = new RangeComparator(
26 * @param byStartPosition
27 * if true, order based on start position, if false by end position
29 RangeComparator(boolean byStartPosition)
31 byStart = byStartPosition;
35 public int compare(ContiguousI o1, ContiguousI o2)
37 int len1 = o1.getEnd() - o1.getBegin();
38 int len2 = o2.getEnd() - o2.getBegin();
42 return compare(o1.getBegin(), o2.getBegin(), len1, len2);
46 return compare(o1.getEnd(), o2.getEnd(), len1, len2);
51 * Compares two ranges for ordering
54 * first range positional ordering criterion
56 * second range positional ordering criterion
58 * first range length ordering criterion
60 * second range length ordering criterion
63 public int compare(long pos1, long pos2, int len1, int len2)
65 int order = Long.compare(pos1, pos2);
69 * if tied on position order, longer length sorts to left
70 * i.e. the negation of normal ordering by length
72 order = -Integer.compare(len1, len2);