Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel.features

File RangeComparator.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
10
3
1
98
38
5
0.5
3.33
3
1.67

Classes

Class Line # Actions
RangeComparator 35 10 5 0
1.0100%
 

Contributing tests

This file is covered by 99 tests. .

Source view

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  4 toggle RangeComparator(boolean byStartPosition)
52    {
53  4 byStart = byStartPosition;
54    }
55   
 
56  463343 toggle @Override
57    public int compare(ContiguousI o1, ContiguousI o2)
58    {
59  463343 int len1 = o1.getEnd() - o1.getBegin();
60  463343 int len2 = o2.getEnd() - o2.getBegin();
61   
62  463343 if (byStart)
63    {
64  463289 return compare(o1.getBegin(), o2.getBegin(), len1, len2);
65    }
66    else
67    {
68  54 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  463348 toggle public int compare(long pos1, long pos2, int len1, int len2)
86    {
87  463348 int order = Long.compare(pos1, pos2);
88  463348 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  15705 order = -Integer.compare(len1, len2);
95    }
96  463348 return order;
97    }
98    }