JAL-3397 final update
[jalview.git] / src / 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.Collections;
35 import java.util.Comparator;
36 import java.util.List;
37
38 public interface IntervalI
39 {
40
41   /**
42    * Compare intervals by start position ascending and end position descending.
43    * 
44    * BIGENDIAN sorts 10-100 ahead of 10-80 (original IntervalStoreJ method
45    * 
46    */
47   static Comparator<? super IntervalI> COMPARATOR_BIGENDIAN = new Comparator<IntervalI>()
48   {
49     @Override
50     public int compare(IntervalI o1, IntervalI o2)
51     {
52       int ret = Integer.signum(o1.getBegin() - o2.getBegin());
53       return (ret == 0 ? Integer.signum(o2.getEnd() - o1.getEnd()) : ret);
54     }
55   };
56
57   /**
58    * Compare intervals by start position ascending and end position descending.
59    * 
60    * LITTLEENDIAN sorts 10-100 after 10-80
61    * 
62    */
63   static Comparator<? super IntervalI> COMPARATOR_LITTLEENDIAN = new Comparator<IntervalI>()
64   {
65     @Override
66     public int compare(IntervalI o1, IntervalI o2)
67     {
68       int ret = Integer.signum(o1.getBegin() - o2.getBegin());
69       return (ret == 0 ? Integer.signum(o1.getEnd() - o2.getEnd()) : ret);
70     }
71   };
72
73   /**
74    * a comparator for sorting intervals by start position ascending
75    */
76   static Comparator<? super IntervalI> FORWARD_STRAND = new Comparator<IntervalI>()
77   {
78     @Override
79     public int compare(IntervalI o1, IntervalI o2)
80     {
81       return Integer.signum(o1.getBegin() - o2.getBegin());
82     }
83   };
84
85   /**
86    * a comparator for sorting intervals by end position descending
87    */
88   static Comparator<? super IntervalI> REVERSE_STRAND = new Comparator<IntervalI>()
89   {
90     @Override
91     public int compare(IntervalI o1, IntervalI o2)
92     {
93       return Integer.signum(o2.getEnd() - o1.getEnd());
94     }
95   };
96
97   static int NOT_CONTAINED = Integer.MIN_VALUE;
98   static int CONTAINMENT_UNKNOWN = 0;
99
100   int getBegin();
101   int getEnd();
102
103   /**
104    * Answers true if this interval contains (or matches) the given interval
105    * based solely on start and end.
106    * 
107    * @param i
108    * @return
109    */
110   default boolean containsInterval(IntervalI i)
111   {
112     return i != null && i.getBegin() >= getBegin()
113             && i.getEnd() <= getEnd();
114   }
115
116
117   /**
118    * Answers true if this interval properly contains the given interval, that
119    * is, it contains it and is larger than it
120    * 
121    * @param i
122    * @return
123    */
124   default boolean properlyContainsInterval(IntervalI i)
125   {
126     return containsInterval(i)
127             && (i.getBegin() > getBegin() || i.getEnd() < getEnd());
128   }
129
130   /**
131    * Slower than equalsInterval; also includes type.
132    * 
133    * Ensure that subclasses override equals(Object). For example:
134    * 
135    * public boolean equals(Object o) { return o != null && o instanceof XXX &&
136    * equalsInterval((XXX) i); }
137    * 
138    * 
139    * equalsInterval also must be overridden.
140    * 
141    * public boolean equalsInterval(IntervalI i) {return ((SimpleFeature)i).start
142    * == start && ((SimpleFeature)i).end == end && ((SimpleFeature)i).description
143    * == this.description; }
144    * 
145    * 
146    * @param o
147    * @return true if equal, including a type check
148    */
149   @Override
150   abstract boolean equals(Object o);
151
152
153
154   /**
155    * Check that two intervals are equal, in terms of end points, descriptions,
156    * or any other distinguishing features.
157    * 
158    * Used in IntervalStore in searches, since it is faster than equals(), as at
159    * that point we know that we have the correct type.
160    * 
161    * @param i
162    *          may be null
163    * @return true if equal; null value must return false, not throw
164    *         NullPointerException
165    */
166   abstract boolean equalsInterval(IntervalI i);
167
168   default boolean overlapsInterval(IntervalI i)
169   {
170     if (i == null)
171     {
172       return false;
173     }
174     if (i.getBegin() < getBegin())
175     {
176       return i.getEnd() >= getBegin();
177     }
178     if (i.getEnd() > getEnd())
179     {
180       return i.getBegin() <= getEnd();
181     }
182     return true; // i internal to this
183   }
184
185   /**
186    * Sorts the list by start position ascending (if forwardString==true), or by
187    * end position descending
188    * 
189    * @param intervals
190    * @param forwardStrand
191    */
192   static void sortIntervals(List<? extends IntervalI> intervals,
193           final boolean forwardStrand)
194   {
195     Collections.sort(intervals,
196             forwardStrand ? FORWARD_STRAND : REVERSE_STRAND);
197   }
198
199
200 }