1 package jalview.datamodel.features;
3 import jalview.datamodel.ContiguousI;
5 import java.util.Comparator;
8 * A comparator that orders ranges by either start position or end position
9 * ascending. If the position matches, ordering is resolved by end position (or
15 public class RangeComparator implements Comparator<ContiguousI>
17 public static final Comparator<ContiguousI> BY_START_POSITION = new RangeComparator(
20 public static final Comparator<ContiguousI> BY_END_POSITION = new RangeComparator(
28 * @param byStartPosition
29 * if true, order based on start position, if false by end position
31 RangeComparator(boolean byStartPosition)
33 byStart = byStartPosition;
37 public int compare(ContiguousI o1, ContiguousI o2)
39 int len1 = o1.getEnd() - o1.getBegin();
40 int len2 = o2.getEnd() - o2.getBegin();
44 return compare(o1.getBegin(), o2.getBegin(), len1, len2);
48 return compare(o1.getEnd(), o2.getEnd(), len1, len2);
53 * Compares two ranges for ordering
56 * first range positional ordering criterion
58 * second range positional ordering criterion
60 * first range length ordering criterion
62 * second range length ordering criterion
65 public int compare(long pos1, long pos2, int len1, int len2)
67 int order = Long.compare(pos1, pos2);
71 * if tied on position order, longer length sorts to left
72 * i.e. the negation of normal ordering by length
74 order = -Integer.compare(len1, len2);