Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / unused / nonc / 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    * a comparator for sorting intervals by start position ascending
42    */
43   static Comparator<? super IntervalI> FORWARD_STRAND = new Comparator<IntervalI>()
44   {
45     @Override
46     public int compare(IntervalI o1, IntervalI o2)
47     {
48       return Integer.compare(o1.getBegin(), o2.getBegin());
49     }
50   };
51
52   /**
53    * a comparator for sorting intervals by end position descending
54    */
55   static Comparator<? super IntervalI> REVERSE_STRAND = new Comparator<IntervalI>()
56   {
57     @Override
58     public int compare(IntervalI o1, IntervalI o2)
59     {
60       return Integer.compare(o2.getEnd(), o1.getEnd());
61     }
62   };
63
64   int getBegin();
65
66   int getEnd();
67
68   /**
69    * Answers true if this interval contains (or matches) the given interval
70    * 
71    * @param i
72    * @return
73    */
74   default boolean containsInterval(IntervalI i)
75   {
76     return i != null
77             && i.getBegin() >= getBegin() && i.getEnd() <= getEnd();
78   }
79
80   /**
81    * Answers true if this interval properly contains the given interval, that
82    * is, it contains it and is larger than it
83    * 
84    * @param i
85    * @return
86    */
87   default boolean properlyContainsInterval(IntervalI i)
88   {
89     return containsInterval(i)
90             && (i.getBegin() > getBegin() || i.getEnd() < getEnd());
91   }
92
93   default boolean equalsInterval(IntervalI i)
94   {
95     return i != null && i.getBegin() == getBegin()
96             && i.getEnd() == getEnd();
97   }
98
99   default boolean overlapsInterval(IntervalI i)
100   {
101     if (i == null)
102     {
103       return false;
104     }
105     if (i.getBegin() < getBegin())
106     {
107       return i.getEnd() >= getBegin();
108     }
109     if (i.getEnd() > getEnd())
110     {
111       return i.getBegin() <= getEnd();
112     }
113     return true; // i internal to this
114   }
115
116   /**
117    * Sorts the list by start position ascending (if forwardString==true), or by
118    * end position descending
119    * 
120    * @param intervals
121    * @param forwardStrand
122    */
123   static void sortIntervals(List<? extends IntervalI> intervals,
124           final boolean forwardStrand)
125   {
126     Collections.sort(intervals,
127             forwardStrand ? FORWARD_STRAND : REVERSE_STRAND);
128   }
129
130   IntervalI getContainedBy();
131
132   void setContainedBy(IntervalI containedBy);
133
134 }