Java 11 integration;
[jalview.git] / src2 / intervalstore / api / IntervalI.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.api;
33
34 import java.util.Comparator;
35
36 public interface IntervalI
37 {
38   /**
39    * Compares intervals by start position ascending and end position descending
40    */
41   static Comparator<? super IntervalI> COMPARE_BEGIN_ASC_END_DESC = new Comparator<IntervalI>()
42   {
43     @Override
44     public int compare(IntervalI o1, IntervalI o2)
45     {
46       int ret = Integer.signum(o1.getBegin() - o2.getBegin());
47       return (ret == 0 ? Integer.signum(o2.getEnd() - o1.getEnd()) : ret);
48     }
49   };
50
51   /**
52    * Compares intervals by start position ascending and end position ascending
53    */
54   static Comparator<? super IntervalI> COMPARE_BEGIN_ASC_END_ASC = new Comparator<IntervalI>()
55   {
56     @Override
57     public int compare(IntervalI o1, IntervalI o2)
58     {
59       int ret = Integer.signum(o1.getBegin() - o2.getBegin());
60       return (ret == 0 ? Integer.signum(o1.getEnd() - o2.getEnd()) : ret);
61     }
62   };
63
64   /**
65    * Compares intervals by start position ascending
66    */
67   static Comparator<? super IntervalI> COMPARE_BEGIN_ASC = new Comparator<IntervalI>()
68   {
69     @Override
70     public int compare(IntervalI o1, IntervalI o2)
71     {
72       return Integer.signum(o1.getBegin() - o2.getBegin());
73     }
74   };
75
76   /**
77    * Compares intervals by end position descending
78    */
79   static Comparator<? super IntervalI> COMPARE_END_DESC = new Comparator<IntervalI>()
80   {
81     @Override
82     public int compare(IntervalI o1, IntervalI o2)
83     {
84       return Integer.signum(o2.getEnd() - o1.getEnd());
85     }
86   };
87
88   /**
89    * Answers the start position of the interval
90    * 
91    * @return
92    */
93   int getBegin();
94
95   /**
96    * Answers the end position of the interval
97    * 
98    * @return
99    */
100   int getEnd();
101
102   /**
103    * Answers true if this interval contains (or matches) the given interval
104    * 
105    * @param i
106    * @return
107    */
108   default boolean containsInterval(IntervalI i)
109   {
110     return i != null
111             && i.getBegin() >= getBegin() && i.getEnd() <= getEnd();
112   }
113
114   /**
115    * Answers true if this interval properly contains the given interval, that
116    * is, it contains it and is larger than it
117    * 
118    * @param i
119    * @return
120    */
121   default boolean properlyContainsInterval(IntervalI i)
122   {
123     return containsInterval(i)
124             && (i.getBegin() > getBegin() || i.getEnd() < getEnd());
125   }
126
127   /**
128    * Answers true if the interval has the same begin and end as this one, else
129    * false
130    * 
131    * @param i
132    * @return
133    */
134   default boolean equalsInterval(IntervalI i)
135   {
136     return i != null && i.getBegin() == getBegin()
137             && i.getEnd() == getEnd();
138   }
139
140   /**
141    * Answers true if interval i overlaps this one (they share at least one
142    * position), else false
143    * 
144    * @param i
145    * @return
146    */
147   default boolean overlapsInterval(IntervalI i)
148   {
149     if (i == null)
150     {
151       return false;
152     }
153     if (i.getBegin() < getBegin())
154     {
155       return i.getEnd() >= getBegin();
156     }
157     if (i.getEnd() > getEnd())
158     {
159       return i.getBegin() <= getEnd();
160     }
161     return true; // i internal to this
162   }
163 }