JAL-3263, JAL 3264 New Transpiler fixes FunctionalInterface issues,
[jalview.git] / srcjar / intervalstore / impl / NCListBuilder.java
1 /*
2 BSD 3-Clause License
3
4 Copyright (c) 2018, Mungo Carstairs
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this
11   list of conditions and the following disclaimer.
12
13 * Redistributions in binary form must reproduce the above copyright notice,
14   this list of conditions and the following disclaimer in the documentation
15   and/or other materials provided with the distribution.
16
17 * Neither the name of the copyright holder nor the names of its
18   contributors may be used to endorse or promote products derived from
19   this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 package intervalstore.impl;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
38
39 import intervalstore.api.IntervalI;
40
41 /**
42  * A comparator that orders ranges by either start position ascending. If
43  * position matches, ordering is by length descending. This provides the
44  * canonical ordering of intervals into subranges in order to build a nested
45  * containment list.
46  * 
47  * @author gmcarstairs
48  */
49 public class NCListBuilder<T extends IntervalI>
50 {
51   class NCListComparator<V extends IntervalI> implements Comparator<V>
52   {
53     /**
54      * Compares two intervals in a way that will sort a list by start position
55      * ascending, then by length descending. Answers
56      * <ul>
57      * <li>a negative value if o1.begin < o2.begin</li>
58      * <li>else a positive value if o1.begin > o2.begin</li>
59      * <li>else a negative value if o1.end > o2.end</li>
60      * <li>else a positive value of o1.end < o2.end</li>
61      * <li>else zero</li
62      * </ul>
63      */
64     @Override
65     public int compare(V o1, V o2)
66     {
67       int order = Integer.compare(o1.getBegin(), o2.getBegin());
68       if (order == 0)
69       {
70         /*
71          * if tied on start position, longer length sorts to left
72          * i.e. the negation of normal ordering by length
73          */
74         order = Integer.compare(o2.getEnd(), o1.getEnd());
75       }
76       return order;
77     }
78   }
79
80   private Comparator<T> comparator = new NCListComparator<>();
81   
82   /**
83    * Default constructor
84    */
85   public NCListBuilder()
86   {
87   }
88
89   /**
90    * Answers a comparator which sorts items of type T by start position
91    * ascending, and within that by end position (length) descending)
92    * 
93    * @return
94    */
95   Comparator<T> getComparator()
96   {
97     return comparator;
98   }
99
100   /**
101    * Sorts and traverses the ranges to identify sublists, whose start intervals
102    * are overlapping or disjoint but not mutually contained. Answers a list of
103    * start-end indices of the sorted list of ranges.
104    * 
105    * @param ranges
106    * @return
107    */
108   List<IntervalI> partitionNestedSublists(List<T> ranges)
109   {
110     List<IntervalI> sublists = new ArrayList<>();
111   
112     /*
113      * sort by start ascending, length descending, so that
114      * contained intervals follow their containing interval
115      */
116     Collections.sort(ranges, comparator);
117   
118     int listStartIndex = 0;
119   
120     IntervalI lastParent = ranges.get(0);
121     boolean first = true;
122   
123     for (int i = 0; i < ranges.size(); i++)
124     {
125       IntervalI nextInterval = ranges.get(i);
126       if (!first && !lastParent.properlyContainsInterval(nextInterval))
127       {
128         /*
129          * this interval is not properly contained in the parent; 
130          * close off the last sublist
131          */
132         sublists.add(new Range(listStartIndex, i - 1));
133         listStartIndex = i;
134         lastParent = nextInterval;
135       }
136       first = false;
137     }
138   
139     sublists.add(new Range(listStartIndex, ranges.size() - 1));
140     return sublists;
141   }
142 }