4 Copyright (c) 2018, Mungo Carstairs
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
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.
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.
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.
32 package intervalstore.api;
34 import java.util.Comparator;
36 public interface IntervalI
39 * Compares intervals by start position ascending and end position descending
41 static Comparator<? super IntervalI> COMPARE_BEGIN_ASC_END_DESC = new Comparator<IntervalI>()
44 public int compare(IntervalI o1, IntervalI o2)
46 int ret = Integer.signum(o1.getBegin() - o2.getBegin());
47 return (ret == 0 ? Integer.signum(o2.getEnd() - o1.getEnd()) : ret);
52 * Compares intervals by start position ascending and end position ascending
54 static Comparator<? super IntervalI> COMPARE_BEGIN_ASC_END_ASC = new Comparator<IntervalI>()
57 public int compare(IntervalI o1, IntervalI o2)
59 int ret = Integer.signum(o1.getBegin() - o2.getBegin());
60 return (ret == 0 ? Integer.signum(o1.getEnd() - o2.getEnd()) : ret);
65 * Compares intervals by start position ascending
67 static Comparator<? super IntervalI> COMPARE_BEGIN_ASC = new Comparator<IntervalI>()
70 public int compare(IntervalI o1, IntervalI o2)
72 return Integer.signum(o1.getBegin() - o2.getBegin());
77 * Compares intervals by end position descending
79 static Comparator<? super IntervalI> COMPARE_END_DESC = new Comparator<IntervalI>()
82 public int compare(IntervalI o1, IntervalI o2)
84 return Integer.signum(o2.getEnd() - o1.getEnd());
89 * Answers the start position of the interval
96 * Answers the end position of the interval
103 * Answers true if this interval contains (or matches) the given interval
108 default boolean containsInterval(IntervalI i)
111 && i.getBegin() >= getBegin() && i.getEnd() <= getEnd();
115 * Answers true if this interval properly contains the given interval, that
116 * is, it contains it and is larger than it
121 default boolean properlyContainsInterval(IntervalI i)
123 return containsInterval(i)
124 && (i.getBegin() > getBegin() || i.getEnd() < getEnd());
128 * Answers true if the interval has the same begin and end as this one, else
134 default boolean equalsInterval(IntervalI i)
136 return i != null && i.getBegin() == getBegin()
137 && i.getEnd() == getEnd();
141 * Answers true if interval i overlaps this one (they share at least one
142 * position), else false
147 default boolean overlapsInterval(IntervalI i)
153 if (i.getBegin() < getBegin())
155 return i.getEnd() >= getBegin();
157 if (i.getEnd() > getEnd())
159 return i.getBegin() <= getEnd();
161 return true; // i internal to this