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