2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel.features;
23 import jalview.datamodel.ContiguousI;
25 import java.util.Comparator;
28 * A comparator that orders ranges by either start position or end position
29 * ascending. If the position matches, ordering is resolved by end position (or
35 public class RangeComparator implements Comparator<ContiguousI>
37 public static final Comparator<ContiguousI> BY_START_POSITION = new RangeComparator(
40 public static final Comparator<ContiguousI> BY_END_POSITION = new RangeComparator(
48 * @param byStartPosition
49 * if true, order based on start position, if false by end position
51 RangeComparator(boolean byStartPosition)
53 byStart = byStartPosition;
57 public int compare(ContiguousI o1, ContiguousI o2)
59 int len1 = o1.getEnd() - o1.getBegin();
60 int len2 = o2.getEnd() - o2.getBegin();
64 return compare(o1.getBegin(), o2.getBegin(), len1, len2);
68 return compare(o1.getEnd(), o2.getEnd(), len1, len2);
73 * Compares two ranges for ordering
76 * first range positional ordering criterion
78 * second range positional ordering criterion
80 * first range length ordering criterion
82 * second range length ordering criterion
85 public int compare(long pos1, long pos2, int len1, int len2)
87 int order = Long.compare(pos1, pos2);
91 * if tied on position order, longer length sorts to left
92 * i.e. the negation of normal ordering by length
94 order = -Integer.compare(len1, len2);