JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / intervalstore / impl / BinarySearcher.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.List;
35 import java.util.function.Function;
36
37 /**
38  * Provides a method to perform binary search of an ordered list for the first
39  * entry that satisfies a supplied condition
40  * 
41  * @author gmcarstairs
42  */
43 public final class BinarySearcher
44 {
45   private BinarySearcher()
46   {
47   }
48
49   /**
50    * Performs a binary search of the list to find the index of the first entry
51    * for which the test returns true. Answers the length of the list if there is
52    * no such entry.
53    * <p>
54    * For correct behaviour, the provided list must be ordered consistent with
55    * the test, that is, any entries returning false must precede any entries
56    * returning true. Note that this means that this method is <em>not</em>
57    * usable to search for equality (to find a specific entry), as all unequal
58    * entries will answer false to the test. To do that, use
59    * <code>Collections.binarySearch</code> instead.
60    * 
61    * @param list
62    * @param test
63    * @return
64    * @see java.util.Collections#binarySearch(List, Object)
65    */
66   public static <T> int findFirst(List<? extends T> list,
67           Function<T, Boolean> test)
68   {
69     int start = 0;
70     int end = list.size() - 1;
71     int matched = list.size();
72   
73     while (start <= end)
74     {
75       int mid = (start + end) / 2;
76       T entry = list.get(mid);
77       boolean itsTrue = test.apply(entry);
78       if (itsTrue)
79       {
80         matched = mid;
81         end = mid - 1;
82       }
83       else
84       {
85         start = mid + 1;
86       }
87     }
88   
89     return matched;
90   }
91 }