JAL-3120 preserve feature colour/mincolour/maxcolour while modifying,
[jalview.git] / src / jalview / datamodel / features / RangeComparator.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel.features;
22
23 import jalview.datamodel.ContiguousI;
24
25 import java.util.Comparator;
26
27 /**
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
30  * start position).
31  * 
32  * @author gmcarstairs
33  *
34  */
35 public class RangeComparator implements Comparator<ContiguousI>
36 {
37   public static final Comparator<ContiguousI> BY_START_POSITION = new RangeComparator(
38           true);
39
40   public static final Comparator<ContiguousI> BY_END_POSITION = new RangeComparator(
41           false);
42
43   boolean byStart;
44
45   /**
46    * Constructor
47    * 
48    * @param byStartPosition
49    *          if true, order based on start position, if false by end position
50    */
51   RangeComparator(boolean byStartPosition)
52   {
53     byStart = byStartPosition;
54   }
55
56   @Override
57   public int compare(ContiguousI o1, ContiguousI o2)
58   {
59     int len1 = o1.getEnd() - o1.getBegin();
60     int len2 = o2.getEnd() - o2.getBegin();
61
62     if (byStart)
63     {
64       return compare(o1.getBegin(), o2.getBegin(), len1, len2);
65     }
66     else
67     {
68       return compare(o1.getEnd(), o2.getEnd(), len1, len2);
69     }
70   }
71
72   /**
73    * Compares two ranges for ordering
74    * 
75    * @param pos1
76    *          first range positional ordering criterion
77    * @param pos2
78    *          second range positional ordering criterion
79    * @param len1
80    *          first range length ordering criterion
81    * @param len2
82    *          second range length ordering criterion
83    * @return
84    */
85   public int compare(long pos1, long pos2, int len1, int len2)
86   {
87     int order = Long.compare(pos1, pos2);
88     if (order == 0)
89     {
90       /*
91        * if tied on position order, longer length sorts to left
92        * i.e. the negation of normal ordering by length
93        */
94       order = -Integer.compare(len1, len2);
95     }
96     return order;
97   }
98 }