View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  
13  package sk.uniba.euromath.editor.textEditor;
14  
15  /***
16   * Interval of non-negative numbers, start index is included in interval, end index
17   * not. Range of this interval is positive.
18   * 
19   * @author Tomáš Studva 7.2.2006 Martin Kollar
20   */
21  public class Interval {
22          /***
23           * Start of the interval.
24           */
25          private int start;
26  
27          /***
28           * End of the interval.
29           */
30          private int end;
31  
32          /***
33           * Contructor. For interval holds 0 <= start < end.
34           * 
35           * @param start
36           *                of interval, must be non-negative
37           * @param end
38           *                of interval, greater than start, is not included in interval
39           */
40          public Interval(int start, int end) {
41                  assert (0 <= start);
42                  assert (start < end);
43                  this.start = start;
44                  this.end = end;
45          }
46  
47          /***
48           * Returns starting index.
49           * 
50           * @return starting index
51           */
52          public int getStart() {
53                  return start;
54          }
55  
56          /***
57           * Returns ending index.
58           * 
59           * @return ending index
60           */
61          public int getEnd() {
62                  return end;
63          }
64  
65          /***
66           * Tests if index falls into interval (into or onto bounds)
67           * 
68           * @param index
69           *                to test
70           * @return <code>true</code> if index is inside of this interval
71           */
72          public boolean contains(int index) {
73                  return ((start <= index) && (index <= end));
74          }
75  
76          /***
77           * Tests if this interval contains interval.
78           * 
79           * @param interval
80           *                Interval to test
81           * @return <code> true </code> if <code> interval </code> is inside this
82           *         interval
83           */
84          public boolean contains(Interval interval) {
85                  return (contains(interval.getStart()) && contains(interval
86                                  .getEnd()));
87          }
88  
89          /***
90           * Tests if this interval intersects interval.
91           * 
92           * @param interval
93           *                Interval to test for inersection
94           * @return <code> true </code> if their intersection is not empty or
95           *         they are adjacent
96           */
97          public boolean intersects(Interval interval) {
98                  return (contains(interval.getStart()) || contains(interval
99                                  .getEnd()));
100         }
101 
102         /***
103          * Computes OR of two intervals. Interval to OR have to intersect.
104          * 
105          * @param interval1
106          * @param interval2
107          * 
108          * @return logical OR of this intersecting intervals
109          */
110         public static Interval ORIntervals(Interval interval1,
111                         Interval interval2) {
112                 if (interval1.intersects(interval2)) {
113 
114                         return new Interval(Math.min(interval1.start,
115                                         interval2.start), Math.max(
116                                         interval1.end, interval2.end));
117                 }
118                 throw new IllegalArgumentException(
119                                 "Intervals do not intersect!");
120         }
121 
122         @Override
123         public String toString() {
124                 return "Interval: " + start + "," + end;
125         }
126 }